📄 schemaparser.java
字号:
if (schemaAll != null) {
onChildElements(schemaAll, elementFactory);
}
}
private void onChildElements(Element element, DatatypeElementFactory fact) {
Iterator iter = element.elementIterator(XSD_ELEMENT);
while (iter.hasNext()) {
Element xsdElement = (Element) iter.next();
onDatatypeElement(xsdElement, fact);
}
}
/**
* processes an XML Schema <attribute> tag
*
* @param xsdElement
* DOCUMENT ME!
* @param elementFactory
* DOCUMENT ME!
* @param xsdAttribute
* DOCUMENT ME!
*/
private void onDatatypeAttribute(Element xsdElement,
DatatypeElementFactory elementFactory, Element xsdAttribute) {
String name = xsdAttribute.attributeValue("name");
QName qname = getQName(name);
XSDatatype dataType = dataTypeForXsdAttribute(xsdAttribute);
if (dataType != null) {
// register the XSDatatype for the given Attribute
elementFactory.setAttributeXSDatatype(qname, dataType);
} else {
String type = xsdAttribute.attributeValue("type");
System.out.println("Warning: Couldn't find XSDatatype for type: "
+ type + " attribute: " + name);
}
}
/**
* processes an XML Schema <attribute> tag
*
* @param xsdAttribute
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws InvalidSchemaException
* DOCUMENT ME!
*/
private XSDatatype dataTypeForXsdAttribute(Element xsdAttribute) {
String type = xsdAttribute.attributeValue("type");
XSDatatype dataType = null;
if (type != null) {
dataType = getTypeByName(type);
} else {
// must parse the <simpleType> element
Element xsdSimpleType = xsdAttribute.element(XSD_SIMPLETYPE);
if (xsdSimpleType == null) {
String name = xsdAttribute.attributeValue("name");
String msg = "The attribute: " + name
+ " has no type attribute and does not contain a "
+ "<simpleType/> element";
throw new InvalidSchemaException(msg);
}
dataType = loadXSDatatypeFromSimpleType(xsdSimpleType);
}
return dataType;
}
/**
* processes an named XML Schema <simpleTypegt; tag
*
* @param schemaSimpleType
* DOCUMENT ME!
*/
private void onNamedSchemaSimpleType(Element schemaSimpleType) {
Attribute nameAttr = schemaSimpleType.attribute("name");
if (nameAttr == null) {
return;
}
String name = nameAttr.getText();
QName qname = getQName(name);
XSDatatype datatype = loadXSDatatypeFromSimpleType(schemaSimpleType);
namedTypeResolver.registerSimpleType(qname, datatype);
}
/**
* Loads a XSDatatype object from a <simpleType> attribute schema
* element
*
* @param xsdSimpleType
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private XSDatatype loadXSDatatypeFromSimpleType(Element xsdSimpleType) {
Element xsdRestriction = xsdSimpleType.element(XSD_RESTRICTION);
if (xsdRestriction != null) {
String base = xsdRestriction.attributeValue("base");
if (base != null) {
XSDatatype baseType = getTypeByName(base);
if (baseType == null) {
onSchemaError("Invalid base type: " + base
+ " when trying to build restriction: "
+ xsdRestriction);
} else {
return deriveSimpleType(baseType, xsdRestriction);
}
} else {
// simpleType and base are mutually exclusive and you
// must have one within a <restriction> tag
Element xsdSubType = xsdSimpleType.element(XSD_SIMPLETYPE);
if (xsdSubType == null) {
String msg = "The simpleType element: " + xsdSimpleType
+ " must contain a base attribute or simpleType"
+ " element";
onSchemaError(msg);
} else {
return loadXSDatatypeFromSimpleType(xsdSubType);
}
}
} else {
onSchemaError("No <restriction>. Could not create XSDatatype for"
+ " simpleType: " + xsdSimpleType);
}
return null;
}
/**
* Derives a new type from a base type and a set of restrictions
*
* @param baseType
* DOCUMENT ME!
* @param xsdRestriction
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private XSDatatype deriveSimpleType(XSDatatype baseType,
Element xsdRestriction) {
TypeIncubator incubator = new TypeIncubator(baseType);
ValidationContext context = null;
try {
for (Iterator iter = xsdRestriction.elementIterator(); iter
.hasNext();) {
Element element = (Element) iter.next();
String name = element.getName();
String value = element.attributeValue("value");
boolean fixed = AttributeHelper.booleanValue(element, "fixed");
// add facet
incubator.addFacet(name, value, fixed, context);
}
// derive a new type by those facets
String newTypeName = null;
return incubator.derive("", newTypeName);
} catch (DatatypeException e) {
onSchemaError("Invalid restriction: " + e.getMessage()
+ " when trying to build restriction: " + xsdRestriction);
return null;
}
}
/**
* DOCUMENT ME!
*
* @param name
* The name of the element
*
* @return the <code>DatatypeElementFactory</code> for the given element
* QName, creating one if it does not already exist
*/
private DatatypeElementFactory getDatatypeElementFactory(QName name) {
DatatypeElementFactory factory = documentFactory
.getElementFactory(name);
if (factory == null) {
factory = new DatatypeElementFactory(name);
name.setDocumentFactory(factory);
}
return factory;
}
private XSDatatype getTypeByName(String type) {
XSDatatype dataType = (XSDatatype) dataTypeCache.get(type);
if (dataType == null) {
// first check to see if it is a built-in type
// maybe a prefix is being used
int idx = type.indexOf(':');
if (idx >= 0) {
String localName = type.substring(idx + 1);
try {
dataType = DatatypeFactory.getTypeByName(localName);
} catch (DatatypeException e) {
}
}
if (dataType == null) {
try {
dataType = DatatypeFactory.getTypeByName(type);
} catch (DatatypeException e) {
}
}
if (dataType == null) {
// it's no built-in type, maybe it's a type we defined ourself
QName typeQName = getQName(type);
dataType = (XSDatatype) namedTypeResolver.simpleTypeMap
.get(typeQName);
}
if (dataType != null) {
// store in cache for later
dataTypeCache.put(type, dataType);
}
}
return dataType;
}
private QName getQName(String name) {
if (targetNamespace == null) {
return documentFactory.createQName(name);
} else {
return documentFactory.createQName(name, targetNamespace);
}
}
/**
* Called when there is a problem with the schema and the builder cannot
* handle the XML Schema Data Types correctly
*
* @param message
* DOCUMENT ME!
*
* @throws InvalidSchemaException
* DOCUMENT ME!
*/
private void onSchemaError(String message) {
// Some users may wish to disable exception throwing
// and instead use some kind of listener for errors and continue
// System.out.println( "WARNING: " + message );
throw new InvalidSchemaException(message);
}
}
/*
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain copyright statements and
* notices. Redistributions must also contain a copy of this document.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name "DOM4J" must not be used to endorse or promote products derived
* from this Software without prior written permission of MetaStuff, Ltd. For
* written permission, please contact dom4j-info@metastuff.com.
*
* 4. Products derived from this Software may not be called "DOM4J" nor may
* "DOM4J" appear in their names without prior written permission of MetaStuff,
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
*
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
*
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -