1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 
  23 package com.sun.org.apache.xalan.internal.xsltc.trax;
  24 
  25 import javax.xml.transform.Source;
  26 import javax.xml.transform.stream.StreamSource;
  27 
  28 import com.sun.org.apache.xalan.internal.xsltc.DOM;
  29 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
  30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  31 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter;
  32 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
  33 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
  34 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
  35 
  36 import org.xml.sax.SAXException;
  37 
  38 /**
  39  * @author Morten Jorgensen
  40  */
  41 public final class XSLTCSource implements Source {
  42 
  43     private String     _systemId = null;
  44     private Source     _source   = null;
  45     private ThreadLocal _dom     = new ThreadLocal();
  46 
  47     /**
  48      * Create a new XSLTC-specific source from a system ID
  49      */
  50     public XSLTCSource(String systemId)
  51     {
  52         _systemId = systemId;
  53     }
  54 
  55     /**
  56      * Create a new XSLTC-specific source from a JAXP Source
  57      */
  58     public XSLTCSource(Source source)
  59     {
  60         _source = source;
  61     }
  62 
  63     /**
  64      * Implements javax.xml.transform.Source.setSystemId()
  65      * Set the system identifier for this Source.
  66      * This Source can get its input either directly from a file (in this case
  67      * it will instanciate and use a JAXP parser) or it can receive it through
  68      * ContentHandler/LexicalHandler interfaces.
  69      * @param systemId The system Id for this Source
  70      */
  71     public void setSystemId(String systemId) {
  72         _systemId = systemId;
  73         if (_source != null) {
  74             _source.setSystemId(systemId);
  75         }
  76     }
  77 
  78     /**
  79      * Implements javax.xml.transform.Source.getSystemId()
  80      * Get the system identifier that was set with setSystemId.
  81      * @return The system identifier that was set with setSystemId,
  82      *         or null if setSystemId was not called.
  83      */
  84     public String getSystemId() {
  85         if (_source != null) {
  86             return _source.getSystemId();
  87         }
  88         else {
  89             return(_systemId);
  90         }
  91     }
  92 
  93     /**
  94      * Internal interface which returns a DOM for a given DTMManager and translet.
  95      */
  96     protected DOM getDOM(XSLTCDTMManager dtmManager, AbstractTranslet translet)
  97         throws SAXException
  98     {
  99         SAXImpl idom = (SAXImpl)_dom.get();
 100 
 101         if (idom != null) {
 102             if (dtmManager != null) {
 103                 idom.migrateTo(dtmManager);
 104             }
 105         }
 106         else {
 107             Source source = _source;
 108             if (source == null) {
 109                 if (_systemId != null && _systemId.length() > 0) {
 110                     source = new StreamSource(_systemId);
 111                 }
 112                 else {
 113                     ErrorMsg err = new ErrorMsg(ErrorMsg.XSLTC_SOURCE_ERR);
 114                     throw new SAXException(err.toString());
 115                 }
 116             }
 117 
 118             DOMWSFilter wsfilter = null;
 119             if (translet != null && translet instanceof StripFilter) {
 120                 wsfilter = new DOMWSFilter(translet);
 121             }
 122 
 123             boolean hasIdCall = (translet != null) ? translet.hasIdCall() : false;
 124 
 125             if (dtmManager == null) {
 126                 dtmManager = XSLTCDTMManager.newInstance();
 127             }
 128 
 129             idom = (SAXImpl)dtmManager.getDTM(source, true, wsfilter, false, false, hasIdCall);
 130 
 131             String systemId = getSystemId();
 132             if (systemId != null) {
 133                 idom.setDocumentURI(systemId);
 134             }
 135             _dom.set(idom);
 136         }
 137         return idom;
 138     }
 139 
 140 }