// --endElement methods SAX1 and SAX2 public void endElement(String name) throws SAXException { int idx = name.indexOf(':'); String prefix = (idx >= 0) ? name.substring(0, idx) : ""; String namespaceURI = _context.getNamespaceURI(prefix); endElement(namespaceURI, getLocalPart(name), name); _context = _context.getParent(); }
// --Namespace related (SAX2 Events) public void startPrefixMapping(String prefix, String uri) throws SAXException { AnyNode temp = new AnyNode(AnyNode.NAMESPACE, null, prefix, uri, null); _namespaces.push(temp); if (_processNamespace) { _context = _context.createNamespaces(); _processNamespace = true; } _context.addNamespace(prefix, uri); }
/** Implementation of {@link org.xml.sax.DocumentHandler#startElement} */ public void startElement(String name, AttributeList atts) throws SAXException { _character = false; String qName; String value; AnyNode tempNode = null; // Namespace handling code to be moved once we integrate // the new event API ///////////////// NAMESPACE HANDLING///////////////////// _context = _context.createNamespaces(); String prefix = ""; String namespaceURI = null; int idx = name.indexOf(':'); if (idx >= 0) { prefix = name.substring(0, idx); } namespaceURI = _context.getNamespaceURI(prefix); // --Overhead here since we process attributes twice for (int i = 0; i < atts.getLength(); ++i) { qName = atts.getName(i); value = atts.getValue(i); String nsPrefix = null; if (qName.startsWith(XMLNS_PREFIX)) { // handles namespace declaration // Extract the prefix if any nsPrefix = (qName.equals(XMLNS_PREFIX)) ? null : qName.substring(XMLNS_PREFIX_LENGTH); tempNode = new AnyNode(AnyNode.NAMESPACE, getLocalPart(qName), nsPrefix, value, null); _context.addNamespace(nsPrefix, value); _namespaces.push(tempNode); if (prefix.equals(nsPrefix)) namespaceURI = value; } } //////////////////////// END OF NAMESPACE HANDLING/////////////// createNodeElement(namespaceURI, getLocalPart(name), name); while (!_namespaces.empty()) { tempNode = (AnyNode) _namespaces.pop(); _node.addNamespace(tempNode); } // process attributes for (int i = 0; i < atts.getLength(); ++i) { qName = atts.getName(i); value = atts.getValue(i); // Namespace handling already done if (!qName.startsWith(XMLNS_PREFIX)) { tempNode = new AnyNode(AnyNode.ATTRIBUTE, getLocalPart(qName), null, null, value); _node.addAttribute(tempNode); } } tempNode = null; }
/** * Returns the ModeGroup of associated with the given name * @return the ModelGroup of associated with the given name, or * null if no ModelGroup with the given name was found. **/ public ModelGroup getModelGroup(String name) { String ns = null; if (name == null) { String err = NULL_ARGUMENT + "getModelGroup: "; err += " 'name' can not be null"; throw new IllegalArgumentException(err); } int idx = name.indexOf(':'); if (idx >= 0) { String nsPrefix = name.substring(0,idx); name = name.substring(idx + 1); ns = (String) _namespaces.getNamespaceURI(nsPrefix); if (ns == null) { String err = "getModelGroup: "; err += "Namespace prefix not recognized '"+nsPrefix+"'"; throw new IllegalArgumentException(err); } } if ((ns==null) || (ns.equals(_targetNamespace)) ) return (ModelGroup)_groups.get(name); else { Schema schema = getImportedSchema(ns); if (schema!=null) { return schema.getModelGroup(name); } } return null; } //--getModelGroup
private void createNodeElement(String namespaceURI, String localName, String qName) { String prefix = null; // retrieves the prefix if any if (namespaceURI != null) { prefix = _context.getNamespacePrefix(namespaceURI); } else if (qName != null) { if ((qName.length() != 0) && (qName.indexOf(':') != -1)) prefix = qName.substring(0, qName.indexOf(':')); } String name = null; // -- if namespace processing is disabled then the localName might be null, in that case // -- we use the localpart of the QName if (localName != null && localName.length() > 0) name = localName; else name = getLocalPart(qName); // creates the starting ELEMENT node // or a default ELEMENT node if ((_nodeStack.empty()) && (_startingNode == null)) { _startingNode = new AnyNode(AnyNode.ELEMENT, name, prefix, namespaceURI, null); _node = _startingNode; } else { _node = new AnyNode(AnyNode.ELEMENT, name, prefix, namespaceURI, null); // push the node in the stack _nodeStack.push(_node); } }
/** * Returns the SimpleType associated with the given name, * or null if no such SimpleType exists. * * @param name the name of the SimpleType. The name may * be a QName (contain a namespace prefix). * @return the SimpleType associated with the given name, * or null if no such SimpleType exists. **/ public SimpleType getSimpleType(String name) { //-- name must not be null if (name == null) { String err = NULL_ARGUMENT + "getSimpleType: "; err += "'name' cannot be null."; throw new IllegalArgumentException(err); } //-- Handle namespace resolution? String nsPrefix = ""; String ns = null; int colon = name.indexOf(':'); if (colon >= 0) { nsPrefix = name.substring(0,colon); name = name.substring(colon + 1); ns = (String) _namespaces.getNamespaceURI(nsPrefix); if (ns == null) { String err = "getSimpleType: "; err += "Namespace prefix not recognised '"+nsPrefix+"'"; err += "for simpleType:"+name; throw new IllegalArgumentException(err); } } else { ns = (String) _namespaces.getNamespaceURI(nsPrefix); } //--if at this point, there is no namespace //--then we assume it is the targetNamespace if (ns == null) ns = _targetNamespace; return getSimpleType(name, ns); } //-- getSimpleType
/** * Adds the given attribute group definition to this Schema * definition. * * @param attrGroup the AttributeGroupDecl to add * @exception SchemaException if an AttributeGroupDecl * already exisits with the same name **/ public void addAttributeGroup(AttributeGroupDecl attrGroup) throws SchemaException { if (attrGroup == null) return; String name = attrGroup.getName(); //-- handle namespace prefix, if necessary int idx = name.indexOf(':'); if (idx >= 0) { String nsPrefix = name.substring(0,idx); name = name.substring(idx + 1); String ns = (String) _namespaces.getNamespaceURI(nsPrefix); if (ns == null) { String err = "addAttributeGroup: "; err += "Namespace prefix not recognized '"+nsPrefix+"'"; throw new IllegalArgumentException(err); } if (!ns.equals(_targetNamespace)) { String err = "AttributeGroup has different namespace " + "than this Schema definition."; throw new IllegalArgumentException(err); } } if (attrGroup.getSchema() != this) { String err = "invalid attempt to add an AttributeGroup which "; err += "belongs to a different Schema; " + name; throw new SchemaException(err); } Object obj = _attributeGroups.get(name); if (obj == attrGroup) return; if (obj != null) { String err = "Error attempting to add an AttributeGroup to this " + "Schema definition, an AttributeGroup already exists with " + "the given name: "; throw new SchemaException(err + name); } _attributeGroups.put(name, attrGroup); } //-- addAttributeGroup
/** * Returns the ComplexType of associated with the given name * @return the ComplexType of associated with the given name, or * null if no ComplexType with the given name was found. **/ public ComplexType getComplexType(String name) { //-- Null? if (name == null) { String err = NULL_ARGUMENT + "getComplexType: "; err += "'name' cannot be null."; throw new IllegalArgumentException(err); } //-- Namespace prefix? String canonicalName = name; String nsprefix = ""; String ns = _targetNamespace; int colon = name.indexOf(':'); if (colon != -1) { canonicalName = name.substring(colon + 1); nsprefix = name.substring(0,colon); ns = (String) _namespaces.getNamespaceURI(nsprefix); if (ns == null) { String err = "getComplexType: "; err += "Namespace prefix not recognized '"+name+"'"; throw new IllegalArgumentException(err); } } //-- Get GetComplexType object if ((ns==null) || (ns.equals(_targetNamespace)) ) return (ComplexType)_complexTypes.get(canonicalName); else { Schema schema = getImportedSchema(ns); if (schema!=null) { return schema.getComplexType(canonicalName); } } return null; } //-- getComplexType
/** * Returns the namespace associated with the given prefix. * * @return the namespace associated with the given prefix, * or null if no associated namespace exists. */ public final String getNamespace(String prefix) { if (prefix == null) prefix = ""; return (String)_namespaces.getNamespaceURI(prefix); } //-- getNamespace
/** * Removes the namespace from the set of namespace declarations for * this Schema definition. * * @param prefix the namespace prefix of the namespace to remove. */ public boolean removeNamespace(String prefix) { if (prefix == null) prefix = ""; return _namespaces.removeNamespace(prefix); } //-- removeNamespace
/** Implementation of {@link org.xml.sax.ContentHandler#startElement} */ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AnyNode tempNode; // --SAX2 Parser has not processed the namespaces so we need to do it. if (_processNamespace) { // Namespace handling code to be moved once we integrate // the new event API ///////////////// NAMESPACE HANDLING///////////////////// _context = _context.createNamespaces(); String prefix = ""; int idx = qName.indexOf(':'); if (idx >= 0) { prefix = qName.substring(0, idx); } namespaceURI = _context.getNamespaceURI(prefix); // --Overhead here since we process attributes twice for (int i = 0; i < atts.getLength(); ++i) { String attrqName = atts.getQName(i); String value = atts.getValue(i); String nsPrefix = null; // handles namespace declaration if (attrqName.startsWith(XMLNS_PREFIX)) { // Extract the prefix if any nsPrefix = (attrqName.equals(XMLNS_PREFIX)) ? null : attrqName.substring(XMLNS_PREFIX_LENGTH); tempNode = new AnyNode(AnyNode.NAMESPACE, getLocalPart(attrqName), nsPrefix, value, null); _context.addNamespace(nsPrefix, value); _namespaces.push(tempNode); if (prefix.equals(nsPrefix)) namespaceURI = value; } } //////////////////////// END OF NAMESPACE HANDLING/////////////// } // create element createNodeElement(namespaceURI, localName, qName); // process attributes for (int i = 0; i < atts.getLength(); ++i) { String uri = atts.getURI(i); String attqName = atts.getQName(i); String value = atts.getValue(i); String prefix = null; // -- skip namespace declarations? (handled above) if (_processNamespace) if (attqName.startsWith(XMLNS_PREFIX)) continue; // --attribute namespace prefix? if ((attqName.length() != 0) && (attqName.indexOf(':') != -1)) prefix = attqName.substring(0, attqName.indexOf(':')); // --namespace not yet processed? if (_processNamespace) { // attribute namespace if (prefix != null) uri = _context.getNamespaceURI(prefix); } // --add attribute tempNode = new AnyNode(AnyNode.ATTRIBUTE, getLocalPart(attqName), prefix, uri, value); _node.addAttribute(tempNode); } // --empty the namespace stack and add // --the namespace nodes to the current node. while (!_namespaces.empty()) { tempNode = (AnyNode) _namespaces.pop(); _node.addNamespace(tempNode); } tempNode = null; }
/** * Returns the first simple or complex type which name equals TypeName */ public XMLType getType(String typeName) { //-- Null? if (typeName == null) { String err = NULL_ARGUMENT + "Schema#getType: "; err += "'name' cannot be null."; throw new IllegalArgumentException(err); } XMLType result = null; String localName = typeName; String prefix = ""; String ns = null; int colon = typeName.indexOf(':'); if (colon >= 0) { localName = typeName.substring(colon + 1); prefix = typeName.substring(0,colon); ns = (String) _namespaces.getNamespaceURI(prefix); if (ns == null) { String err = "Schema#getType: "; err += "Namespace prefix not recognised '"+typeName+"'"; throw new IllegalArgumentException(err); } } //-- use default namespace if necessary if (ns == null) { ns = (String)_namespaces.getNamespaceURI(prefix); } //--if at this point, there is no namespace //--then we assume it is the targetNamespace if (ns == null) ns = _targetNamespace; //1--support for anyType if (localName.equals(SchemaNames.ANYTYPE)) { //--if 'anyType' in the default schema namespace-->type is anyType if (ns.equals(DEFAULT_SCHEMA_NS)) { result = new AnyType(this); } }//--anyType IllegalArgumentException exception = null; //2--look for a simpleType if (result == null) { try { result= getSimpleType(localName, ns); } catch (IllegalArgumentException iox) { exception = iox; } } //3--look for a complexType if (result == null) { try { result = getComplexType(typeName); } catch (IllegalArgumentException iox) { exception = iox; } } if ((result == null) && (exception != null)) throw exception; return result; }
public void endPrefixMapping(String prefix) throws SAXException { _context.removeNamespace(prefix); }
/** * Returns the namespace prefix associated with the * given namespace. If more than one prefix has been * associated, the first one found will be returned. * * @return the namespace prefix associaed with the * given namespace. **/ protected String getNamespacePrefix(String namespace) { return _namespaces.getNamespacePrefix(namespace); }
/** * Returns the SimpleType associated with the given name * and namespace, or null if no such SimpleType exists. * * @param name the name of the simpleType. It is an error * if this name contains a prefix, it must be an NCName. * @param namespace the namespace URI of the simpleType. * @return the SimpleType, or null if no such SimpleType exists. **/ public SimpleType getSimpleType(String name, String namespace) { //-- name must not be null if (name == null) { String err = NULL_ARGUMENT + "getSimpleType: "; err += "'name' cannot be null."; throw new IllegalArgumentException(err); } //--Is the declaration in the default namespace (if any) boolean isDefaultNS = false; if (namespace == null) { namespace = (String)_namespaces.getNamespaceURI(""); isDefaultNS = true; } //-- Get SimpleType object SimpleType result = null; if ((namespace == null) || (isDefaultNS)) { //-- first check user-defined types result = (SimpleType)_simpleTypes.get(name); if (result != null) { //-- resolve deferred type if necessary if (result.getType() != result) { //-- can result.getType ever return null? //-- We can check, just in case. if (result.getType() != null) { result = (SimpleType)result.getType(); result.setParent(this); _simpleTypes.put(name, result); } } } //-- otherwise try built-in types else { result= simpleTypesFactory.getBuiltInType(name); //if we have a built-in type not declared in the good namespace -> Exception if ( (result != null) && (!_schemaNamespace.equals(namespace))) { String err = "getSimpleType: the simple type '"+name+ "' has not been declared in XML Schema namespace."; throw new IllegalArgumentException(err); } } } else if (namespace.equals(_schemaNamespace)) { result= simpleTypesFactory.getBuiltInType(name); if (result == null) { String err = "getSimpleType: the simple type '"+name+ "' is not a built-in type as defined in XML Schema specification."; throw new IllegalArgumentException(err); } } else if (namespace.equals(_targetNamespace)) { result = (SimpleType)_simpleTypes.get(name); if (result != null) { //-- resolve deferred type if necessary if (result.getType() != result) { //-- can result.getType ever return null? //-- We can check, just in case. if (result.getType() != null) { result = (SimpleType)result.getType(); result.setParent(this); _simpleTypes.put(name, result); } } } } else { Schema schema = getImportedSchema(namespace); if (schema != null) { result = schema.getSimpleType(name, namespace); } } //-- Result could be a deferredSimpleType => getType will resolve it if (result!=null) result= (SimpleType)result.getType(); return result; } //-- getSimpleType
/** * Returns True if the namespace is known to this schema * @param namespace the namespace URL * @return True if the namespace was declared in the schema */ public boolean isKnownNamespace(String namespaceURL) { return (_namespaces.getNamespacePrefix(namespaceURL) != null); }
/** * Adds to the namespaces declared in this Schema * @param namespaces the list of namespaces */ public void addNamespace(String prefix, String ns) { _namespaces.addNamespace(prefix, ns); } //-- setNamespaces