jaxp/src/com/sun/org/apache/xpath/internal/SourceTreeManager.java
author lana
Tue, 18 Mar 2014 17:49:48 -0700
changeset 23377 2af1ddf102a4
parent 12457 c348e06f0e82
permissions -rw-r--r--
Merge
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
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: SourceTreeManager.java,v 1.2.4.1 2005/09/10 18:14:09 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xpath.internal;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import javax.xml.transform.Source;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import javax.xml.transform.SourceLocator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import javax.xml.transform.TransformerException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import javax.xml.transform.URIResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import javax.xml.transform.sax.SAXSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import javax.xml.transform.stream.StreamSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xml.internal.dtm.DTM;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import org.xml.sax.helpers.XMLReaderFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * This class bottlenecks all management of source trees.  The methods
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * in this class should allow easy garbage collection of source
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * trees (not yet!), and should centralize parsing for those source trees.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
public class SourceTreeManager
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  /** Vector of SourceTree objects that this manager manages. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  private Vector m_sourceTree = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   * Reset the list of SourceTree objects that this manager manages.
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
  public void reset()
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    m_sourceTree = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  /** The TrAX URI resolver used to obtain source trees. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  URIResolver m_uriResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   * Set an object that will be used to resolve URIs used in
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * document(), etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * @param resolver An object that implements the URIResolver interface,
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
  public void setURIResolver(URIResolver resolver)
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    m_uriResolver = resolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   * Get the object that will be used to resolve URIs used in
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
   * document(), etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * @return An object that implements the URIResolver interface,
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
  public URIResolver getURIResolver()
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    return m_uriResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   * Given a document, find the URL associated with that document.
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
   * @param owner Document that was previously processed by this liaison.
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   * @return The base URI of the owner argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  public String findURIFromDoc(int owner)
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    int n = m_sourceTree.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    for (int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
      SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      if (owner == sTree.m_root)
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        return sTree.m_url;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
   * This will be called by the processor when it encounters
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
   * an xsl:include, xsl:import, or document() function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * @param base The base URI that should be used.
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * @param urlString Value from an xsl:import or xsl:include's href attribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   * or a URI specified in the document() function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * @return a Source that can be used to process the resource.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  public Source resolveURI(
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
          String base, String urlString, SourceLocator locator)
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            throws TransformerException, IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    Source source = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    if (null != m_uriResolver)
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
      source = m_uriResolver.resolve(urlString, base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    if (null == source)
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      String uri = SystemIDResolver.getAbsoluteURI(urlString, base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
      source = new StreamSource(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    return source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
  /** JJK: Support  <?xalan:doc_cache_off?> kluge in ElemForEach.
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
   * TODO: This function is highly dangerous. Cache management must be improved.
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
   * @param n The node to remove.
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
  public void removeDocumentFromCache(int n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    if(DTM.NULL ==n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    for(int i=m_sourceTree.size()-1;i>=0;--i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      SourceTree st=(SourceTree)m_sourceTree.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
      if(st!=null && st.m_root==n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        m_sourceTree.removeElementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
   * Put the source tree root node in the document cache.
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   * TODO: This function needs to be a LOT more sophisticated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
   * @param n The node to cache.
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
   * @param source The Source object to cache.
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
  public void putDocumentInCache(int n, Source source)
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    int cachedNode = getNode(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    if (DTM.NULL != cachedNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
      if (!(cachedNode == n))
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        throw new RuntimeException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
          "Programmer's Error!  "
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
          + "putDocumentInCache found reparse of doc: "
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
          + source.getSystemId());
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
      return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    if (null != source.getSystemId())
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
      m_sourceTree.addElement(new SourceTree(n, source.getSystemId()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * Given a Source object, find the node associated with it.
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
   * @param source The Source object to act as the key.
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
   * @return The node that is associated with the Source, or null if not found.
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
  public int getNode(Source source)
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
//    if (source instanceof DOMSource)
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
//      return ((DOMSource) source).getNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    // TODO: Not sure if the BaseID is really the same thing as the ID.
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
    String url = source.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    if (null == url)
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
      return DTM.NULL;
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    int n = m_sourceTree.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    // System.out.println("getNode: "+n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    for (int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
      SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
      // System.out.println("getNode -         url: "+url);
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
      // System.out.println("getNode - sTree.m_url: "+sTree.m_url);
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
      if (url.equals(sTree.m_url))
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        return sTree.m_root;
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    // System.out.println("getNode - returning: "+node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    return DTM.NULL;
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   * Get the source tree from the a base URL and a URL string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   * @param base The base URI to use if the urlString is relative.
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
   * @param urlString An absolute or relative URL string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
   * @param locator The location of the caller, for diagnostic purposes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
   * @return should be a non-null reference to the node identified by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
   * base and urlString.
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   * @throws TransformerException If the URL can not resolve to a node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
  public int getSourceTree(
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
          String base, String urlString, SourceLocator locator, XPathContext xctxt)
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    // System.out.println("getSourceTree");
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
      Source source = this.resolveURI(base, urlString, locator);
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
      // System.out.println("getSourceTree - base: "+base+", urlString: "+urlString+", source: "+source.getSystemId());
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
      return getSourceTree(source, locator, xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
    catch (IOException ioe)
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
      throw new TransformerException(ioe.getMessage(), locator, ioe);
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
    /* catch (TransformerException te)
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
     {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
       throw new TransformerException(te.getMessage(), locator, te);
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
     }*/
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   * Get the source tree from the input source.
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   * @param source The Source object that should identify the desired node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * @param locator The location of the caller, for diagnostic purposes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
   * @return non-null reference to a node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
   * @throws TransformerException if the Source argument can't be resolved to
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
   *         a node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
  public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt)
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
    int n = getNode(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    if (DTM.NULL != n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
      return n;
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
    n = parseToNode(source, locator, xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    if (DTM.NULL != n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
      putDocumentInCache(n, source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
    return n;
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   * Try to create a DOM source tree from the input source.
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
   * @param source The Source object that identifies the source node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
   * @param locator The location of the caller, for diagnostic purposes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
   * @return non-null reference to node identified by the source argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
   * @throws TransformerException if the source argument can not be resolved
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
   *         to a source node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
  public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt)
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
      Object xowner = xctxt.getOwnerObject();
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
      DTM dtm;
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
      if(null != xowner && xowner instanceof com.sun.org.apache.xml.internal.dtm.DTMWSFilter)
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
        dtm = xctxt.getDTM(source, false,
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
                          (com.sun.org.apache.xml.internal.dtm.DTMWSFilter)xowner, false, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
        dtm = xctxt.getDTM(source, false, null, false, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
      return dtm.getDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
    catch (Exception e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
      //e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
      throw new TransformerException(e.getMessage(), locator, e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
   * This method returns the SAX2 parser to use with the InputSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
   * obtained from this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
   * It may return null if any SAX2-conformant XML parser can be used,
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
   * or if getInputSource() will also return null. The parser must
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
   * be free for use (i.e.
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
   * not currently in use for another parse().
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
   * @param inputSource The value returned from the URIResolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
   * @return a SAX2 XMLReader to use to resolve the inputSource argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
   * @param locator The location of the original caller, for diagnostic purposes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
   * @throws TransformerException if the reader can not be created.
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
  public static XMLReader getXMLReader(Source inputSource, SourceLocator locator)
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
      XMLReader reader = (inputSource instanceof SAXSource)
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
                         ? ((SAXSource) inputSource).getXMLReader() : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
      if (null == reader)
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
          javax.xml.parsers.SAXParserFactory factory=
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
              javax.xml.parsers.SAXParserFactory.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
          factory.setNamespaceAware( true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
          javax.xml.parsers.SAXParser jaxpParser=
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
              factory.newSAXParser();
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
          reader=jaxpParser.getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
        } catch( javax.xml.parsers.ParserConfigurationException ex ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
          throw new org.xml.sax.SAXException( ex );
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
        } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
            throw new org.xml.sax.SAXException( ex1.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
        } catch( NoSuchMethodError ex2 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        catch (AbstractMethodError ame){}
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
        if(null == reader)
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
          reader = XMLReaderFactory.createXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
      try
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
                          true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
      catch (org.xml.sax.SAXException se)
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
        // What can we do?
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
        // TODO: User diagnostics.
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
      return reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    catch (org.xml.sax.SAXException se)
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
      throw new TransformerException(se.getMessage(), locator, se);
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
}