src/java.xml/share/classes/com/sun/org/apache/xerces/internal/util/SAX2XNI.java
author joehw
Thu, 21 Dec 2017 17:08:05 -0800
changeset 48412 d4412e380f6b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8184431: References to @sun.com Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xerces.internal.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.xerces.internal.jaxp.validation.WrappedSAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xerces.internal.xni.QName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xerces.internal.xni.XMLLocator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xerces.internal.xni.XMLString;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import org.xml.sax.Attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import org.xml.sax.ContentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import org.xml.sax.Locator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * Receves SAX {@link ContentHandler} events
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * and produces the equivalent {@link XMLDocumentHandler} events.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * @author
48412
d4412e380f6b 8184431: References to @sun.com
joehw
parents: 47216
diff changeset
    42
 *     Kohsuke Kawaguchi
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
public class SAX2XNI implements ContentHandler, XMLDocumentSource {
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    public SAX2XNI( XMLDocumentHandler core ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
        this.fCore = core;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    private XMLDocumentHandler fCore;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    private final NamespaceSupport nsContext = new NamespaceSupport();
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    private final SymbolTable symbolTable = new SymbolTable();
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    public void setDocumentHandler(XMLDocumentHandler handler) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        fCore = handler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    public XMLDocumentHandler getDocumentHandler() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        return fCore;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    // ContentHandler implementation
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    public void startDocument() throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
            nsContext.reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            XMLLocator xmlLocator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
            if(locator==null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
                // some SAX source doesn't provide a locator,
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
                // in which case we assume no line information is available
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
                // and use a dummy locator. With this, downstream components
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
                // can always assume that they will get a non-null Locator.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
                xmlLocator=new SimpleLocator(null,null,-1,-1);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
                xmlLocator=new LocatorWrapper(locator);
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
            fCore.startDocument(
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
                    xmlLocator,
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                    null,
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
                    nsContext,
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                    null);
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    public void endDocument() throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
            fCore.endDocument(null);
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    public void startElement( String uri, String local, String qname, Attributes att ) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            fCore.startElement(createQName(uri,local,qname),createAttributes(att),null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    public void endElement( String uri, String local, String qname ) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
            fCore.endElement(createQName(uri,local,qname),null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    public void characters( char[] buf, int offset, int len ) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            fCore.characters(new XMLString(buf,offset,len),null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    public void ignorableWhitespace( char[] buf, int offset, int len ) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
            fCore.ignorableWhitespace(new XMLString(buf,offset,len),null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    public void startPrefixMapping( String prefix, String uri ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        nsContext.pushContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        nsContext.declarePrefix(prefix,uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    public void endPrefixMapping( String prefix ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        nsContext.popContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    public void processingInstruction( String target, String data ) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            fCore.processingInstruction(
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
                    symbolize(target),createXMLString(data),null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        } catch( WrappedSAXException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
            throw e.exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    public void skippedEntity( String name ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    private Locator locator;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    public void setDocumentLocator( Locator _loc ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        this.locator = _loc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    /** Creates a QName object. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    private QName createQName(String uri, String local, String raw) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        int idx = raw.indexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        if( local.length()==0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
            // if naemspace processing is turned off, local could be "".
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
            // in that case, treat everything to be in the no namespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            uri = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
            if(idx<0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
                local = raw;
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                local = raw.substring(idx+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
        String prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        if (idx < 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
            prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
            prefix = raw.substring(0, idx);
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        if (uri != null && uri.length() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
            uri = null; // XNI uses null whereas SAX uses the empty string
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri));
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    /** Symbolizes the specified string. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    private String symbolize(String s) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        if (s == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
            return symbolTable.addSymbol(s);
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
    private XMLString createXMLString(String str) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
        // with my patch
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        // return new XMLString(str);
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        // for now
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
        return new XMLString(str.toCharArray(), 0, str.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    /** only one instance of XMLAttributes is used. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    private final XMLAttributes xa = new XMLAttributesImpl();
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    /** Creates an XMLAttributes object. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    private XMLAttributes createAttributes(Attributes att) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
        xa.removeAllAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
        int len = att.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
        for (int i = 0; i < len; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
            xa.addAttribute(
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
                    createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)),
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
                    att.getType(i),
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                    att.getValue(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        return xa;
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
}