use of com.sun.xml.xsom.SCD in project schema2proto by entur.
the class SCDDriver method main.
public static void main(String[] args) throws Exception {
XSOMParser p = new XSOMParser();
for (int i = 1; i < args.length; i++) p.parse(args[i]);
XSSchemaSet r = p.getResult();
SCD scd = SCD.create(args[0], new DummyNSContext());
Collection<XSComponent> result = scd.select(r);
for (XSComponent c : result) {
System.out.println(c.apply(new ComponentNameFunction()));
print(c.getLocator());
System.out.println();
}
System.out.printf("%1d match(s)\n", result.size());
}
use of com.sun.xml.xsom.SCD in project jaxb-ri by eclipse-ee4j.
the class SCDDriver method main.
public static void main(String[] args) throws Exception {
XSOMParser p = new XSOMParser();
for (int i = 1; i < args.length; i++) p.parse(args[i]);
XSSchemaSet r = p.getResult();
SCD scd = SCD.create(args[0], new DummyNSContext());
Collection<XSComponent> result = scd.select(r);
for (XSComponent c : result) {
System.out.println(c.apply(new ComponentNameFunction()));
print(c.getLocator());
System.out.println();
}
System.out.printf("%1d match(s)\n", result.size());
}
use of com.sun.xml.xsom.SCD in project jaxb-ri by eclipse-ee4j.
the class Internalizer method buildTargetNodeMap.
/**
* Determines the target node of the "bindings" element
* by using the inherited target node, then put
* the result into the "result" map and the "scd" map.
*
* @param inheritedTarget
* The current target node. This always exists, even if
* the user starts specifying targets via SCD (in that case
* this inherited target is just not going to be used.)
* @param inheritedSCD
* If the ancestor {@code <bindings>} node specifies @scd to
* specify the target via SCD, then this parameter represents that context.
*/
private void buildTargetNodeMap(Element bindings, @NotNull Node inheritedTarget, @Nullable SCDBasedBindingSet.Target inheritedSCD, Map<Element, List<Node>> result, SCDBasedBindingSet scdResult) {
// start by the inherited target
Node target = inheritedTarget;
ArrayList<Node> targetMultiple = null;
// validate this node ?
// validate(bindings);
boolean required = true;
boolean multiple = false;
if (bindings.getAttribute("required") != null) {
String requiredAttr = bindings.getAttribute("required");
if (requiredAttr.equals("no") || requiredAttr.equals("false") || requiredAttr.equals("0"))
required = false;
}
if (bindings.getAttribute("multiple") != null) {
String requiredAttr = bindings.getAttribute("multiple");
if (requiredAttr.equals("yes") || requiredAttr.equals("true") || requiredAttr.equals("1"))
multiple = true;
}
// look for @schemaLocation
if (bindings.getAttributeNode("schemaLocation") != null) {
String schemaLocation = bindings.getAttribute("schemaLocation");
// enhancement - schemaLocation="*" = bind to all schemas..
if (schemaLocation.equals("*")) {
for (String systemId : forest.listSystemIDs()) {
result.computeIfAbsent(bindings, k -> new ArrayList<>());
result.get(bindings).add(forest.get(systemId).getDocumentElement());
Element[] children = DOMUtils.getChildElements(bindings, Const.JAXB_NSURI, "bindings");
for (Element value : children) buildTargetNodeMap(value, forest.get(systemId).getDocumentElement(), inheritedSCD, result, scdResult);
}
return;
} else {
try {
// TODO: use the URI class
// TODO: honor xml:base
URL loc = new URL(new URL(forest.getSystemId(bindings.getOwnerDocument())), schemaLocation);
schemaLocation = loc.toExternalForm();
target = forest.get(schemaLocation);
if ((target == null) && (loc.getProtocol().startsWith("file"))) {
File f = new File(loc.getFile());
schemaLocation = new File(f.getCanonicalPath()).toURI().toString();
}
} catch (MalformedURLException e) {
} catch (IOException e) {
Logger.getLogger(Internalizer.class.getName()).log(Level.FINEST, e.getLocalizedMessage());
}
target = forest.get(schemaLocation);
if (target == null) {
reportError(bindings, Messages.format(Messages.ERR_INCORRECT_SCHEMA_REFERENCE, schemaLocation, EditDistance.findNearest(schemaLocation, forest.listSystemIDs())));
// abort processing this <jaxb:bindings>
return;
}
target = ((Document) target).getDocumentElement();
}
}
// look for @node
if (bindings.getAttributeNode("node") != null) {
String nodeXPath = bindings.getAttribute("node");
// evaluate this XPath
NodeList nlst;
try {
xpath.setNamespaceContext(new NamespaceContextImpl(bindings));
nlst = (NodeList) xpath.evaluate(nodeXPath, target, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
if (required) {
reportError(bindings, Messages.format(Messages.ERR_XPATH_EVAL, e.getMessage()), e);
// abort processing this <jaxb:bindings>
return;
} else {
return;
}
}
if (nlst.getLength() == 0) {
if (required)
reportError(bindings, Messages.format(Messages.NO_XPATH_EVAL_TO_NO_TARGET, nodeXPath));
// abort
return;
}
if (nlst.getLength() != 1) {
if (!multiple) {
reportError(bindings, Messages.format(Messages.NO_XPATH_EVAL_TOO_MANY_TARGETS, nodeXPath, nlst.getLength()));
// abort
return;
} else {
if (targetMultiple == null)
targetMultiple = new ArrayList<>();
for (int i = 0; i < nlst.getLength(); i++) {
targetMultiple.add(nlst.item(i));
}
}
}
// check
if (!multiple || nlst.getLength() == 1) {
Node rnode = nlst.item(0);
if (!(rnode instanceof Element)) {
reportError(bindings, Messages.format(Messages.NO_XPATH_EVAL_TO_NON_ELEMENT, nodeXPath));
// abort
return;
}
if (!forest.logic.checkIfValidTargetNode(forest, bindings, (Element) rnode)) {
reportError(bindings, Messages.format(Messages.XPATH_EVAL_TO_NON_SCHEMA_ELEMENT, nodeXPath, rnode.getNodeName()));
// abort
return;
}
target = rnode;
} else {
for (Node rnode : targetMultiple) {
if (!(rnode instanceof Element)) {
reportError(bindings, Messages.format(Messages.NO_XPATH_EVAL_TO_NON_ELEMENT, nodeXPath));
// abort
return;
}
if (!forest.logic.checkIfValidTargetNode(forest, bindings, (Element) rnode)) {
reportError(bindings, Messages.format(Messages.XPATH_EVAL_TO_NON_SCHEMA_ELEMENT, nodeXPath, rnode.getNodeName()));
// abort
return;
}
}
}
}
// look for @scd
if (bindings.getAttributeNode("scd") != null) {
String scdPath = bindings.getAttribute("scd");
if (!enableSCD) {
// SCD selector was found, but it's not activated. report an error
// but recover by handling it anyway. this also avoids repeated error messages.
reportError(bindings, Messages.format(Messages.SCD_NOT_ENABLED));
enableSCD = true;
}
try {
inheritedSCD = scdResult.createNewTarget(inheritedSCD, bindings, SCD.create(scdPath, new NamespaceContextImpl(bindings)));
} catch (ParseException e) {
reportError(bindings, Messages.format(Messages.ERR_SCD_EVAL, e.getMessage()), e);
// abort processing this bindings
return;
}
}
// update the result map
if (inheritedSCD != null) {
inheritedSCD.addBinidng(bindings);
} else if (!multiple || targetMultiple == null) {
result.computeIfAbsent(bindings, k -> new ArrayList<>());
result.get(bindings).add(target);
} else {
for (Node rnode : targetMultiple) {
result.computeIfAbsent(bindings, k -> new ArrayList<>());
result.get(bindings).add(rnode);
}
}
// look for child <jaxb:bindings> and process them recursively
Element[] children = DOMUtils.getChildElements(bindings, Const.JAXB_NSURI, "bindings");
for (Element value : children) if (!multiple || targetMultiple == null)
buildTargetNodeMap(value, target, inheritedSCD, result, scdResult);
else {
for (Node rnode : targetMultiple) {
buildTargetNodeMap(value, rnode, inheritedSCD, result, scdResult);
}
}
}
Aggregations