📄 xmlschemabuilder.java
字号:
} String sg = getAttribute(attrs, "substitutionGroup"); QName substitutionGroup = QName.valueOf(sg); String sgPrefix = substitutionGroup.getPrefix(); if (sgPrefix != null && !"".equals(sgPrefix)) { String sgName = substitutionGroup.getLocalPart(); String sgNamespace = node.lookupNamespaceURI(sgPrefix); substitutionGroup = new QName(sgNamespace, sgName); } String block = getAttribute(attrs, "block"); int substitutionGroupExclusions = (block == null) ? schema.blockDefault : parseBlockSet(block); String final_ = getAttribute(attrs, "final"); int disallowedSubstitutions = (final_ == null) ? schema.finalDefault : parseFullDerivationSet(final_); boolean nillable = "true".equals(getAttribute(attrs, "nillable")); boolean isAbstract = "true".equals(getAttribute(attrs, "abstract")); if (notTopLevel) { minOccurs = getOccurrence(getAttribute(attrs, "minOccurs")); maxOccurs = getOccurrence(getAttribute(attrs, "maxOccurs")); String form = getAttribute(attrs, "form"); if (form != null) { if ("qualified".equals(form)) { elementNamespace = schema.targetNamespace; } } else if (schema.elementFormQualified) { elementNamespace = schema.targetNamespace; } } ElementDeclaration ed = new ElementDeclaration(new QName(elementNamespace, elementName), datatype, scope, parent, constraintType, constraintValue, nillable, substitutionGroup, substitutionGroupExclusions, disallowedSubstitutions, isAbstract); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("annotation".equals(name)) { ed.annotation = parseAnnotation(child); } else if ("simpleType".equals(name) && datatype == null) { ed.datatype = parseSimpleType(child); } else if ("complexType".equals(name) && datatype == null) { ed.datatype = parseComplexType(child, ed); } else { // throw schema exception } } } if (notTopLevel) { return new Particle(minOccurs, maxOccurs, ed); } else { return ed; } } Integer getOccurrence(String value) { if (value == null) { return new Integer(1); } else if ("unbounded".equals(value)) { return null; } else { return new Integer(value); } } SimpleType parseSimpleType(QName typeName) throws DatatypeException { SimpleType type = (SimpleType) schema.types.get(typeName); if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(typeName.getNamespaceURI())) return null; String localName = typeName.getLocalPart(); return (SimpleType) typeLibrary.createDatatype(localName); } SimpleType parseSimpleType(Node simpleType) throws DatatypeException { NamedNodeMap attrs = simpleType.getAttributes(); String typeFinal = getAttribute(attrs, "final"); if (typeFinal == null) { Node schema = simpleType.getParentNode(); while (schema != null && !"schema".equals(schema.getLocalName())) { schema = schema.getParentNode(); } if (schema != null) { NamedNodeMap schemaAttrs = schema.getAttributes(); typeFinal = getAttribute(schemaAttrs, "finalDefault"); } } int typeFinality = parseSimpleTypeDerivationSet(typeFinal); QName typeName = asQName(getAttribute(attrs, "name"), simpleType); int variety = 0; Set facets = new LinkedHashSet(); int fundamentalFacets = 0; // TODO SimpleType baseType = null; // TODO Annotation annotation = null; // TODO use DatatypeBuilder for (Node child = simpleType.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("annotation".equals(name)) { annotation = parseAnnotation(child); } else if ("restriction".equals(name)) { // TODO } else if ("list".equals(name)) { variety = SimpleType.LIST; // TODO } else if ("union".equals(name)) { variety = SimpleType.UNION; // TODO } } } return new SimpleType(typeName, variety, facets, fundamentalFacets, baseType, annotation); } Type parseComplexType(Node complexType, ElementDeclaration parent) throws DatatypeException { NamedNodeMap attrs = complexType.getAttributes(); QName typeName = asQName(getAttribute(attrs, "name"), complexType); boolean isAbstract = "true".equals(getAttribute(attrs, "abstract")); String block = getAttribute(attrs, "block"); int prohibitedSubstitutions = (block == null) ? schema.blockDefault : parseComplexTypeBlockSet(block); String final_ = getAttribute(attrs, "final"); int finality = (final_ == null) ? schema.finalDefault : parseComplexTypeDerivationSet(final_); ComplexType type = new ComplexType(typeName, isAbstract, prohibitedSubstitutions, finality); boolean mixed = "true".equals(getAttribute(attrs, "mixed")); for (Node child = complexType.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("simpleContent".equals(name)) { parseSimpleContent(child, type); } } } if (mixed) { type.contentType = XMLSchema.CONTENT_MIXED; } return type; } void parseSimpleContent(Node simpleContent, ComplexType type) throws DatatypeException { for (Node child = simpleContent.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("annotation".equals(name)) { type.annotations.add(parseAnnotation(child)); } else if ("restriction".equals(name)) { type.derivationMethod = XMLSchema.FINAL_RESTRICTION; parseRestriction(child, type); } else if ("extension".equals(name)) { type.derivationMethod = XMLSchema.FINAL_EXTENSION; parseExtension(child, type); } } } } void parseRestriction(Node restriction, ComplexType type) throws DatatypeException { NamedNodeMap attrs = restriction.getAttributes(); String base = getAttribute(attrs, "base"); QName baseType = asQName(base, restriction); SimpleType simpleType = null; for (Node child = restriction.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("annotation".equals(name)) { type.annotations.add(parseAnnotation(child)); } else if ("simpleType".equals(name)) { type.contentType = XMLSchema.CONTENT_SIMPLE; simpleType = parseSimpleType(child); } else if ("minExclusive".equals(name)) { } else if ("minInclusive".equals(name)) { } else if ("maxExclusive".equals(name)) { } else if ("maxInclusive".equals(name)) { } else if ("totalDigits".equals(name)) { } else if ("fractionDigits".equals(name)) { } else if ("length".equals(name)) { } else if ("minLength".equals(name)) { } else if ("maxLength".equals(name)) { } else if ("enumeration".equals(name)) { } else if ("whiteSpace".equals(name)) { } else if ("pattern".equals(name)) { } else if ("attribute".equals(name)) { AttributeUse use = (AttributeUse) parseAttribute(child, false); schema.attributeDeclarations.put(use.declaration.name, use.declaration); type.attributeUses.add(use); } else if ("attributeGroup".equals(name)) { NamedNodeMap agAttrs = child.getAttributes(); String ref = getAttribute(agAttrs, "ref"); QName ag = asQName(ref, child); type.attributeUses.add(ag); } else if ("anyAttribute".equals(name)) { type.attributeWildcard = parseAnyAttribute(child); } } } } void parseExtension(Node extension, ComplexType type) throws DatatypeException { NamedNodeMap attrs = extension.getAttributes(); String base = getAttribute(attrs, "base"); QName baseType = asQName(base, extension); for (Node child = extension.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("annotation".equals(name)) { type.annotations.add(parseAnnotation(child)); } else if ("attribute".equals(name)) { AttributeUse use = (AttributeUse) parseAttribute(child, false); schema.attributeDeclarations.put(use.declaration.name, use.declaration); type.attributeUses.add(use); } else if ("attributeGroup".equals(name)) { NamedNodeMap agAttrs = child.getAttributes(); String ref = getAttribute(agAttrs, "ref"); QName ag = asQName(ref, child); type.attributeUses.add(ag); } else if ("anyAttribute".equals(name)) { type.attributeWildcard = parseAnyAttribute(child); } } } } AnyAttribute parseAnyAttribute(Node node) { NamedNodeMap attrs = node.getAttributes(); String namespace = getAttribute(attrs, "namespace"); String pc = getAttribute(attrs, "processContents"); int processContents = AnyAttribute.STRICT; if ("lax".equals(pc)) { processContents = AnyAttribute.LAX; } else if ("skip".equals(pc)) { processContents = AnyAttribute.SKIP; } AnyAttribute ret = new AnyAttribute(namespace, processContents); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { String uri = child.getNamespaceURI(); String name = child.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && child.getNodeType() == Node.ELEMENT_NODE) { if ("annotation".equals(name)) { ret.annotation = parseAnnotation(child); } } } return ret; } Annotation parseAnnotation(Node node) { // TODO return null; } private static String getAttribute(NamedNodeMap attrs, String name) { Node attr = attrs.getNamedItem(name); return (attr == null) ? null : attr.getNodeValue(); } private static QName asQName(String text, Node resolver) { QName name = QName.valueOf(text); String prefix = name.getPrefix(); if (prefix != null && prefix.length() > 0) { String uri = resolver.lookupNamespaceURI(prefix); name = new QName(uri, name.getLocalPart()); } return name; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -