1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.schema.element;
|
11
|
|
|
12
|
|
import java.io.StringReader;
|
13
|
|
|
14
|
|
import org.jbind.xml.base.IAttribute;
|
15
|
|
import org.jbind.xml.core.bridge.IXPath;
|
16
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
17
|
|
import org.jbind.xml.msg.XmlException;
|
18
|
|
import org.jbind.xml.msg.XmlMessages;
|
19
|
|
import org.jbind.xml.schema.instantiation.IElemValHelper;
|
20
|
|
import org.jbind.xml.schema.xpath.ParseException;
|
21
|
|
import org.jbind.xml.schema.xpath.XPathParser;
|
22
|
|
|
23
|
|
public class XPathComponent extends Annotated implements IXPathComponent {
|
24
|
|
|
25
|
|
private String myStringXPath = null;
|
26
|
|
private IXPath myXPath = null;
|
27
|
|
private boolean myIsRestrictedForm;
|
28
|
|
private boolean myIsSelectorNotField;
|
29
|
|
|
30
|
133
|
public XPathComponent(CreationParams aCreationParams, boolean anIsRestrictedForm, boolean anIsSelectorNotField) {
|
31
|
133
|
super(aCreationParams);
|
32
|
133
|
myIsRestrictedForm = anIsRestrictedForm;
|
33
|
133
|
myIsSelectorNotField = anIsSelectorNotField;
|
34
|
|
}
|
35
|
|
|
36
|
133
|
protected IAttribute doCreateAttribute(ACParams anACParams) throws XmlException {
|
37
|
133
|
IAttribute res = null;
|
38
|
133
|
String an = NameUtil.getSchemaAttributeName(anACParams);
|
39
|
133
|
if ("xpath".equals(an)) {
|
40
|
133
|
res = new Attribute(anACParams);
|
41
|
133
|
myStringXPath = res.getStringValue();
|
42
|
133
|
checkSyntax();
|
43
|
|
} else {
|
44
|
0
|
res = super.doCreateAttribute(anACParams);
|
45
|
|
}
|
46
|
133
|
return res;
|
47
|
|
}
|
48
|
|
|
49
|
133
|
public void validateElement(IElemValHelper aHelper, IConstraintViolations aViolations) {
|
50
|
133
|
super.validateElement(aHelper, aViolations);
|
51
|
133
|
if (null == myStringXPath) {
|
52
|
0
|
aViolations.add(XmlMessages.missingAttribute("xpath", this));
|
53
|
|
} else {
|
54
|
133
|
try {
|
55
|
133
|
myXPath = aHelper.createXPath(myStringXPath, getPrefixToNamespaceMapping(), myIsRestrictedForm);
|
56
|
|
} catch (Exception e) {
|
57
|
0
|
aViolations.add(XmlMessages.invalidXPath(myStringXPath, e, this));
|
58
|
|
}
|
59
|
|
}
|
60
|
|
}
|
61
|
|
|
62
|
133
|
private void checkSyntax() throws XmlException {
|
63
|
133
|
if (myIsRestrictedForm) {
|
64
|
97
|
XPathParser xpParser = new XPathParser(new StringReader(myStringXPath));
|
65
|
97
|
try {
|
66
|
97
|
if (myIsSelectorNotField) {
|
67
|
47
|
xpParser.Selector();
|
68
|
|
} else {
|
69
|
50
|
xpParser.Field();
|
70
|
|
}
|
71
|
|
} catch (ParseException e) {
|
72
|
0
|
throw new XmlException(XmlMessages.invalidXPath(myStringXPath, e, this));
|
73
|
|
}
|
74
|
|
}
|
75
|
|
}
|
76
|
|
|
77
|
127
|
public IXPath getXPath() {
|
78
|
127
|
return myXPath;
|
79
|
|
}
|
80
|
|
|
81
|
|
}
|
82
|
|
|