You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2008/08/07 21:37:02 UTC

svn commit: r683688 - in /xerces/java/trunk/src/org/apache/xerces: impl/msg/ jaxp/validation/

Author: mrglavas
Date: Thu Aug  7 12:37:02 2008
New Revision: 683688

URL: http://svn.apache.org/viewvc?rev=683688&view=rev
Log:
JIRA Issue #1314:
https://issues.apache.org/jira/browse/XERCESJ-1314

For JAXP 1.4, adding support for StAXSource/StAXResult as an input/output 
to the JAXP Validator. From the perspective of the Validator component
this is just another ValidatorHelper implementation.

Added:
    xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java   (with props)
    xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java   (with props)
    xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java   (with props)
    xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java   (with props)
Modified:
    xerces/java/trunk/src/org/apache/xerces/impl/msg/JAXPValidationMessages.properties
    xerces/java/trunk/src/org/apache/xerces/jaxp/validation/ValidatorImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/impl/msg/JAXPValidationMessages.properties
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/impl/msg/JAXPValidationMessages.properties?rev=683688&r1=683687&r2=683688&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/msg/JAXPValidationMessages.properties (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/msg/JAXPValidationMessages.properties Thu Aug  7 12:37:02 2008
@@ -36,6 +36,7 @@
 SourceParameterNull = Source parameter cannot be null.
 SourceNotAccepted = Source parameter of type ''{0}'' is not accepted by this validator.
 SourceResultMismatch = Source parameter of type ''{0}'' is not compatible with result parameter of type ''{1}''.
+StAXIllegalInitialState = Expecting the initial state to be start document or start element.
 
 # TypeInfoProvider error messages
 TypeInfoProviderIllegalState = A TypeInfoProvider cannot be queried outside of a startElement callback.

Added: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java?rev=683688&view=auto
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java (added)
+++ xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java Thu Aug  7 12:37:02 2008
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xerces.jaxp.validation;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.DTD;
+import javax.xml.stream.events.EndDocument;
+import javax.xml.stream.events.EntityReference;
+import javax.xml.stream.events.ProcessingInstruction;
+import javax.xml.stream.events.StartDocument;
+import javax.xml.transform.stax.StAXResult;
+
+import org.apache.xerces.xni.XMLDocumentHandler;
+
+/**
+ * <p>An extension to XMLDocumentHandler for building StAX structures.</p>
+ * 
+ * @author Michael Glavassevich, IBM
+ * @version $Id$
+ */
+interface StAXDocumentHandler extends XMLDocumentHandler {
+    
+    /**
+     * <p>Sets the <code>StAXResult</code> object which will be receiving the output.</p>
+     */
+    public void setStAXResult(StAXResult result);
+    
+    public void startDocument(XMLStreamReader reader) throws XMLStreamException;
+    public void endDocument(XMLStreamReader reader) throws XMLStreamException;
+    public void comment(XMLStreamReader reader) throws XMLStreamException;
+    public void processingInstruction(XMLStreamReader reader) throws XMLStreamException;
+    public void entityReference(XMLStreamReader reader) throws XMLStreamException;
+    
+    public void startDocument(StartDocument event) throws XMLStreamException;
+    public void endDocument(EndDocument event) throws XMLStreamException;
+    public void doctypeDecl(DTD event) throws XMLStreamException;
+    public void characters(Characters event) throws XMLStreamException;
+    public void cdata(Characters event) throws XMLStreamException;
+    public void comment(Comment event) throws XMLStreamException;
+    public void processingInstruction(ProcessingInstruction event) throws XMLStreamException;
+    public void entityReference(EntityReference event) throws XMLStreamException;
+    
+    public void setIgnoringCharacters(boolean ignore);
+
+} // StAXDocumentHandler

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXDocumentHandler.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java?rev=683688&view=auto
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java (added)
+++ xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java Thu Aug  7 12:37:02 2008
@@ -0,0 +1,376 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xerces.jaxp.validation;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import javax.xml.stream.XMLEventFactory;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.DTD;
+import javax.xml.stream.events.EndDocument;
+import javax.xml.stream.events.EntityReference;
+import javax.xml.stream.events.ProcessingInstruction;
+import javax.xml.stream.events.StartDocument;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.stax.StAXResult;
+
+import org.apache.xerces.util.JAXPNamespaceContextWrapper;
+import org.apache.xerces.xni.Augmentations;
+import org.apache.xerces.xni.NamespaceContext;
+import org.apache.xerces.xni.QName;
+import org.apache.xerces.xni.XMLAttributes;
+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.parser.XMLDocumentSource;
+
+/**
+ * <p>StAX event result builder.</p>
+ * 
+ * @author Michael Glavassevich, IBM
+ * @version $Id$
+ */
+final class StAXEventResultBuilder implements StAXDocumentHandler {
+    
+    //
+    // Data
+    //
+    
+    private XMLEventWriter fEventWriter;
+    XMLEventFactory fEventFactory;
+    private StAXValidatorHelper fStAXValidatorHelper;
+    private JAXPNamespaceContextWrapper fNamespaceContext;
+    private boolean fIgnoreChars;
+    private boolean fInCDATA;
+    final QName fAttrName = new QName();
+
+    public StAXEventResultBuilder(StAXValidatorHelper helper, JAXPNamespaceContextWrapper context) {
+        fStAXValidatorHelper = helper;
+        fNamespaceContext = context;
+        fEventFactory = XMLEventFactory.newInstance();
+    }
+    
+    /*
+     * StAXDocumentHandler methods
+     */
+
+    public void setStAXResult(StAXResult result) {
+        fIgnoreChars = false;
+        fInCDATA = false;
+        fEventWriter = (result != null) ? result.getXMLEventWriter() : null;
+    }
+
+    public void startDocument(XMLStreamReader reader) throws XMLStreamException {
+        String version = reader.getVersion();
+        String encoding = reader.getCharacterEncodingScheme();
+        boolean standalone = reader.standaloneSet();
+        fEventWriter.add(fEventFactory.createStartDocument(encoding != null ? encoding : "UTF-8",
+                version != null ? version : "1.0", standalone));
+    }
+
+    public void endDocument(XMLStreamReader reader) throws XMLStreamException {
+        fEventWriter.add(fEventFactory.createEndDocument());
+        fEventWriter.flush();
+    }
+
+    public void comment(XMLStreamReader reader) throws XMLStreamException {
+        fEventWriter.add(fEventFactory.createComment(reader.getText()));
+    }
+
+    public void processingInstruction(XMLStreamReader reader)
+            throws XMLStreamException {
+        String data = reader.getPIData();
+        fEventWriter.add(fEventFactory.createProcessingInstruction(reader.getPITarget(), 
+                data != null ? data : ""));
+    }
+
+    public void entityReference(XMLStreamReader reader)
+            throws XMLStreamException {
+        String name = reader.getLocalName();
+        fEventWriter.add(fEventFactory.createEntityReference(name, 
+                fStAXValidatorHelper.getEntityDeclaration(name)));
+    }
+
+    public void startDocument(StartDocument event) throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void endDocument(EndDocument event) throws XMLStreamException {
+        fEventWriter.add(event);
+        fEventWriter.flush();
+    }
+
+    public void doctypeDecl(DTD event) throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void characters(Characters event) throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void cdata(Characters event) throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void comment(Comment event) throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void processingInstruction(ProcessingInstruction event)
+            throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void entityReference(EntityReference event)
+            throws XMLStreamException {
+        fEventWriter.add(event);
+    }
+
+    public void setIgnoringCharacters(boolean ignore) {
+        fIgnoreChars = ignore;
+    }
+    
+    /*
+     * XMLDocumentHandler methods
+     */
+
+    public void startDocument(XMLLocator locator, String encoding,
+            NamespaceContext namespaceContext, Augmentations augs)
+            throws XNIException {}
+
+    public void xmlDecl(String version, String encoding, String standalone,
+            Augmentations augs) throws XNIException {}
+
+    public void doctypeDecl(String rootElement, String publicId,
+            String systemId, Augmentations augs) throws XNIException {}
+
+    public void comment(XMLString text, Augmentations augs) throws XNIException {}
+
+    public void processingInstruction(String target, XMLString data,
+            Augmentations augs) throws XNIException {}
+
+    public void startElement(QName element, XMLAttributes attributes,
+            Augmentations augs) throws XNIException {
+        try {
+            int length = attributes.getLength();
+            if (length == 0) {
+                /** Avoid creating a new StartElement event object (if possible). */
+                XMLEvent start = fStAXValidatorHelper.getCurrentEvent();
+                if (start != null) {
+                    fEventWriter.add(start);
+                    return;
+                }
+            }
+            fEventWriter.add(fEventFactory.createStartElement(element.prefix, 
+                    element.uri != null ? element.uri : "", element.localpart, 
+                    getAttributeIterator(attributes, length), getNamespaceIterator(),
+                    fNamespaceContext.getNamespaceContext()));
+        }
+        catch (XMLStreamException e) {
+            throw new XNIException(e);
+        }
+    }
+
+    public void emptyElement(QName element, XMLAttributes attributes,
+            Augmentations augs) throws XNIException {
+        startElement(element, attributes, augs);
+        endElement(element, augs);
+    }
+
+    public void startGeneralEntity(String name,
+            XMLResourceIdentifier identifier, String encoding,
+            Augmentations augs) throws XNIException {}
+
+    public void textDecl(String version, String encoding, Augmentations augs)
+            throws XNIException {}
+
+    public void endGeneralEntity(String name, Augmentations augs)
+            throws XNIException {}
+
+    public void characters(XMLString text, Augmentations augs)
+            throws XNIException {
+        if (!fIgnoreChars) {
+            try {
+                if (!fInCDATA) {
+                    fEventWriter.add(fEventFactory.createCharacters(text.toString()));
+                }
+                else {
+                    fEventWriter.add(fEventFactory.createCData(text.toString()));
+                }
+            }
+            catch (XMLStreamException e) {
+                throw new XNIException(e);
+            }
+        }
+    }
+
+    public void ignorableWhitespace(XMLString text, Augmentations augs)
+            throws XNIException {
+        characters(text, augs);
+    }
+
+    public void endElement(QName element, Augmentations augs)
+            throws XNIException {
+        try {
+            /** Avoid creating a new EndElement event object (if possible). */
+            XMLEvent end = fStAXValidatorHelper.getCurrentEvent();
+            if (end != null) {
+                fEventWriter.add(end);
+            }
+            else {
+                fEventWriter.add(fEventFactory.createEndElement(element.prefix, 
+                    element.uri, element.localpart, getNamespaceIterator()));
+            }
+        }
+        catch (XMLStreamException e) {
+            throw new XNIException(e);
+        }
+    }
+
+    public void startCDATA(Augmentations augs) throws XNIException {
+        fInCDATA = true;
+    }
+
+    public void endCDATA(Augmentations augs) throws XNIException {
+        fInCDATA = false;
+    }
+
+    public void endDocument(Augmentations augs) throws XNIException {}
+
+    public void setDocumentSource(XMLDocumentSource source) {}
+
+    public XMLDocumentSource getDocumentSource() {
+        return null;
+    }
+    
+    /*
+     * Other methods.
+     */
+    
+    private Iterator getAttributeIterator(XMLAttributes attributes, int length) {
+        return (length > 0) ? new AttributeIterator(attributes, length) : EMPTY_COLLECTION_ITERATOR;
+    }
+    
+    private Iterator getNamespaceIterator() {
+        int length = fNamespaceContext.getDeclaredPrefixCount();
+        return (length > 0) ? new NamespaceIterator(length) : EMPTY_COLLECTION_ITERATOR;
+    }
+    
+    /**
+     * An iterator over XMLAttributes which returns Attribute event objects. 
+     */
+    final class AttributeIterator implements Iterator {
+
+        XMLAttributes fAttributes;
+        int fIndex;
+        int fEnd;
+        
+        AttributeIterator(XMLAttributes attributes, int length) {
+            fAttributes = attributes;
+            fIndex = 0;
+            fEnd = length;
+        }
+        
+        public boolean hasNext() {
+            if (fIndex < fEnd) {
+                return true;
+            }
+            fAttributes = null;
+            return false;
+        }
+        
+        public Object next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+            fAttributes.getName(fIndex, fAttrName);
+            return fEventFactory.createAttribute(fAttrName.prefix, 
+                    fAttrName.uri != null ? fAttrName.uri : "",
+                    fAttrName.localpart, fAttributes.getValue(fIndex++));
+        }
+        
+        public void remove() {
+            throw new UnsupportedOperationException();                   
+        }
+    }
+    
+    /**
+     * An iterator over the current context of a NamespaceContext 
+     * which returns Namespace event objects.
+     */
+    final class NamespaceIterator implements Iterator {
+        
+        javax.xml.namespace.NamespaceContext fNC;
+        int fIndex;
+        int fEnd;
+        
+        NamespaceIterator(int length) {
+            fNC = fNamespaceContext.getNamespaceContext();
+            fIndex = 0;
+            fEnd = length;
+        }
+        
+        public boolean hasNext() {
+            if (fIndex < fEnd) {
+                return true;
+            }
+            fNC = null;
+            return false;
+        }
+        
+        public Object next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+            String prefix = fNamespaceContext.getDeclaredPrefixAt(fIndex++);
+            String uri = fNC.getNamespaceURI(prefix);
+            if (prefix.length() == 0) {
+                return fEventFactory.createNamespace(uri != null ? uri : "");
+            }
+            else {
+                return fEventFactory.createNamespace(prefix, uri != null ? uri : "");
+            }
+        }
+        
+        public void remove() {
+            throw new UnsupportedOperationException(); 
+        }
+    }
+    
+    /**
+     * An iterator for an empty collection.
+     */
+    private static final Iterator EMPTY_COLLECTION_ITERATOR = new Iterator () {
+        public boolean hasNext() {
+            return false;
+        }
+        public Object next() {
+            throw new NoSuchElementException();
+        }
+        public void remove() {
+            throw new UnsupportedOperationException(); 
+        }  
+    };
+
+} // StAXEventResultBuilder

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXEventResultBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java?rev=683688&view=auto
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java (added)
+++ xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java Thu Aug  7 12:37:02 2008
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xerces.jaxp.validation;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.DTD;
+import javax.xml.stream.events.EndDocument;
+import javax.xml.stream.events.EntityReference;
+import javax.xml.stream.events.ProcessingInstruction;
+import javax.xml.stream.events.StartDocument;
+import javax.xml.transform.stax.StAXResult;
+
+import org.apache.xerces.util.JAXPNamespaceContextWrapper;
+import org.apache.xerces.xni.Augmentations;
+import org.apache.xerces.xni.NamespaceContext;
+import org.apache.xerces.xni.QName;
+import org.apache.xerces.xni.XMLAttributes;
+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.parser.XMLDocumentSource;
+
+/**
+ * <p>StAX stream result builder.</p>
+ * 
+ * @author Michael Glavassevich, IBM
+ * @version $Id$
+ */
+final class StAXStreamResultBuilder implements StAXDocumentHandler {
+    
+    //
+    // Data
+    //
+
+    private XMLStreamWriter fStreamWriter;
+    private JAXPNamespaceContextWrapper fNamespaceContext;
+    private boolean fIgnoreChars;
+    private boolean fInCDATA;
+    private final QName fAttrName = new QName();
+    
+    public StAXStreamResultBuilder(JAXPNamespaceContextWrapper context) {
+        fNamespaceContext = context;
+    }
+    
+    /*
+     * StAXDocumentHandler methods
+     */
+
+    public void setStAXResult(StAXResult result) {
+        fIgnoreChars = false;
+        fInCDATA = false;
+        fAttrName.clear();
+        fStreamWriter = (result != null) ? result.getXMLStreamWriter() : null;
+    }
+
+    public void startDocument(XMLStreamReader reader) throws XMLStreamException {
+        String version = reader.getVersion();
+        String encoding = reader.getCharacterEncodingScheme();
+        fStreamWriter.writeStartDocument(encoding != null ? encoding : "UTF-8",
+                version != null ? version : "1.0");
+    }
+
+    public void endDocument(XMLStreamReader reader) throws XMLStreamException {
+        fStreamWriter.writeEndDocument();
+        fStreamWriter.flush();
+    }
+
+    public void comment(XMLStreamReader reader) throws XMLStreamException {
+        fStreamWriter.writeComment(reader.getText());
+    }
+
+    public void processingInstruction(XMLStreamReader reader)
+            throws XMLStreamException {
+        String data = reader.getPIData();
+        if (data != null && data.length() > 0) {
+            fStreamWriter.writeProcessingInstruction(reader.getPITarget(), data);
+        }
+        else {
+            fStreamWriter.writeProcessingInstruction(reader.getPITarget());
+        }
+    }
+
+    public void entityReference(XMLStreamReader reader) throws XMLStreamException {
+        fStreamWriter.writeEntityRef(reader.getLocalName());
+    }
+
+    public void startDocument(StartDocument event) throws XMLStreamException {
+        String version = event.getVersion();
+        String encoding = event.getCharacterEncodingScheme();
+        fStreamWriter.writeStartDocument(encoding != null ? encoding : "UTF-8",
+                version != null ? version : "1.0");
+    }
+
+    public void endDocument(EndDocument event) throws XMLStreamException {
+        fStreamWriter.writeEndDocument();
+        fStreamWriter.flush();
+    }
+
+    public void doctypeDecl(DTD event) throws XMLStreamException {
+        fStreamWriter.writeDTD(event.getDocumentTypeDeclaration());
+    }
+
+    public void characters(Characters event) throws XMLStreamException {
+        fStreamWriter.writeCharacters(event.getData());
+    }
+
+    public void cdata(Characters event) throws XMLStreamException {
+        fStreamWriter.writeCData(event.getData());
+    }
+
+    public void comment(Comment event) throws XMLStreamException {
+        fStreamWriter.writeComment(event.getText());
+    }
+
+    public void processingInstruction(ProcessingInstruction event)
+            throws XMLStreamException {
+        String data = event.getData();
+        if (data != null && data.length() > 0) {
+            fStreamWriter.writeProcessingInstruction(event.getTarget(), data);
+        }
+        else {
+            fStreamWriter.writeProcessingInstruction(event.getTarget());
+        }
+    }
+
+    public void entityReference(EntityReference event) throws XMLStreamException {
+        fStreamWriter.writeEntityRef(event.getName());
+
+    }
+
+    public void setIgnoringCharacters(boolean ignore) {
+        fIgnoreChars = ignore;
+    }
+    
+    /*
+     * XMLDocumentHandler methods
+     */
+
+    public void startDocument(XMLLocator locator, String encoding,
+            NamespaceContext namespaceContext, Augmentations augs)
+            throws XNIException {}
+
+    public void xmlDecl(String version, String encoding, String standalone,
+            Augmentations augs) throws XNIException {}
+
+    public void doctypeDecl(String rootElement, String publicId,
+            String systemId, Augmentations augs) throws XNIException {}
+
+    public void comment(XMLString text, Augmentations augs) throws XNIException {}
+
+    public void processingInstruction(String target, XMLString data,
+            Augmentations augs) throws XNIException {}
+
+    public void startElement(QName element, XMLAttributes attributes,
+            Augmentations augs) throws XNIException {
+        try {
+            if (element.prefix.length() > 0) {
+                fStreamWriter.writeStartElement(element.prefix, 
+                        element.localpart, element.uri != null ? element.uri : "");
+            }
+            else if (element.uri != null){
+                fStreamWriter.writeStartElement(element.uri, element.localpart);
+            }
+            else {
+                fStreamWriter.writeStartElement(element.localpart);
+            }
+            int size = fNamespaceContext.getDeclaredPrefixCount();
+            final javax.xml.namespace.NamespaceContext nc = fNamespaceContext.getNamespaceContext();
+            for (int i = 0; i < size; ++i) {
+                String prefix = fNamespaceContext.getDeclaredPrefixAt(i);
+                String uri = nc.getNamespaceURI(prefix);
+                if (prefix.length() == 0) {
+                    fStreamWriter.writeDefaultNamespace(uri != null ? uri : "");
+                }
+                else {
+                    fStreamWriter.writeNamespace(prefix, uri != null ? uri : "");
+                }
+            }
+            size = attributes.getLength();
+            for (int i = 0; i < size; ++i) {
+                attributes.getName(i, fAttrName);
+                if (fAttrName.prefix.length() > 0) {
+                    fStreamWriter.writeAttribute(fAttrName.prefix, 
+                            fAttrName.uri != null ? fAttrName.uri : "", 
+                            fAttrName.localpart, attributes.getValue(i));
+                }
+                else if (fAttrName.uri != null) {
+                    fStreamWriter.writeAttribute(fAttrName.uri, 
+                            fAttrName.localpart, attributes.getValue(i));
+                }
+                else {
+                    fStreamWriter.writeAttribute(fAttrName.localpart, attributes.getValue(i));
+                }
+            }
+        }
+        catch (XMLStreamException e) {
+            throw new XNIException(e);
+        }
+    }
+
+    public void emptyElement(QName element, XMLAttributes attributes,
+            Augmentations augs) throws XNIException {
+        startElement(element, attributes, augs);
+        endElement(element, augs);
+    }
+
+    public void startGeneralEntity(String name,
+            XMLResourceIdentifier identifier, String encoding,
+            Augmentations augs) throws XNIException {}
+
+    public void textDecl(String version, String encoding, Augmentations augs)
+            throws XNIException {}
+
+    public void endGeneralEntity(String name, Augmentations augs)
+            throws XNIException {}
+
+    public void characters(XMLString text, Augmentations augs)
+        throws XNIException {
+        if (!fIgnoreChars) {
+            try {
+                if (!fInCDATA) {
+                    fStreamWriter.writeCharacters(text.ch, text.offset, text.length);
+                }
+                else {
+                    fStreamWriter.writeCData(text.toString());
+                }
+            }
+            catch (XMLStreamException e) {
+                throw new XNIException(e);
+            }
+        }
+    }
+
+    public void ignorableWhitespace(XMLString text, Augmentations augs)
+            throws XNIException {
+        characters(text, augs);
+    }
+
+    public void endElement(QName element, Augmentations augs)
+            throws XNIException {
+        try {
+            fStreamWriter.writeEndElement();
+        }
+        catch (XMLStreamException e) {
+            throw new XNIException(e);
+        }
+    }
+
+    public void startCDATA(Augmentations augs) throws XNIException {
+        fInCDATA = true;
+    }
+
+    public void endCDATA(Augmentations augs) throws XNIException {
+        fInCDATA = false;
+    }
+
+    public void endDocument(Augmentations augs) throws XNIException {}
+
+    public void setDocumentSource(XMLDocumentSource source) {}
+
+    public XMLDocumentSource getDocumentSource() {
+        return null;
+    }
+
+} // StAXStreamResultBuilder

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXStreamResultBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java?rev=683688&view=auto
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java (added)
+++ xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java Thu Aug  7 12:37:02 2008
@@ -0,0 +1,615 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xerces.jaxp.validation;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.DTD;
+import javax.xml.stream.events.EndDocument;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.EntityDeclaration;
+import javax.xml.stream.events.EntityReference;
+import javax.xml.stream.events.Namespace;
+import javax.xml.stream.events.ProcessingInstruction;
+import javax.xml.stream.events.StartDocument;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.stax.StAXResult;
+import javax.xml.transform.stax.StAXSource;
+
+import org.apache.xerces.impl.Constants;
+import org.apache.xerces.impl.XMLErrorReporter;
+import org.apache.xerces.impl.validation.EntityState;
+import org.apache.xerces.impl.validation.ValidationManager;
+import org.apache.xerces.impl.xs.XMLSchemaValidator;
+import org.apache.xerces.util.JAXPNamespaceContextWrapper;
+import org.apache.xerces.util.StAXLocationWrapper;
+import org.apache.xerces.util.SymbolTable;
+import org.apache.xerces.util.XMLAttributesImpl;
+import org.apache.xerces.util.XMLStringBuffer;
+import org.apache.xerces.util.XMLSymbols;
+import org.apache.xerces.xni.QName;
+import org.apache.xerces.xni.XMLString;
+import org.apache.xerces.xni.XNIException;
+import org.apache.xerces.xni.parser.XMLParseException;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>A validator helper for <code>StAXSource</code>s.</p>
+ * 
+ * @author Michael Glavassevich, IBM
+ * @version $Id$
+ */
+final class StAXValidatorHelper implements ValidatorHelper, EntityState {
+    
+    // property identifiers
+    
+    /** Property identifier: error reporter. */
+    private static final String ERROR_REPORTER =
+        Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
+    
+    /** Property identifier: XML Schema validator. */
+    private static final String SCHEMA_VALIDATOR =
+        Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
+    
+    /** Property identifier: symbol table. */
+    private static final String SYMBOL_TABLE =
+        Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
+    
+    /** Property identifier: validation manager. */
+    private static final String VALIDATION_MANAGER =
+        Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
+    
+    //
+    // Data
+    //
+    
+    /** Error reporter. */
+    private XMLErrorReporter fErrorReporter;
+    
+    /** Schema validator. **/
+    private XMLSchemaValidator fSchemaValidator;
+    
+    /** Symbol table **/
+    private SymbolTable fSymbolTable;
+    
+    /** Validation manager. **/
+    private ValidationManager fValidationManager;
+    
+    /** Component manager. **/
+    private XMLSchemaValidatorComponentManager fComponentManager;
+    
+    /** The namespace context of this document: stores namespaces in scope. **/
+    private JAXPNamespaceContextWrapper fNamespaceContext;
+    
+    /** XML Locator wrapper for StAX. **/
+    private final StAXLocationWrapper fStAXLocationWrapper = new StAXLocationWrapper();
+    
+    /** Map for tracking entity declarations. */
+    private HashMap fEntities = null;
+    
+    /** Validator helper for XMLStreamReaders. **/
+    private StreamHelper fStreamHelper;
+    
+    /** Validator helper for XMLEventReaders. **/
+    private EventHelper fEventHelper;
+    
+    /** StAX document handler. **/
+    private StAXDocumentHandler fStAXValidatorHandler;
+    
+    /** StAX stream result builder. **/
+    private StAXStreamResultBuilder fStAXStreamResultBuilder;
+    
+    /** StAX event result builder. **/
+    private StAXEventResultBuilder fStAXEventResultBuilder;
+    
+    /** Document depth. **/
+    private int fDepth = 0;
+    
+    /** Current event. **/
+    private XMLEvent fCurrentEvent = null;
+    
+    /** Fields for start element, end element and characters. **/
+    final QName fElementQName = new QName();
+    final QName fAttributeQName = new QName();
+    final XMLAttributesImpl fAttributes = new XMLAttributesImpl();
+    final ArrayList fDeclaredPrefixes = new ArrayList();
+    final XMLString fTempString = new XMLString();
+    final XMLStringBuffer fStringBuffer = new XMLStringBuffer();
+
+    public StAXValidatorHelper(XMLSchemaValidatorComponentManager componentManager) {
+        fComponentManager = componentManager;
+        fErrorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER);
+        fSchemaValidator = (XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR);
+        fSymbolTable = (SymbolTable) fComponentManager.getProperty(SYMBOL_TABLE);        
+        fValidationManager = (ValidationManager) fComponentManager.getProperty(VALIDATION_MANAGER);
+        fNamespaceContext = new JAXPNamespaceContextWrapper(fSymbolTable);
+        fNamespaceContext.setDeclaredPrefixes(fDeclaredPrefixes);
+    }
+    
+    /*
+     * ValidatorHelper methods
+     */
+
+    public void validate(Source source, Result result) throws SAXException,
+            IOException {
+        if (result instanceof StAXResult || result == null) {
+            StAXSource staxSource = (StAXSource) source;
+            StAXResult staxResult = (StAXResult) result;
+            try {
+                XMLStreamReader streamReader = staxSource.getXMLStreamReader();
+                if (streamReader != null) {
+                    // Hand off to XMLStreamReader helper.
+                    if (fStreamHelper == null) {
+                        fStreamHelper = new StreamHelper();
+                    }
+                    fStreamHelper.validate(streamReader, staxResult);
+                }
+                else {
+                    // Hand off to XMLEventReader helper.
+                    if (fEventHelper == null) {
+                        fEventHelper = new EventHelper();
+                    }
+                    fEventHelper.validate(staxSource.getXMLEventReader(), staxResult);
+                } 
+            }
+            catch (XMLStreamException e) {
+                throw new SAXException(e);
+            }
+            catch (XMLParseException e) {
+                throw Util.toSAXParseException(e);
+            }
+            catch (XNIException e) {
+                throw Util.toSAXException(e);
+            }
+            finally {
+                // Release references to application objects
+                fCurrentEvent = null;
+                fStAXLocationWrapper.setLocation(null);
+                if (fStAXValidatorHandler != null) {
+                    fStAXValidatorHandler.setStAXResult(null);
+                }
+            }
+            return;
+        }
+        throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
+                "SourceResultMismatch", 
+                new Object [] {source.getClass().getName(), result.getClass().getName()}));
+    }
+
+    /*
+     * EntityState methods
+     */
+    
+    public boolean isEntityDeclared(String name) {
+        if (fEntities != null) {
+            return fEntities.containsKey(name);
+        }
+        return false;
+    }
+
+    public boolean isEntityUnparsed(String name) {
+        if (fEntities != null) {
+            EntityDeclaration entityDecl = (EntityDeclaration) fEntities.get(name);
+            // If the entity is associated with a notation then it must be an unparsed entity.
+            if (entityDecl != null) {
+                return (entityDecl.getNotationName() != null);
+            }
+        }
+        return false;
+    }
+    
+    /*
+     * Other methods.
+     */
+    
+    final EntityDeclaration getEntityDeclaration(String name) {
+        return (fEntities != null) ? (EntityDeclaration) fEntities.get(name) : null;
+    }
+    
+    final XMLEvent getCurrentEvent() {
+        return fCurrentEvent;
+    }
+    
+    /** Fills in a QName object. */
+    final void fillQName(QName toFill, String uri, String localpart, String prefix) {
+        uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
+        localpart = (localpart != null) ? fSymbolTable.addSymbol(localpart) : XMLSymbols.EMPTY_STRING;
+        prefix = (prefix != null && prefix.length() > 0) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
+        String raw = localpart;
+        if (prefix != XMLSymbols.EMPTY_STRING) {
+            fStringBuffer.clear();
+            fStringBuffer.append(prefix);
+            fStringBuffer.append(':');
+            fStringBuffer.append(localpart);
+            raw = fSymbolTable.addSymbol(fStringBuffer.ch, fStringBuffer.offset, fStringBuffer.length);
+        }
+        toFill.setValues(prefix, localpart, raw, uri);
+    }
+    
+    /** Setup for validation. **/
+    final void setup(Location location, StAXResult result) {
+        fDepth = 0;
+        fComponentManager.reset();
+        setupStAXResultHandler(result);
+        fValidationManager.setEntityState(this);
+        if (fEntities != null && !fEntities.isEmpty()) {
+            // should only clear this if the last document contained unparsed entities
+            fEntities.clear();
+        }
+        fStAXLocationWrapper.setLocation(location);
+        fErrorReporter.setDocumentLocator(fStAXLocationWrapper);
+    }
+    
+    /** Copies entity declarations into a hash map. */
+    final void processEntityDeclarations(List entityDecls) {
+        int size = (entityDecls != null) ? entityDecls.size() : 0;
+        if (size > 0) {
+            if (fEntities == null) {
+                fEntities = new HashMap();
+            }
+            for (int i = 0; i < size; ++i) {
+                EntityDeclaration decl = (EntityDeclaration) entityDecls.get(i);
+                fEntities.put(decl.getName(), decl);
+            }
+        }
+    }
+    
+    /**
+     * Sets up handler for <code>StAXResult</code>.
+     */
+    private void setupStAXResultHandler(StAXResult result) {
+        // If there's no StAXResult, unset the validator handler
+        if (result == null) {
+            fStAXValidatorHandler = null;
+            fSchemaValidator.setDocumentHandler(null);
+            return;
+        }
+        XMLStreamWriter writer = result.getXMLStreamWriter();
+        if (writer != null) {
+            if (fStAXStreamResultBuilder == null) {
+                fStAXStreamResultBuilder = new StAXStreamResultBuilder(fNamespaceContext);
+            }
+            fStAXValidatorHandler = fStAXStreamResultBuilder;
+            fStAXStreamResultBuilder.setStAXResult(result);
+        }
+        else {
+            if (fStAXEventResultBuilder == null) {
+                fStAXEventResultBuilder = new StAXEventResultBuilder(this, fNamespaceContext);
+            }
+            fStAXValidatorHandler = fStAXEventResultBuilder;
+            fStAXEventResultBuilder.setStAXResult(result);
+        }
+        fSchemaValidator.setDocumentHandler(fStAXValidatorHandler);
+    }
+    
+    /**
+     * Helper for <code>XMLStreamReader</code>s.
+     */
+    final class StreamHelper {
+        
+        StreamHelper() {}
+        
+        final void validate(XMLStreamReader reader, StAXResult result) 
+            throws SAXException, XMLStreamException {
+            if (reader.hasNext()) {
+                int eventType = reader.getEventType();
+                if (eventType != XMLStreamConstants.START_DOCUMENT &&
+                    eventType != XMLStreamConstants.START_ELEMENT) {
+                    throw new SAXException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
+                            "StAXIllegalInitialState", null));
+                }
+                setup(reader.getLocation(), result);
+                fSchemaValidator.startDocument(fStAXLocationWrapper, null, fNamespaceContext, null);
+                do {
+                    switch (eventType) {
+                        case XMLStreamConstants.START_ELEMENT:
+                            ++fDepth;
+                            fillQName(fElementQName, reader.getNamespaceURI(), 
+                                    reader.getLocalName(), reader.getPrefix());
+                            fillXMLAttributes(reader);
+                            fillDeclaredPrefixes(reader);
+                            fNamespaceContext.setNamespaceContext(reader.getNamespaceContext());
+                            fSchemaValidator.startElement(fElementQName, fAttributes, null);
+                            break;
+                        case XMLStreamConstants.END_ELEMENT:
+                            fillQName(fElementQName, reader.getNamespaceURI(), 
+                                    reader.getLocalName(), reader.getPrefix());
+                            fillDeclaredPrefixes(reader);
+                            fNamespaceContext.setNamespaceContext(reader.getNamespaceContext());
+                            fSchemaValidator.endElement(fElementQName, null);
+                            --fDepth;
+                            break;
+                        case XMLStreamConstants.CHARACTERS:
+                        case XMLStreamConstants.SPACE:
+                            fTempString.setValues(reader.getTextCharacters(), 
+                                    reader.getTextStart(), reader.getTextLength());
+                            fSchemaValidator.characters(fTempString, null);
+                            break;
+                        case XMLStreamConstants.CDATA:
+                            fSchemaValidator.startCDATA(null);
+                            fTempString.setValues(reader.getTextCharacters(), 
+                                    reader.getTextStart(), reader.getTextLength());
+                            fSchemaValidator.characters(fTempString, null);
+                            fSchemaValidator.endCDATA(null);
+                            break;
+                        case XMLStreamConstants.START_DOCUMENT:
+                            ++fDepth;
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.startDocument(reader);
+                            }
+                            break;
+                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.processingInstruction(reader);
+                            }
+                            break;
+                        case XMLStreamConstants.COMMENT:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.comment(reader);
+                            }
+                            break;
+                        case XMLStreamConstants.ENTITY_REFERENCE:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.entityReference(reader);
+                            }
+                            break;
+                        case XMLStreamConstants.DTD:
+                            processEntityDeclarations((List) reader.getProperty("javax.xml.stream.entities"));
+                            break;
+                    }
+                    eventType = reader.next();
+                }
+                while (reader.hasNext() && fDepth > 0);
+                fSchemaValidator.endDocument(null);
+                if (eventType == XMLStreamConstants.END_DOCUMENT && fStAXValidatorHandler != null) {
+                    fStAXValidatorHandler.endDocument(reader);
+                }
+            } 
+        }
+        
+        /** Fills in the XMLAttributes object. */
+        private void fillXMLAttributes(XMLStreamReader reader) {
+            fAttributes.removeAllAttributes();
+            final int len = reader.getAttributeCount();
+            for (int i = 0; i < len; ++i) {
+                fillQName(fAttributeQName, reader.getAttributeNamespace(i), 
+                        reader.getAttributeLocalName(i), reader.getAttributePrefix(i));
+                String type = reader.getAttributeType(i);
+                fAttributes.addAttributeNS(fAttributeQName, 
+                        (type != null) ? type : XMLSymbols.fCDATASymbol, reader.getAttributeValue(i));
+                fAttributes.setSpecified(i, reader.isAttributeSpecified(i));
+            }
+        }
+        
+        /** Fills in the list of declared prefixes. */
+        private void fillDeclaredPrefixes(XMLStreamReader reader) {
+            fDeclaredPrefixes.clear();
+            final int len = reader.getNamespaceCount();
+            for (int i = 0; i < len; ++i) {
+                String prefix = reader.getNamespacePrefix(i);
+                fDeclaredPrefixes.add(prefix != null ? prefix : "");
+            }
+        }
+    }
+    
+    /**
+     * Helper for <code>XMLEventReader</code>s.
+     */
+    final class EventHelper {
+        
+        //
+        // Constants
+        //
+
+        /** Chunk size (1024). */
+        private static final int CHUNK_SIZE = (1 << 10);
+        
+        /** Chunk mask (CHUNK_SIZE - 1). */
+        private static final int CHUNK_MASK = CHUNK_SIZE - 1;
+        
+        //
+        // Data
+        //
+        
+        /** Array for holding character data. **/
+        private char [] fCharBuffer = new char[CHUNK_SIZE];
+        
+        EventHelper() {}
+        
+        final void validate(XMLEventReader reader, StAXResult result) 
+            throws SAXException, XMLStreamException {
+            fCurrentEvent = reader.peek();
+            if (fCurrentEvent != null) {
+                int eventType = fCurrentEvent.getEventType();
+                if (eventType != XMLStreamConstants.START_DOCUMENT &&
+                    eventType != XMLStreamConstants.START_ELEMENT) {
+                    throw new SAXException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
+                            "StAXIllegalInitialState", null));
+                }
+                setup(null, result);
+                fSchemaValidator.startDocument(fStAXLocationWrapper, null, fNamespaceContext, null);
+                loop : while (reader.hasNext()) {
+                    fCurrentEvent = reader.nextEvent();
+                    eventType = fCurrentEvent.getEventType();
+                    switch (eventType) {
+                        case XMLStreamConstants.START_ELEMENT:
+                            ++fDepth;
+                            StartElement start = fCurrentEvent.asStartElement();
+                            fillQName(fElementQName, start.getName());
+                            fillXMLAttributes(start);
+                            fillDeclaredPrefixes(start);
+                            fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
+                            fStAXLocationWrapper.setLocation(start.getLocation());
+                            fSchemaValidator.startElement(fElementQName, fAttributes, null);
+                            break;
+                        case XMLStreamConstants.END_ELEMENT:
+                            EndElement end = fCurrentEvent.asEndElement();
+                            fillQName(fElementQName, end.getName());
+                            fillDeclaredPrefixes(end);
+                            fStAXLocationWrapper.setLocation(end.getLocation());
+                            fSchemaValidator.endElement(fElementQName, null);
+                            if (--fDepth <= 0) {
+                                break loop;
+                            }
+                            break;
+                        case XMLStreamConstants.CHARACTERS:
+                        case XMLStreamConstants.SPACE:
+                            if (fStAXValidatorHandler != null) {
+                                Characters chars = fCurrentEvent.asCharacters();
+                                fStAXValidatorHandler.setIgnoringCharacters(true);
+                                sendCharactersToValidator(chars.getData());
+                                fStAXValidatorHandler.setIgnoringCharacters(false);
+                                fStAXValidatorHandler.characters(chars);
+                            }
+                            else {
+                                sendCharactersToValidator(fCurrentEvent.asCharacters().getData());
+                            }
+                            break;
+                        case XMLStreamConstants.CDATA:
+                            if (fStAXValidatorHandler != null) {
+                                Characters chars = fCurrentEvent.asCharacters();
+                                fStAXValidatorHandler.setIgnoringCharacters(true);
+                                fSchemaValidator.startCDATA(null);
+                                sendCharactersToValidator(fCurrentEvent.asCharacters().getData());
+                                fSchemaValidator.endCDATA(null);
+                                fStAXValidatorHandler.setIgnoringCharacters(false);
+                                fStAXValidatorHandler.cdata(chars);
+                            }
+                            else {
+                                fSchemaValidator.startCDATA(null);
+                                sendCharactersToValidator(fCurrentEvent.asCharacters().getData());
+                                fSchemaValidator.endCDATA(null);
+                            }
+                            break;
+                        case XMLStreamConstants.START_DOCUMENT:
+                            ++fDepth;
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.startDocument((StartDocument) fCurrentEvent);
+                            }
+                            break;
+                        case XMLStreamConstants.END_DOCUMENT:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.endDocument((EndDocument) fCurrentEvent);
+                            }
+                            break;
+                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.processingInstruction((ProcessingInstruction) fCurrentEvent);
+                            }
+                            break;
+                        case XMLStreamConstants.COMMENT:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.comment((Comment) fCurrentEvent);
+                            }
+                            break;
+                        case XMLStreamConstants.ENTITY_REFERENCE:
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.entityReference((EntityReference) fCurrentEvent);
+                            }
+                            break;
+                        case XMLStreamConstants.DTD:
+                            DTD dtd = (DTD) fCurrentEvent;
+                            processEntityDeclarations(dtd.getEntities());
+                            if (fStAXValidatorHandler != null) {
+                                fStAXValidatorHandler.doctypeDecl(dtd);
+                            }
+                            break;
+                    }
+                }
+                fSchemaValidator.endDocument(null);
+            }
+        }
+        
+        /** Fills in a QName object. */
+        private void fillQName(QName toFill, javax.xml.namespace.QName toCopy) {
+            StAXValidatorHelper.this.fillQName(toFill, toCopy.getNamespaceURI(), toCopy.getLocalPart(), toCopy.getPrefix());
+        }
+        
+        /** Fills in the XMLAttributes object. */
+        private void fillXMLAttributes(StartElement event) {
+            fAttributes.removeAllAttributes();
+            final Iterator attrs = event.getAttributes();
+            while (attrs.hasNext()) {
+                Attribute attr = (Attribute) attrs.next();
+                fillQName(fAttributeQName, attr.getName());
+                String type = attr.getDTDType();
+                int idx = fAttributes.getLength();
+                fAttributes.addAttributeNS(fAttributeQName, 
+                        (type != null) ? type : XMLSymbols.fCDATASymbol, attr.getValue());
+                fAttributes.setSpecified(idx, attr.isSpecified());
+            }
+        }
+        
+        /** Fills in the list of declared prefixes. */
+        private void fillDeclaredPrefixes(StartElement event) {
+            fillDeclaredPrefixes(event.getNamespaces());
+        }
+        
+        /** Fills in the list of declared prefixes. */
+        private void fillDeclaredPrefixes(EndElement event) {
+            fillDeclaredPrefixes(event.getNamespaces());
+        }
+        
+        /** Fills in the list of declared prefixes. */
+        private void fillDeclaredPrefixes(Iterator namespaces) {
+            fDeclaredPrefixes.clear();
+            while (namespaces.hasNext()) {
+                Namespace ns = (Namespace) namespaces.next();
+                String prefix = ns.getPrefix();
+                fDeclaredPrefixes.add(prefix != null ? prefix : "");
+            }
+        }
+        
+        /** Send characters to the validator in CHUNK_SIZE character chunks. */
+        private void sendCharactersToValidator(String str) {
+            if (str != null) {
+                final int length = str.length();
+                final int remainder = length & CHUNK_MASK;
+                if (remainder > 0) {
+                    str.getChars(0, remainder, fCharBuffer, 0);
+                    fTempString.setValues(fCharBuffer, 0, remainder);
+                    fSchemaValidator.characters(fTempString, null);
+                }
+                int i = remainder;
+                while (i < length) {
+                    str.getChars(i, i += CHUNK_SIZE, fCharBuffer, 0);
+                    fTempString.setValues(fCharBuffer, 0, CHUNK_SIZE);
+                    fSchemaValidator.characters(fTempString, null);
+                }
+            }
+        }
+    }
+
+} // StAXValidatorHelper

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/StAXValidatorHelper.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: xerces/java/trunk/src/org/apache/xerces/jaxp/validation/ValidatorImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/jaxp/validation/ValidatorImpl.java?rev=683688&r1=683687&r2=683688&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/jaxp/validation/ValidatorImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/jaxp/validation/ValidatorImpl.java Thu Aug  7 12:37:02 2008
@@ -24,6 +24,7 @@
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Validator;
 
@@ -68,6 +69,9 @@
     /** DOM validator helper. **/
     private DOMValidatorHelper fDOMValidatorHelper;
     
+    /** StAX validator helper. **/
+    private StAXValidatorHelper fStAXValidatorHelper;
+    
     /** Stream validator helper. **/
     private StreamValidatorHelper fStreamValidatorHelper;
     
@@ -102,6 +106,13 @@
             }
             fDOMValidatorHelper.validate(source, result);
         }
+        else if (source instanceof StAXSource) {
+            // Hand off to StAX validator helper.
+            if (fStAXValidatorHelper == null) {
+                fStAXValidatorHelper = new StAXValidatorHelper(fComponentManager);
+            }
+            fStAXValidatorHelper.validate(source, result);
+        }
         else if (source instanceof StreamSource) {
             // Hand off to stream validator helper.
             if (fStreamValidatorHelper == null) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org