286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A *
286N/A * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Xerces" and "Apache Software Foundation" must
286N/A * not be used to endorse or promote products derived from this
286N/A * software without prior written permission. For written
286N/A * permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * nor may "Apache" appear in their name, without prior written
286N/A * permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation and was
286N/A * originally based on software copyright (c) 1999, International
286N/A * Business Machines, Inc., http://www.apache.org. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/Apackage com.sun.org.apache.xerces.internal.util;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
286N/Aimport com.sun.org.apache.xerces.internal.jaxp.validation.WrappedSAXException;
286N/Aimport com.sun.org.apache.xerces.internal.xni.QName;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLAttributes;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLLocator;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLString;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
286N/Aimport org.xml.sax.Attributes;
286N/Aimport org.xml.sax.ContentHandler;
286N/Aimport org.xml.sax.Locator;
286N/Aimport org.xml.sax.SAXException;
286N/A
286N/A/**
286N/A * Receves SAX {@link ContentHandler} events
286N/A * and produces the equivalent {@link XMLDocumentHandler} events.
286N/A *
286N/A * @author
286N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
286N/A */
286N/Apublic class SAX2XNI implements ContentHandler, XMLDocumentSource {
286N/A public SAX2XNI( XMLDocumentHandler core ) {
286N/A this.fCore = core;
286N/A }
286N/A
286N/A private XMLDocumentHandler fCore;
286N/A
286N/A private final NamespaceSupport nsContext = new NamespaceSupport();
286N/A private final SymbolTable symbolTable = new SymbolTable();
286N/A
286N/A
286N/A public void setDocumentHandler(XMLDocumentHandler handler) {
286N/A fCore = handler;
286N/A }
286N/A
286N/A public XMLDocumentHandler getDocumentHandler() {
286N/A return fCore;
286N/A }
286N/A
286N/A
286N/A //
286N/A //
286N/A // ContentHandler implementation
286N/A //
286N/A //
286N/A public void startDocument() throws SAXException {
286N/A try {
286N/A nsContext.reset();
286N/A
286N/A XMLLocator xmlLocator;
286N/A if(locator==null)
286N/A // some SAX source doesn't provide a locator,
286N/A // in which case we assume no line information is available
286N/A // and use a dummy locator. With this, downstream components
286N/A // can always assume that they will get a non-null Locator.
286N/A xmlLocator=new SimpleLocator(null,null,-1,-1);
286N/A else
286N/A xmlLocator=new LocatorWrapper(locator);
286N/A
286N/A fCore.startDocument(
286N/A xmlLocator,
286N/A null,
286N/A nsContext,
286N/A null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void endDocument() throws SAXException {
286N/A try {
286N/A fCore.endDocument(null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void startElement( String uri, String local, String qname, Attributes att ) throws SAXException {
286N/A try {
286N/A fCore.startElement(createQName(uri,local,qname),createAttributes(att),null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void endElement( String uri, String local, String qname ) throws SAXException {
286N/A try {
286N/A fCore.endElement(createQName(uri,local,qname),null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void characters( char[] buf, int offset, int len ) throws SAXException {
286N/A try {
286N/A fCore.characters(new XMLString(buf,offset,len),null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void ignorableWhitespace( char[] buf, int offset, int len ) throws SAXException {
286N/A try {
286N/A fCore.ignorableWhitespace(new XMLString(buf,offset,len),null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void startPrefixMapping( String prefix, String uri ) {
286N/A nsContext.pushContext();
286N/A nsContext.declarePrefix(prefix,uri);
286N/A }
286N/A
286N/A public void endPrefixMapping( String prefix ) {
286N/A nsContext.popContext();
286N/A }
286N/A
286N/A public void processingInstruction( String target, String data ) throws SAXException {
286N/A try {
286N/A fCore.processingInstruction(
286N/A symbolize(target),createXMLString(data),null);
286N/A } catch( WrappedSAXException e ) {
286N/A throw e.exception;
286N/A }
286N/A }
286N/A
286N/A public void skippedEntity( String name ) {
286N/A }
286N/A
286N/A private Locator locator;
286N/A public void setDocumentLocator( Locator _loc ) {
286N/A this.locator = _loc;
286N/A }
286N/A
286N/A /** Creates a QName object. */
286N/A private QName createQName(String uri, String local, String raw) {
286N/A
286N/A int idx = raw.indexOf(':');
286N/A
286N/A if( local.length()==0 ) {
286N/A // if naemspace processing is turned off, local could be "".
286N/A // in that case, treat everything to be in the no namespace.
286N/A uri = "";
286N/A if(idx<0)
286N/A local = raw;
286N/A else
286N/A local = raw.substring(idx+1);
286N/A }
286N/A
286N/A String prefix;
286N/A if (idx < 0)
286N/A prefix = null;
286N/A else
286N/A prefix = raw.substring(0, idx);
286N/A
286N/A if (uri != null && uri.length() == 0)
286N/A uri = null; // XNI uses null whereas SAX uses the empty string
286N/A
286N/A return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri));
286N/A }
286N/A
286N/A /** Symbolizes the specified string. */
286N/A private String symbolize(String s) {
286N/A if (s == null)
286N/A return null;
286N/A else
286N/A return symbolTable.addSymbol(s);
286N/A }
286N/A
286N/A private XMLString createXMLString(String str) {
286N/A // with my patch
286N/A // return new XMLString(str);
286N/A
286N/A // for now
286N/A return new XMLString(str.toCharArray(), 0, str.length());
286N/A }
286N/A
286N/A
286N/A /** only one instance of XMLAttributes is used. */
286N/A private final XMLAttributes xa = new XMLAttributesImpl();
286N/A
286N/A /** Creates an XMLAttributes object. */
286N/A private XMLAttributes createAttributes(Attributes att) {
286N/A xa.removeAllAttributes();
286N/A int len = att.getLength();
286N/A for (int i = 0; i < len; i++)
286N/A xa.addAttribute(
286N/A createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)),
286N/A att.getType(i),
286N/A att.getValue(i));
286N/A return xa;
286N/A }
286N/A}