📄 xmlschemabuilder.java
字号:
/* XMLSchemaBuilder.java -- Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.xml.validation.xmlschema;import java.util.LinkedHashSet;import java.util.List;import java.util.Set;import java.util.StringTokenizer;import javax.xml.XMLConstants;import javax.xml.namespace.QName;import org.relaxng.datatype.DatatypeException;import org.relaxng.datatype.DatatypeLibrary;import org.relaxng.datatype.helpers.DatatypeLibraryLoader;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import gnu.xml.validation.datatype.Annotation;import gnu.xml.validation.datatype.AtomicSimpleType;import gnu.xml.validation.datatype.ListSimpleType;import gnu.xml.validation.datatype.SimpleType;import gnu.xml.validation.datatype.Type;import gnu.xml.validation.datatype.UnionSimpleType;/** * Parses an XML Schema DOM tree, constructing a compiled internal * representation. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */class XMLSchemaBuilder{ XMLSchema schema; final DatatypeLibrary typeLibrary; XMLSchemaBuilder() { final String ns = XMLConstants.W3C_XML_SCHEMA_NS_URI; typeLibrary = new DatatypeLibraryLoader().createDatatypeLibrary(ns); } void parseSchema(Node node) throws DatatypeException { String uri = node.getNamespaceURI(); String name = node.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && node.getNodeType() == Node.ELEMENT_NODE) { if ("schema".equals(name)) { NamedNodeMap attrs = node.getAttributes(); String targetNamespace = getAttribute(attrs, "targetNamespace"); String version = getAttribute(attrs, "version"); String fd = getAttribute(attrs, "finalDefault"); int finalDefault = parseFullDerivationSet(fd); String bd = getAttribute(attrs, "blockDefault"); int blockDefault = parseBlockSet(bd); String afd = getAttribute(attrs, "attributeFormDefault"); boolean attributeFormQualified = "qualified".equals(afd); String efd = getAttribute(attrs, "elementFormDefault"); boolean elementFormQualified = "qualified".equals(efd); schema = new XMLSchema(targetNamespace, version, finalDefault, blockDefault, attributeFormQualified, elementFormQualified); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { parseTopLevelElement(child); } return; } } // TODO throw schema exception } void parseTopLevelElement(Node node) throws DatatypeException { String uri = node.getNamespaceURI(); String name = node.getLocalName(); if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) && node.getNodeType() == Node.ELEMENT_NODE) { if ("element".equals(name)) { ElementDeclaration ed = (ElementDeclaration) parseElement(node, null); schema.elementDeclarations.put(ed.name, ed); // TODO } else if ("attribute".equals(name)) { AttributeDeclaration ad = (AttributeDeclaration) parseAttribute(node, true); schema.attributeDeclarations.put(ad.name, ad); // TODO } else if ("type".equals(name)) { // TODO } else if ("group".equals(name)) { // TODO } else if ("attributeGroup".equals(name)) { // TODO } else if ("notation".equals(name)) { // TODO } else if ("identityConstraint".equals(name)) { // TODO } } // TODO throw schema exception } Object parseAttribute(Node node, boolean scope) throws DatatypeException { NamedNodeMap attrs = node.getAttributes(); String def = getAttribute(attrs, "default"); String fixed = getAttribute(attrs, "fixed"); int constraintType = AttributeDeclaration.NONE; String constraintValue = null; if (def != null) { constraintType = AttributeDeclaration.DEFAULT; constraintValue = def; } else if (fixed != null) { constraintType = AttributeDeclaration.FIXED; constraintValue = fixed; } // TODO form = (qualified | unqualified) String attrName = getAttribute(attrs, "name"); String attrNamespace = getAttribute(attrs, "targetNamespace"); String ref = getAttribute(attrs, "ref"); String use = getAttribute(attrs, "use"); String type = getAttribute(attrs, "type"); SimpleType datatype = (type == null) ? null : parseSimpleType(asQName(type, node)); Annotation annotation = null; 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)) { annotation = parseAnnotation(child); } else if ("simpleType".equals(name)) { datatype = parseSimpleType(child); } else { // TODO throw schema exception } } } if (scope) { return new AttributeDeclaration(scope, constraintType, constraintValue, new QName(attrNamespace, attrName), datatype, annotation); } else { boolean required = "required".equals(use); // TODO ref AttributeDeclaration decl = (ref == null) ? new AttributeDeclaration(scope, AttributeDeclaration.NONE, null, new QName(attrNamespace, attrName), datatype, annotation) : /*schema.getAttributeDeclaration(ref)*/ null; return new AttributeUse(required, constraintType, constraintValue, decl); } } int parseFullDerivationSet(String value) { int ret = XMLSchema.FINAL_NONE; if ("#all".equals(value)) { ret = XMLSchema.FINAL_ALL; } else { StringTokenizer st = new StringTokenizer(value, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("extension".equals(token)) { ret |= XMLSchema.FINAL_EXTENSION; } else if ("restriction".equals(token)) { ret |= XMLSchema.FINAL_RESTRICTION; } else if ("list".equals(token)) { ret |= XMLSchema.FINAL_LIST; } else if ("union".equals(token)) { ret |= XMLSchema.FINAL_UNION; } } } return ret; } int parseSimpleTypeDerivationSet(String value) { int ret = XMLSchema.FINAL_NONE; if ("#all".equals(value)) { ret = XMLSchema.FINAL_LIST | XMLSchema.FINAL_UNION | XMLSchema.FINAL_RESTRICTION; } else { StringTokenizer st = new StringTokenizer(value, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("list".equals(token)) { ret |= XMLSchema.FINAL_LIST; } else if ("union".equals(token)) { ret |= XMLSchema.FINAL_UNION; } else if ("restriction".equals(token)) { ret |= XMLSchema.FINAL_RESTRICTION; } } } return ret; } int parseComplexTypeDerivationSet(String value) { int ret = XMLSchema.FINAL_NONE; if ("#all".equals(value)) { ret = XMLSchema.FINAL_EXTENSION | XMLSchema.FINAL_RESTRICTION; } else { StringTokenizer st = new StringTokenizer(value, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("extension".equals(token)) { ret |= XMLSchema.FINAL_EXTENSION; } else if ("restriction".equals(token)) { ret |= XMLSchema.FINAL_RESTRICTION; } } } return ret; } int parseBlockSet(String value) { int ret = XMLSchema.BLOCK_NONE; if ("#all".equals(value)) { ret = XMLSchema.BLOCK_ALL; } else { StringTokenizer st = new StringTokenizer(value, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("extension".equals(token)) { ret |= XMLSchema.BLOCK_EXTENSION; } else if ("restriction".equals(token)) { ret |= XMLSchema.BLOCK_RESTRICTION; } else if ("substitution".equals(token)) { ret |= XMLSchema.BLOCK_SUBSTITUTION; } } } return ret; } int parseComplexTypeBlockSet(String value) { int ret = XMLSchema.BLOCK_NONE; if ("#all".equals(value)) { ret = XMLSchema.BLOCK_EXTENSION | XMLSchema.BLOCK_RESTRICTION; } else { StringTokenizer st = new StringTokenizer(value, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("extension".equals(token)) { ret |= XMLSchema.BLOCK_EXTENSION; } else if ("restriction".equals(token)) { ret |= XMLSchema.BLOCK_RESTRICTION; } } } return ret; } Object parseElement(Node node, ElementDeclaration parent) throws DatatypeException { NamedNodeMap attrs = node.getAttributes(); Integer minOccurs = null; Integer maxOccurs = null; Node parentNode = node.getParentNode(); boolean notTopLevel = !"schema".equals(parentNode.getLocalName()); if (notTopLevel) { String ref = getAttribute(attrs, "ref"); if (ref != null) { minOccurs = getOccurrence(getAttribute(attrs, "minOccurs")); maxOccurs = getOccurrence(getAttribute(attrs, "maxOccurs")); // TODO resolve top-level element declaration ElementDeclaration ad = null; return new Particle(minOccurs, maxOccurs, ad); } } String elementName = getAttribute(attrs, "name"); String elementNamespace = getAttribute(attrs, "targetNamespace"); String type = getAttribute(attrs, "type"); Type datatype = (type != null) ? parseSimpleType(asQName(type, node)) : null; int scope = (parent == null) ? XMLSchema.GLOBAL : XMLSchema.LOCAL; String def = getAttribute(attrs, "default"); String fixed = getAttribute(attrs, "fixed"); int constraintType = AttributeDeclaration.NONE; String constraintValue = null; if (def != null) { constraintType = AttributeDeclaration.DEFAULT; constraintValue = def; } else if (fixed != null) { constraintType = AttributeDeclaration.FIXED; constraintValue = fixed;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -