elena 2002/12/30 13:31:03 Modified: java/src/org/apache/xerces/dom CoreDocumentImpl.java DOMConfigurationImpl.java DOMNormalizer.java ElementImpl.java NamedNodeMapImpl.java PSVIDocumentImpl.java ParentNode.java java/src/org/apache/xerces/impl/xs XMLSchemaValidator.java java/src/org/apache/xerces/parsers DOMBuilderImpl.java Log: Implement XML 1.0 attribute value normalization. Expose normalized values and default content via DOM Tree. Add implementation of discard-default-content. Revision Changes Path 1.36 +2 -7 xml-xerces/java/src/org/apache/xerces/dom/CoreDocumentImpl.java Index: CoreDocumentImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/CoreDocumentImpl.java,v retrieving revision 1.35 retrieving revision 1.36 diff -u -r1.35 -r1.36 --- CoreDocumentImpl.java 11 Dec 2002 16:14:30 -0000 1.35 +++ CoreDocumentImpl.java 30 Dec 2002 21:31:02 -0000 1.36 @@ -975,12 +975,7 @@ fConfiguration.reset(); } - try { - domNormalizer.normalizeDocument(this, fConfiguration); - } - catch (RuntimeException e){ - // fatal error occured - } + domNormalizer.normalizeDocument(this, fConfiguration); isNormalized(true); } 1.2 +31 -18 xml-xerces/java/src/org/apache/xerces/dom/DOMConfigurationImpl.java Index: DOMConfigurationImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/DOMConfigurationImpl.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DOMConfigurationImpl.java 11 Dec 2002 16:14:30 -0000 1.1 +++ DOMConfigurationImpl.java 30 Dec 2002 21:31:02 -0000 1.2 @@ -125,7 +125,8 @@ protected static final String NORMALIZE_DATA = Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; - protected static final String PSVI = + /** sending psvi in the pipeline */ + protected static final String SEND_PSVI = Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; @@ -185,6 +186,7 @@ protected final static short SPLITCDATA = 0x1<<5; protected final static short COMMENTS = 0x1<<6; protected final static short VALIDATE = 0x1<<7; + protected final static short PSVI = 0x1<<8; // components @@ -249,7 +251,9 @@ XERCES_NAMESPACES, SCHEMA, DYNAMIC_VALIDATION, - NORMALIZE_DATA + NORMALIZE_DATA, + SEND_PSVI, + Constants.DOM_DISCARD_DEFAULT_CONTENT }; addRecognizedFeatures(recognizedFeatures); @@ -257,8 +261,10 @@ setFeature(XERCES_VALIDATION, false); setFeature(SCHEMA, false); setFeature(DYNAMIC_VALIDATION, false); - setFeature(NORMALIZE_DATA, true); + setFeature(NORMALIZE_DATA, false); setFeature(XERCES_NAMESPACES, true); + setFeature(Constants.DOM_DISCARD_DEFAULT_CONTENT, true); + setFeature(SEND_PSVI, true); // add default recognized properties final String[] recognizedProperties = { @@ -279,7 +285,6 @@ features |= NAMESPACES; features |= ENTITIES; features |= COMMENTS; - features |= DTNORMALIZATION; features |= CDATA; features |= DEFAULTS; features |= SPLITCDATA; @@ -549,24 +554,23 @@ } else if (name.equals(Constants.DOM_DATATYPE_NORMALIZATION)) { - // REVISIT: datatype-normalization only takes effect if validation is on + setFeature(NORMALIZE_DATA, state); features = (short) (state ? features | DTNORMALIZATION : features & ~DTNORMALIZATION); } else if (name.equals(Constants.DOM_NAMESPACES)) { features = (short) (state ? features | NAMESPACES : features & ~NAMESPACES); - } else if (name.equals(Constants.DOM_CDATA_SECTIONS)) { features = (short) (state ? features | CDATA : features & ~CDATA); - } else if (name.equals(Constants.DOM_ENTITIES)) { features = (short) (state ? features | ENTITIES : features & ~ENTITIES); } else if (name.equals(Constants.DOM_DISCARD_DEFAULT_CONTENT)) { + setFeature (Constants.DOM_DISCARD_DEFAULT_CONTENT, state); features = (short) (state ? features | DEFAULTS : features & ~DEFAULTS); } @@ -603,11 +607,11 @@ } } - else if (name.equals(Constants.DOM_PSVI)){ - try { - setFeature(PSVI, state); - } - catch (XMLConfigurationException e){ + else if (name.equals(SEND_PSVI) ){ + // REVISIT: turning augmentation of PSVI is not support, + // because in this case we won't be able to retrieve element + // default value. + if (!state) { // false is not supported String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, @@ -616,6 +620,9 @@ throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); } } + else if (name.equals(Constants.DOM_PSVI)){ + features = (short) (state ? features | PSVI : features & ~PSVI); + } else { String msg = DOMMessageFormatter.formatMessage( @@ -641,8 +648,7 @@ new Object[] { name }); throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); } - } - + } else if (name.equals(Constants.DOM_ENTITY_RESOLVER)) { if (value instanceof DOMEntityResolver) { try { @@ -819,6 +825,13 @@ || name.equals(Constants.DOM_VALIDATE_IF_SCHEMA)) { return Boolean.FALSE; } + else if (name.equals(SEND_PSVI)) { + return Boolean.TRUE; + + } + else if (name.equals(Constants.DOM_PSVI)) { + return (features & PSVI) != 0 ? Boolean.TRUE : Boolean.FALSE; + } else if ( name.equals(Constants.DOM_NAMESPACE_DECLARATIONS) || name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)) { @@ -883,9 +896,9 @@ } return false; } - else if ( - name.equals(Constants.DOM_NAMESPACE_DECLARATIONS) - || name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)) { + else if ( name.equals(Constants.DOM_NAMESPACE_DECLARATIONS) + || name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT) + || name.equals(SEND_PSVI)) { return (state == Boolean.TRUE) ? true : false; } 1.21 +232 -141 xml-xerces/java/src/org/apache/xerces/dom/DOMNormalizer.java Index: DOMNormalizer.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/DOMNormalizer.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- DOMNormalizer.java 16 Dec 2002 01:26:19 -0000 1.20 +++ DOMNormalizer.java 30 Dec 2002 21:31:02 -0000 1.21 @@ -58,45 +58,35 @@ package org.apache.xerces.dom; -import org.apache.xerces.dom3.DOMErrorHandler; -import org.apache.xerces.dom3.DOMError; +import java.util.Vector; +import org.apache.xerces.dom3.DOMError; +import org.apache.xerces.dom3.DOMErrorHandler; import org.apache.xerces.impl.Constants; import org.apache.xerces.impl.RevalidationHandler; import org.apache.xerces.util.AugmentationsImpl; import org.apache.xerces.util.NamespaceSupport; import org.apache.xerces.util.SymbolTable; import org.apache.xerces.util.XMLSymbols; -import org.apache.xerces.parsers.AbstractXMLDocumentParser; - - import org.apache.xerces.xni.Augmentations; import org.apache.xerces.xni.NamespaceContext; import org.apache.xerces.xni.QName; -import org.apache.xerces.xni.XMLDocumentHandler; import org.apache.xerces.xni.XMLAttributes; -import org.apache.xerces.xni.XMLResourceIdentifier; +import org.apache.xerces.xni.XMLDocumentHandler; import org.apache.xerces.xni.XMLLocator; +import org.apache.xerces.xni.XMLResourceIdentifier; import org.apache.xerces.xni.XMLString; import org.apache.xerces.xni.XNIException; +import org.apache.xerces.xni.grammars.XMLGrammarDescription; import org.apache.xerces.xni.parser.XMLComponent; -import org.apache.xerces.xni.parser.XMLComponentManager; import org.apache.xerces.xni.parser.XMLConfigurationException; -import org.apache.xerces.xni.parser.XMLErrorHandler; import org.apache.xerces.xni.parser.XMLDocumentSource; -import org.apache.xerces.xni.psvi.ElementPSVI; import org.apache.xerces.xni.psvi.AttributePSVI; - -import org.apache.xerces.xni.grammars.XMLGrammarDescription; -import org.apache.xerces.xni.grammars.XMLGrammarPool; -import org.apache.xerces.xni.grammars.Grammar; - -import org.w3c.dom.Node; -import org.w3c.dom.Element; +import org.apache.xerces.xni.psvi.ElementPSVI; import org.w3c.dom.Attr; -import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; import org.w3c.dom.Text; -import java.util.Vector; /** * This class adds implementation for normalizeDocument method. @@ -123,19 +113,14 @@ * @author Elena Litani, IBM * @version $Id$ */ -public class DOMNormalizer implements XMLGrammarPool, XMLDocumentHandler { +public class DOMNormalizer implements XMLDocumentHandler { // - // REVISIT: - // 1. Send all appropriate calls for entity reference content. - // 2. If datatype-normalization feature is on: - // a) replace values of text nodes with normalized value: - // need to pass to documentHandler augmentations that will be filled in - // during validation process. - // b) add element default content: retrieve from augementations (PSVI Element schemaDefault) - // c) replace values of attributes: the augmentations for attributes have the values. - // + // REVISIT: + // 1. For element content we need to perform "2.11 End-of-Line Handling", + // see normalizeAttributeValue, characterData() + // 2. Send all appropriate calls for entity reference content (?). // // constants @@ -144,6 +129,8 @@ protected final static boolean DEBUG_ND = false; /** Debug namespace fix up algorithm*/ protected final static boolean DEBUG = false; + /** Debug document handler events */ + protected final static boolean DEBUG_EVENTS = false; /** prefix added by namespace fixup algorithm should follow a pattern "NS" + index*/ protected final static String PREFIX = "NS"; @@ -191,6 +178,9 @@ /** for setting the PSVI */ protected Node fCurrentNode = null; private QName fAttrQName = new QName(); + + // attribute value normalization + final XMLString fNormalizedValue = new XMLString(new char[16], 0, 0); // // Constructor @@ -225,40 +215,50 @@ fConfiguration.setFeature(DOMConfigurationImpl.SCHEMA, true); // report fatal error on DOM Level 1 nodes fNamespaceValidation = true; + // check if we need to fill in PSVI - try { - fPSVI = fConfiguration.getFeature(DOMConfigurationImpl.PSVI); - } - catch (XMLConfigurationException e) { - fPSVI = false; - } + fPSVI = ((fConfiguration.features & DOMConfigurationImpl.PSVI) !=0)?true:false; // REVISIT: pass namespace context to the XML Schema validator ((XMLComponent) fValidationHandler).reset(fConfiguration); } - fErrorHandler = (DOMErrorHandler)fConfiguration.getParameter("error-handler"); + fErrorHandler = (DOMErrorHandler) fConfiguration.getParameter("error-handler"); if (fValidationHandler != null) { fValidationHandler.setBaseURI(fDocument.fDocumentURI); fValidationHandler.setDocumentHandler(this); fValidationHandler.startDocument(null, fDocument.encoding, fNamespaceContext, null); } + try { + Node kid, next; + for (kid = fDocument.getFirstChild(); kid != null; kid = next) { + next = kid.getNextSibling(); + kid = normalizeNode(kid); + if (kid != null) { // don't advance + next = kid; + } + } - Node kid, next; - for (kid = fDocument.getFirstChild(); kid != null; kid = next) { - next = kid.getNextSibling(); - kid = normalizeNode(kid); - if (kid != null) { // don't advance - next = kid; + // release resources + if (fValidationHandler != null) { + fValidationHandler.endDocument(null); + // REVISIT: only validation against XML Schema occurs + CoreDOMImplementationImpl.singleton.releaseValidator( + XMLGrammarDescription.XML_SCHEMA); + fValidationHandler = null; } } - - // release resources - if (fValidationHandler != null) { - fValidationHandler.endDocument(null); - // REVISIT: only validation against XML Schema occurs - CoreDOMImplementationImpl.singleton.releaseValidator(XMLGrammarDescription.XML_SCHEMA); - fValidationHandler = null; + catch (RuntimeException e) { + // fatal error occured + modifyDOMError( + "Runtime exception: " + e.getMessage(), + DOMError.SEVERITY_FATAL_ERROR, + null); + + this.fErrorHandler.handleError(fDOMError); + if (true) { + e.printStackTrace(); + } } } @@ -326,6 +326,8 @@ Attr attr = (Attr)attributes.item(i); removeDefault(attr, attributes); attr.normalize(); + // XML 1.0 attribute value normalization + normalizeAttributeValue(attr.getValue(), attr); } } } @@ -335,7 +337,6 @@ // or rely on the PSVI information. fAttrProxy.setAttributes(attributes, fDocument, elem); updateQName(elem, fQName); // updates global qname - // // set error node in the dom error wrapper // so if error occurs we can report an error node fConfiguration.fErrorHandlerWrapper.fCurrentNode = node; @@ -588,6 +589,8 @@ } } } else { + // XML 1.0 Attribute value normalization + value = normalizeAttributeValue(value, attr); prefix = attr.getPrefix(); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix); @@ -695,14 +698,13 @@ // clone content of the attributes attributes.cloneMap(fAttributeList); for (int i = 0; i < fAttributeList.size(); i++) { - Attr attr = (Attr) fAttributeList.elementAt(i); - // normalize attribute value - attr.normalize(); if (DEBUG) { System.out.println("==>[ns-fixup] process attribute: "+attr.getNodeName()); } + // normalize attribute value + attr.normalize(); value = attr.getValue(); name = attr.getNodeName(); uri = attr.getNamespaceURI(); @@ -734,6 +736,8 @@ if (removeDefault(attr, attributes)) { continue; } + // XML 1.0 Attribute value normalization + value = normalizeAttributeValue(value, attr); uri = fSymbolTable.addSymbol(uri); @@ -786,7 +790,8 @@ // data int colon = name.indexOf(':'); - + // XML 1.0 Attribute value normalization + value = normalizeAttributeValue(value, attr); if (colon > -1) { // It is an error if document has DOM L1 nodes. boolean continueProcess = true; @@ -884,6 +889,60 @@ qname.rawname = fSymbolTable.addSymbol(node.getNodeName()); qname.uri = (namespace != null)?fSymbolTable.addSymbol(namespace):null; } + + /** + * Performs partial XML 1.0 attribute value normalization and replaces + * attribute value if the value is changed after the normalization. + * DOM defines that normalizeDocument acts as if the document was going + * through a save and load cycle, given that serializer will not escape + * any '\n' or '\r' characters on load those will be normalized. + * Thus during normalize document we need to do the following: + * - perform "2.11 End-of-Line Handling" + * - replace #xD, #xA, #x9 with #x20 (white space). + * Note: This alg. won't attempt to resolve entity references or character entity + * references, since '&' will be escaped during serialization and during loading + * this won't be recognized as entity reference, i.e. attribute value "&foo;" will + * be serialized as "&foo;" and thus after loading will be "&foo;" again. + * @param value current attribute value + * @param attr current attribute + * @return String the value (could be original if normalization did not change + * the string) + */ + final String normalizeAttributeValue(String value, Attr attr) { + if (!attr.getSpecified()){ + // specified attributes should already have a normalized form + // since those were added by validator + return value; + } + int end = value.length(); + // ensure capacity + if (fNormalizedValue.ch.length < end) { + fNormalizedValue.ch = new char[end]; + } + fNormalizedValue.length = 0; + boolean normalized = false; + for (int i = 0; i < end; i++) { + char c = value.charAt(i); + if (c==0x0009 || c==0x000A) { + fNormalizedValue.ch[fNormalizedValue.length++] = ' '; + normalized = true; + } + else if(c==0x000D){ + normalized = true; + fNormalizedValue.ch[fNormalizedValue.length++] = ' '; + int next = i+1; + if (next < end && value.charAt(next)==0x000A) i=next; // skip following xA + } + else { + fNormalizedValue.ch[fNormalizedValue.length++] = c; + } + } + if (normalized){ + value = fNormalizedValue.toString(); + attr.setValue(value); + } + return value; + } protected final class XMLAttributesProxy implements XMLAttributes { @@ -914,18 +973,37 @@ } - public int addAttribute(QName attrQName, String attrType, String attrValue){ - Attr attr = fDocument.createAttributeNS(attrQName.uri, - attrQName.rawname, - attrQName.localpart); - attr.setValue(attrValue); - if (fAttributes == null) { - fAttributes = (AttributeMap)fElement.getAttributes(); + /** + * This method adds default declarations + * @see org.apache.xerces.xni.XMLAttributes#addAttribute(QName, String, String) + */ + public int addAttribute(QName qname, String attrType, String attrValue) { + int index = fElement.getXercesAttribute(qname.uri, qname.localpart); + // add defaults to the tree + if (index < 0) { + // the default attribute was removed by a user and needed to + // be added back + AttrImpl attr = (AttrImpl) + ((CoreDocumentImpl) fElement.getOwnerDocument()).createAttributeNS( + qname.uri, + qname.rawname, + qname.localpart); + // REVISIT: the following should also update ID table + index = fElement.setXercesAttributeNode(attr); + attr.setNodeValue(attrValue); + fAugmentations.insertElementAt(new AugmentationsImpl(), index); + attr.setSpecified(false); + } + else { + // default attribute is in the tree + // we don't need to do anything since prefix was already fixed + // at the namespace fixup time and value must be same value, otherwise + // attribute will be treated as specified and we will never reach + // this method. + } - int index = fElement.setXercesAttributeNode(attr); - fAugmentations.insertElementAt(new AugmentationsImpl(), index); return index; - } + } public void removeAllAttributes(){ @@ -1008,12 +1086,22 @@ public void setValue(int attrIndex, String attrValue){ - // REVISIT: implement + // REVISIT: is this desired behaviour? + // The values are updated in the case datatype-normalization is turned on + // in this case we need to make sure that specified attributes stay specified + + if (fAttributes != null){ + AttrImpl attr = (AttrImpl)fAttributes.getItem(attrIndex); + boolean specified = attr.getSpecified(); + attr.setValue(attrValue); + attr.setSpecified(specified); + + } } public String getValue(int index){ - return fAttributes.item(index).getNodeValue(); + return (fAttributes !=null)?fAttributes.item(index).getNodeValue():""; } @@ -1024,7 +1112,7 @@ } - public String getValue(String uri, String localName){ + public String getValue(String uri, String localName){ if (fAttributes != null) { Node node = fAttributes.getNamedItemNS(uri, localName); return(node != null)? node.getNodeValue():null; @@ -1046,7 +1134,6 @@ public void setSpecified(int attrIndex, boolean specified){ - // REVISIT: this will be called after add attributes is called AttrImpl attr = (AttrImpl)fAttributes.getItem(attrIndex); attr.setSpecified(specified); } @@ -1070,41 +1157,6 @@ } } - - // - // XML GrammarPool methods - // - - // REVISIT: should DOMNormalizer implement grammar pool? - public Grammar[] retrieveInitialGrammarSet(String grammarType){ - // REVISIT: should take into account grammarType - return null; - - } - - - public void cacheGrammars(String grammarType, Grammar[] grammars){ - // REVISIT: implement - } - - - public Grammar retrieveGrammar(XMLGrammarDescription desc){ - return null; - } - - public void lockPool(){ - // REVISIT: implement - } - - - public void unlockPool(){ - // REVISIT: implement - } - - public void clear(){ - // REVISIT: implement - } - // // XMLDocumentHandler methods // @@ -1222,26 +1274,45 @@ * @exception XNIException * Thrown by handler to signal an error. */ - public void startElement(QName element, XMLAttributes attributes, Augmentations augs) - throws XNIException{ - if (fPSVI) { - Element currentElement = (Element)fCurrentNode; - int attrCount = attributes.getLength(); - for (int i = 0; i < attrCount; i++) { - attributes.getName(i, fAttrQName); - Attr attr = null; - //REVISIT: schema elements must be namespace aware. - // DTD re-validation is not implemented yet.. - attr = currentElement.getAttributeNodeNS(fAttrQName.uri,fAttrQName.localpart); - - AttributePSVI attrPSVI = (AttributePSVI)attributes.getAugmentations(i).getItem(Constants.ATTRIBUTE_PSVI); - if (attrPSVI !=null) { - ((PSVIAttrNSImpl)attr).setPSVI(attrPSVI); - } - } - } - - } + public void startElement(QName element, XMLAttributes attributes, Augmentations augs) + throws XNIException { + //REVISIT: schema elements must be namespace aware. + // DTD re-validation is not implemented yet.. + Element currentElement = (Element) fCurrentNode; + int attrCount = attributes.getLength(); + if (DEBUG_EVENTS) { + System.out.println("==>startElement: " +element+ + " attrs.length="+attrCount); + } + + for (int i = 0; i < attrCount; i++) { + attributes.getName(i, fAttrQName); + Attr attr = null; + + attr = currentElement.getAttributeNodeNS(fAttrQName.uri, fAttrQName.localpart); + AttributePSVI attrPSVI = + (AttributePSVI) attributes.getAugmentations(i).getItem(Constants.ATTRIBUTE_PSVI); + + if (attrPSVI != null) { + if (fPSVI) { + ((PSVIAttrNSImpl) attr).setPSVI(attrPSVI); + } + if ((fConfiguration.features & DOMConfigurationImpl.DTNORMALIZATION) != 0) { + // datatype-normalization + // NOTE: The specified value MUST be set after we set + // the node value because that turns the "specified" + // flag to "true" which may overwrite a "false" + // value from the attribute list. + boolean specified = attr.getSpecified(); + attr.setValue(attrPSVI.getSchemaNormalizedValue()); + if (!specified) { + ((AttrImpl) attr).setSpecified(specified); + } + } + } + } + } + /** * An empty element. @@ -1253,11 +1324,15 @@ * @exception XNIException * Thrown by handler to signal an error. */ - public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) - throws XNIException{ - startElement(element, attributes, augs); + public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) + throws XNIException { + if (DEBUG_EVENTS) { + System.out.println("==>emptyElement: " +element); + } + + startElement(element, attributes, augs); endElement(element, augs); - } + } /** * This method notifies the start of a general entity. @@ -1356,20 +1431,36 @@ * @exception XNIException * Thrown by handler to signal an error. */ - public void endElement(QName element, Augmentations augs) throws XNIException{ - if (fPSVI) { + public void endElement(QName element, Augmentations augs) throws XNIException { + if (DEBUG_EVENTS) { + System.out.println("==>endElement: " + element); + } - ElementPSVI elementPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); - if (elementPSVI != null) { - - ((PSVIElementNSImpl)fCurrentNode).setPSVI(elementPSVI); - if (elementPSVI.getIsSchemaSpecified()) { - // default value from XML Schema. - fCurrentNode.setNodeValue(elementPSVI.getSchemaDefault()); - } - } - } - } + ElementPSVI elementPSVI = (ElementPSVI) augs.getItem(Constants.ELEMENT_PSVI); + if (elementPSVI != null) { + ElementImpl elementNode = (ElementImpl) fCurrentNode; + if (fPSVI) { + ((PSVIElementNSImpl) fCurrentNode).setPSVI(elementPSVI); + } + if ((fConfiguration.features & DOMConfigurationImpl.DEFAULTS) == 0) { + String normalizedValue = elementPSVI.getSchemaNormalizedValue(); + if ((fConfiguration.features & DOMConfigurationImpl.DTNORMALIZATION) != 0) { + elementNode.setTextContent(normalizedValue); + } + else { + // NOTE: this is a hack: it is possible that DOM had an empty element + // and validator sent default value using characters(), which we don't + // implement. Thus, here we attempt to add the default value. + String text = elementNode.getTextContent(); + if (text.length() == 0) { + // default content could be provided + // REVISIT: should setTextConent(null) be allowed? + elementNode.setTextContent(normalizedValue); + } + } + } + } + } /** * The start of a CDATA section. 1.55 +20 -1 xml-xerces/java/src/org/apache/xerces/dom/ElementImpl.java Index: ElementImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/ElementImpl.java,v retrieving revision 1.54 retrieving revision 1.55 diff -u -r1.54 -r1.55 --- ElementImpl.java 11 Dec 2002 14:19:13 -0000 1.54 +++ ElementImpl.java 30 Dec 2002 21:31:02 -0000 1.55 @@ -786,12 +786,31 @@ */ protected int setXercesAttributeNode (Attr attr){ + if (needsSyncData()) { + synchronizeData(); + } + if (attributes == null) { attributes = new AttributeMap(this, null); } return attributes.addItem(attr); } + + /** + * NON-DOM: get inded of an attribute + */ + protected int getXercesAttribute(String namespaceURI, String localName){ + + if (needsSyncData()) { + synchronizeData(); + } + if (attributes == null) { + return -1; + } + return attributes.getNamedItemIndex(namespaceURI, localName); + + } /** * Introduced in DOM Level 2. 1.34 +5 -1 xml-xerces/java/src/org/apache/xerces/dom/NamedNodeMapImpl.java Index: NamedNodeMapImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/NamedNodeMapImpl.java,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- NamedNodeMapImpl.java 19 Nov 2002 01:41:36 -0000 1.33 +++ NamedNodeMapImpl.java 30 Dec 2002 21:31:02 -0000 1.34 @@ -595,6 +595,10 @@ return list; } + + protected int getNamedItemIndex(String namespaceURI, String localName) { + return findNamePoint(namespaceURI, localName); + } /** * NON-DOM remove all elements from this map 1.3 +2 -8 xml-xerces/java/src/org/apache/xerces/dom/PSVIDocumentImpl.java Index: PSVIDocumentImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/PSVIDocumentImpl.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PSVIDocumentImpl.java 11 Dec 2002 16:14:30 -0000 1.2 +++ PSVIDocumentImpl.java 30 Dec 2002 21:31:02 -0000 1.3 @@ -71,12 +71,7 @@ * @version $Id$ */ public class PSVIDocumentImpl extends DocumentImpl { - - /** Recognized features. */ - private static final String[] RECOGNIZED_FEATURES = { - DOMConfigurationImpl.PSVI - }; - + /** * Create a document. @@ -132,7 +127,6 @@ */ public DOMConfiguration getConfig(){ super.getConfig(); - fConfiguration.addRecognizedFeatures(RECOGNIZED_FEATURES); return fConfiguration; } 1.38 +4 -2 xml-xerces/java/src/org/apache/xerces/dom/ParentNode.java Index: ParentNode.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/ParentNode.java,v retrieving revision 1.37 retrieving revision 1.38 diff -u -r1.37 -r1.38 --- ParentNode.java 19 Nov 2002 01:41:36 -0000 1.37 +++ ParentNode.java 30 Dec 2002 21:31:02 -0000 1.38 @@ -693,7 +693,9 @@ removeChild(child); } // create a Text node to hold the given content - appendChild(ownerDocument().createTextNode(textContent)); + if (textContent != null){ + appendChild(ownerDocument().createTextNode(textContent)); + } } // 1.126 +15 -5 xml-xerces/java/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java Index: XMLSchemaValidator.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java,v retrieving revision 1.125 retrieving revision 1.126 diff -u -r1.125 -r1.126 --- XMLSchemaValidator.java 16 Dec 2002 01:26:20 -0000 1.125 +++ XMLSchemaValidator.java 30 Dec 2002 21:31:03 -0000 1.126 @@ -314,6 +314,8 @@ // to indicate whether we are in the scope of entity reference or CData protected boolean fEntityRef = false; protected boolean fInCDATA = false; + // DOM Level 3: discard default attributes ( + protected boolean fDiscardDefaults = false; // properties @@ -707,7 +709,7 @@ // call handlers if (fDocumentHandler != null) { - if (!fSchemaElementDefault || fDefaultValue == null) { + if (fDiscardDefaults || !fSchemaElementDefault || fDefaultValue == null) { fDocumentHandler.emptyElement(element, attributes, modifiedAugs); } else { fDocumentHandler.startElement(element, attributes, modifiedAugs); @@ -1309,6 +1311,14 @@ fSchemaType = null; } + try { + fDiscardDefaults = componentManager.getFeature(Constants.DOM_DISCARD_DEFAULT_CONTENT); + } + catch (XMLConfigurationException e){ + fDiscardDefaults = false; + } + + fEntityResolver = (XMLEntityResolver)componentManager.getProperty(ENTITY_MANAGER); fSchemaLoader.setEntityResolver(fEntityResolver); @@ -1993,7 +2003,7 @@ processAttributes(element, attributes, attrGrp); // add default attributes - if (attrGrp != null) { + if (attrGrp != null && !fDiscardDefaults) { addDefaultAttributes(element, attributes, attrGrp); } @@ -2659,7 +2669,6 @@ reportSchemaError("cvc-complex-type.3.1", new Object[]{element.rawname, fTempQName.rawname, attrValue}); } } - if (fAugPSVI) { // PSVI: attribute declaration attrPSVI.fDeclaration = currDecl; @@ -2744,10 +2753,11 @@ attrs.setSchemaId(attrIndex, schemaId); } + if (fAugPSVI) { + // PSVI: attribute is "schema" specified Augmentations augs = attributes.getAugmentations(attrIndex); - AttributePSVImpl attrPSVI = new AttributePSVImpl(); augs.putItem(Constants.ATTRIBUTE_PSVI, attrPSVI); 1.25 +6 -14 xml-xerces/java/src/org/apache/xerces/parsers/DOMBuilderImpl.java Index: DOMBuilderImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/DOMBuilderImpl.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- DOMBuilderImpl.java 11 Dec 2002 16:14:31 -0000 1.24 +++ DOMBuilderImpl.java 30 Dec 2002 21:31:03 -0000 1.25 @@ -188,7 +188,8 @@ Constants.DOM_INFOSET, Constants.DOM_NAMESPACE_DECLARATIONS, Constants.DOM_SUPPORTED_MEDIATYPES_ONLY, - Constants.DOM_CERTIFIED + Constants.DOM_CERTIFIED, + Constants.DOM_DISCARD_DEFAULT_CONTENT }; fConfiguration.addRecognizedFeatures(domRecognizedFeatures); @@ -203,6 +204,7 @@ fConfiguration.setFeature(Constants.DOM_INFOSET, false); fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, true); fConfiguration.setFeature(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY, false); + fConfiguration.setFeature(Constants.DOM_DISCARD_DEFAULT_CONTENT, true); // REVISIT: by default Xerces assumes that input is certified. // default is different from the one specified in the DOM spec @@ -210,15 +212,6 @@ // Xerces datatype-normalization feature is on by default fConfiguration.setFeature( NORMALIZE_DATA, false ); - - - // REVISIT: we need to be able to validate against XMLSchema ONLY if - // schemaType equal XML Schema namespace, even if - // DOCTYPE is present. - // This is similar to JAXP schemaLanguage property. - - - } // <init>(XMLParserConfiguration) /** @@ -693,9 +686,8 @@ DOMErrorImpl error = new DOMErrorImpl(); error.fException = e; error.fMessage = e.getMessage(); - error.fSeverity = error.SEVERITY_ERROR; + error.fSeverity = error.SEVERITY_FATAL_ERROR; fErrorHandler.getErrorHandler().handleError(error); - } if (DEBUG) { e.printStackTrace(); @@ -721,7 +713,7 @@ DOMErrorImpl error = new DOMErrorImpl(); error.fException = e; error.fMessage = e.getMessage(); - error.fSeverity = error.SEVERITY_ERROR; + error.fSeverity = error.SEVERITY_FATAL_ERROR; fErrorHandler.getErrorHandler().handleError(error); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]