Search in sources :

Example 1 with SCD

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());
}
Also used : XSOMParser(com.sun.xml.xsom.parser.XSOMParser) SCD(com.sun.xml.xsom.SCD) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) ComponentNameFunction(com.sun.xml.xsom.util.ComponentNameFunction) XSComponent(com.sun.xml.xsom.XSComponent)

Example 2 with SCD

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());
}
Also used : XSOMParser(com.sun.xml.xsom.parser.XSOMParser) SCD(com.sun.xml.xsom.SCD) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) ComponentNameFunction(com.sun.xml.xsom.util.ComponentNameFunction) XSComponent(com.sun.xml.xsom.XSComponent)

Example 3 with SCD

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);
        }
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPath(javax.xml.xpath.XPath) NotNull(com.sun.istack.NotNull) URL(java.net.URL) XPathConstants(javax.xml.xpath.XPathConstants) Nullable(com.sun.istack.Nullable) HashMap(java.util.HashMap) SAXParseException2(com.sun.istack.SAXParseException2) Attr(org.w3c.dom.Attr) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) Document(org.w3c.dom.Document) Map(java.util.Map) Node(org.w3c.dom.Node) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) NamedNodeMap(org.w3c.dom.NamedNodeMap) ParseException(java.text.ParseException) XMLConstants(javax.xml.XMLConstants) DOMUtils(com.sun.tools.xjc.util.DOMUtils) SCD(com.sun.xml.xsom.SCD) NodeList(org.w3c.dom.NodeList) MalformedURLException(java.net.MalformedURLException) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) Const(com.sun.tools.xjc.reader.Const) XmlFactory(org.glassfish.jaxb.core.v2.util.XmlFactory) File(java.io.File) List(java.util.List) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) EditDistance(org.glassfish.jaxb.core.v2.util.EditDistance) MalformedURLException(java.net.MalformedURLException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) ParseException(java.text.ParseException) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File)

Aggregations

SCD (com.sun.xml.xsom.SCD)3 XSComponent (com.sun.xml.xsom.XSComponent)2 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)2 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)2 ComponentNameFunction (com.sun.xml.xsom.util.ComponentNameFunction)2 NotNull (com.sun.istack.NotNull)1 Nullable (com.sun.istack.Nullable)1 SAXParseException2 (com.sun.istack.SAXParseException2)1 ErrorReceiver (com.sun.tools.xjc.ErrorReceiver)1 Const (com.sun.tools.xjc.reader.Const)1 DOMUtils (com.sun.tools.xjc.util.DOMUtils)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1