Search in sources :

Example 16 with XSElementDecl

use of com.sun.xml.xsom.XSElementDecl in project atlasmap by atlasmap.

the class SchemaInspector method printSchemaSet.

private void printSchemaSet(XSSchemaSet schemaSet) throws Exception {
    if (schemaSet == null) {
        throw new XmlInspectionException("Schema set is null");
    }
    XSSchema schema = rootNamespace != null ? schemaSet.getSchema(rootNamespace) : schemaSet.getSchema("");
    // we only care about declared elements...
    Iterator<XSElementDecl> jtr = schema.iterateElementDecls();
    while (jtr.hasNext()) {
        XSElementDecl e = jtr.next();
        String rootName = getNameNS(e);
        if (e.getType().isComplexType()) {
            XmlComplexType rootComplexType = getXmlComplexType();
            rootComplexType.setName(rootName);
            rootComplexType.setPath("/" + rootName);
            rootComplexType.setFieldType(FieldType.COMPLEX);
            xmlDocument.getFields().getField().add(rootComplexType);
            printComplexType(e.getType().asComplexType(), "/" + rootName, rootComplexType);
        } else if (e.getType().isSimpleType()) {
            XmlField xmlField = AtlasXmlModelFactory.createXmlField();
            xmlField.setName(rootName);
            xmlField.setPath("/" + rootName);
            xmlDocument.getFields().getField().add(xmlField);
            printSimpleType(e.getType().asSimpleType(), xmlField);
        }
    }
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlField(io.atlasmap.xml.v2.XmlField) XSElementDecl(com.sun.xml.xsom.XSElementDecl) XSSchema(com.sun.xml.xsom.XSSchema)

Example 17 with XSElementDecl

use of com.sun.xml.xsom.XSElementDecl in project atlasmap by atlasmap.

the class XmlModule method enforceSchema.

private Document enforceSchema(Document doc) {
    if (getDataSourceMetadata() == null || getDataSourceMetadata().getInspectionType() != InspectionType.SCHEMA || getDataSourceMetadata().getSpecification() == null || getDataSourceMetadata().getSpecification().length == 0) {
        return doc;
    }
    try {
        byte[] bytes = getDataSourceMetadata().getSpecification();
        AtlasXmlSchemaSetParser schemaParser = new AtlasXmlSchemaSetParser(getClassLoader());
        XSSchemaSet schemaSet = schemaParser.parse(new ByteArrayInputStream(bytes));
        Element sourceRoot = doc.getDocumentElement();
        String namespaceUri = sourceRoot.getNamespaceURI();
        if (namespaceUri == null) {
            namespaceUri = XMLConstants.NULL_NS_URI;
        }
        String localName = sourceRoot.getLocalName();
        if (XMLConstants.NULL_NS_URI.equals(namespaceUri)) {
            localName = sourceRoot.getTagName();
        }
        XSElementDecl rootDecl = schemaSet.getElementDecl(namespaceUri, localName);
        if (rootDecl == null) {
            LOG.warn("Declaration of the root element '{}' was not found in the schema", namespaceUri != null ? namespaceUri + ":" + localName : localName);
            return doc;
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document targetDoc = dbf.newDocumentBuilder().newDocument();
        rootDecl.visit(new AtlasRewritingXSVisitor(doc, targetDoc));
        return targetDoc;
    } catch (Exception e) {
        LOG.warn("Failed to load XML schema for the document '{}': {} - ignoring", getDocId(), e.getMessage());
        if (LOG.isDebugEnabled()) {
            LOG.debug("", e);
        }
        return doc;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) Element(org.w3c.dom.Element) AtlasRewritingXSVisitor(io.atlasmap.xml.core.schema.AtlasRewritingXSVisitor) AtlasXmlSchemaSetParser(io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser) XSElementDecl(com.sun.xml.xsom.XSElementDecl) Document(org.w3c.dom.Document) AtlasValidationException(io.atlasmap.api.AtlasValidationException) AtlasException(io.atlasmap.api.AtlasException)

Example 18 with XSElementDecl

use of com.sun.xml.xsom.XSElementDecl in project atlasmap by atlasmap.

the class SchemaTreeTraverser method schema.

/* (non-Javadoc)
     * @see com.sun.xml.xsom.visitor.XSVisitor#schema(com.sun.xml.xsom.XSSchema)
     */
public void schema(XSSchema s) {
    // QUICK HACK: don't print the built-in components
    if (s.getTargetNamespace().equals(Const.schemaNamespace)) {
        return;
    }
    SchemaTreeNode newNode = new SchemaTreeNode("Schema " + s.getLocator().getSystemId(), s.getLocator());
    this.currNode = newNode;
    this.model.addSchemaNode(newNode);
    for (XSAttGroupDecl groupDecl : s.getAttGroupDecls().values()) {
        attGroupDecl(groupDecl);
    }
    for (XSAttributeDecl attrDecl : s.getAttributeDecls().values()) {
        attributeDecl(attrDecl);
    }
    for (XSComplexType complexType : s.getComplexTypes().values()) {
        complexType(complexType);
    }
    for (XSElementDecl elementDecl : s.getElementDecls().values()) {
        elementDecl(elementDecl);
    }
    for (XSModelGroupDecl modelGroupDecl : s.getModelGroupDecls().values()) {
        modelGroupDecl(modelGroupDecl);
    }
    for (XSSimpleType simpleType : s.getSimpleTypes().values()) {
        simpleType(simpleType);
    }
}
Also used : XSAttGroupDecl(com.sun.xml.xsom.XSAttGroupDecl) XSSimpleType(com.sun.xml.xsom.XSSimpleType) XSComplexType(com.sun.xml.xsom.XSComplexType) XSModelGroupDecl(com.sun.xml.xsom.XSModelGroupDecl) XSAttributeDecl(com.sun.xml.xsom.XSAttributeDecl) XSElementDecl(com.sun.xml.xsom.XSElementDecl)

Example 19 with XSElementDecl

use of com.sun.xml.xsom.XSElementDecl in project atlasmap by atlasmap.

the class SchemaWriter method particle.

public void particle(XSParticle part) {
    BigInteger i;
    StringBuilder buf = new StringBuilder();
    i = part.getMaxOccurs();
    if (i.equals(BigInteger.valueOf(XSParticle.UNBOUNDED)))
        buf.append(" maxOccurs=\"unbounded\"");
    else if (!i.equals(BigInteger.ONE))
        buf.append(" maxOccurs=\"").append(i).append('\"');
    i = part.getMinOccurs();
    if (!i.equals(BigInteger.ONE))
        buf.append(" minOccurs=\"").append(i).append('\"');
    final String extraAtts = buf.toString();
    part.getTerm().visit(new XSTermVisitor() {

        public void elementDecl(XSElementDecl decl) {
            if (decl.isLocal())
                SchemaWriter.this.elementDecl(decl, extraAtts);
            else {
                // reference
                println(MessageFormat.format("<element ref=\"'{'{0}'}'{1}\"{2}/>", decl.getTargetNamespace(), decl.getName(), extraAtts));
            }
        }

        public void modelGroupDecl(XSModelGroupDecl decl) {
            // reference
            println(MessageFormat.format("<group ref=\"'{'{0}'}'{1}\"{2}/>", decl.getTargetNamespace(), decl.getName(), extraAtts));
        }

        public void modelGroup(XSModelGroup group) {
            SchemaWriter.this.modelGroup(group, extraAtts);
        }

        public void wildcard(XSWildcard wc) {
            SchemaWriter.this.wildcard("any", wc, extraAtts);
        }
    });
}
Also used : XSModelGroupDecl(com.sun.xml.xsom.XSModelGroupDecl) BigInteger(java.math.BigInteger) XSElementDecl(com.sun.xml.xsom.XSElementDecl) XSWildcard(com.sun.xml.xsom.XSWildcard) XSTermVisitor(com.sun.xml.xsom.visitor.XSTermVisitor) XSModelGroup(com.sun.xml.xsom.XSModelGroup)

Example 20 with XSElementDecl

use of com.sun.xml.xsom.XSElementDecl in project narayana by jbosstm.

the class NBFSchemaParser method parse.

public boolean parse(String fname) {
    boolean rc = false;
    try {
        flds.clear();
        XSOMParser parser = new XSOMParser();
        parser.parse(fname);
        XSSchemaSet xsSchema = parser.getResult();
        XSSchema schema = xsSchema.getSchema(1);
        File file = new File(fname);
        XSElementDecl element = schema.getElementDecl(file.getName().replace(".xsd", ""));
        if (element != null) {
            log.debug("element is " + element.getName());
            bufferName = element.getName();
            XSType xtype = element.getType();
            if (xtype.isComplexType()) {
                findElementType(xtype.asComplexType());
                rc = true;
            }
        }
    } catch (Exception e) {
        log.error("parse " + fname + " failed with " + e.getMessage(), e);
    }
    return rc;
}
Also used : XSType(com.sun.xml.xsom.XSType) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) XSElementDecl(com.sun.xml.xsom.XSElementDecl) File(java.io.File) XSSchema(com.sun.xml.xsom.XSSchema)

Aggregations

XSElementDecl (com.sun.xml.xsom.XSElementDecl)48 XSModelGroupDecl (com.sun.xml.xsom.XSModelGroupDecl)18 XSComplexType (com.sun.xml.xsom.XSComplexType)16 XSModelGroup (com.sun.xml.xsom.XSModelGroup)16 XSSchema (com.sun.xml.xsom.XSSchema)12 XSSimpleType (com.sun.xml.xsom.XSSimpleType)11 XSAttributeDecl (com.sun.xml.xsom.XSAttributeDecl)10 XSType (com.sun.xml.xsom.XSType)10 XSWildcard (com.sun.xml.xsom.XSWildcard)10 XSTermVisitor (com.sun.xml.xsom.visitor.XSTermVisitor)10 XSAttGroupDecl (com.sun.xml.xsom.XSAttGroupDecl)9 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)8 XSParticle (com.sun.xml.xsom.XSParticle)7 XSTerm (com.sun.xml.xsom.XSTerm)7 ArrayList (java.util.ArrayList)7 BigInteger (java.math.BigInteger)6 QName (javax.xml.namespace.QName)6 XSContentType (com.sun.xml.xsom.XSContentType)4 Iterator (java.util.Iterator)4 CClassInfo (com.sun.tools.xjc.model.CClassInfo)3