001    
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements. See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership. The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the  "License");
008     * you may not use this file except in compliance with the License.
009     * You may obtain a copy of the License at
010     *
011     *     http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    /*
020     * $Id: XSLTCSource.java 468653 2006-10-28 07:07:05Z minchau $
021     */
022    
023    package org.apache.xalan.xsltc.trax;
024    
025    import javax.xml.transform.Source;
026    import javax.xml.transform.stream.StreamSource;
027    
028    import org.apache.xalan.xsltc.DOM;
029    import org.apache.xalan.xsltc.StripFilter;
030    import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
031    import org.apache.xalan.xsltc.dom.DOMWSFilter;
032    import org.apache.xalan.xsltc.dom.SAXImpl;
033    import org.apache.xalan.xsltc.dom.XSLTCDTMManager;
034    import org.apache.xalan.xsltc.runtime.AbstractTranslet;
035    
036    import org.xml.sax.SAXException;
037    
038    /**
039     * @author Morten Jorgensen
040     */
041    public final class XSLTCSource implements Source {
042    
043        private String     _systemId = null;
044        private Source     _source   = null;
045        private ThreadLocal _dom     = new ThreadLocal();
046    
047        /**
048         * Create a new XSLTC-specific source from a system ID 
049         */
050        public XSLTCSource(String systemId) 
051        {
052            _systemId = systemId;
053        }
054    
055        /**
056         * Create a new XSLTC-specific source from a JAXP Source
057         */
058        public XSLTCSource(Source source) 
059        {
060            _source = source;
061        }
062    
063        /**
064         * Implements javax.xml.transform.Source.setSystemId()
065         * Set the system identifier for this Source. 
066         * This Source can get its input either directly from a file (in this case
067         * it will instanciate and use a JAXP parser) or it can receive it through
068         * ContentHandler/LexicalHandler interfaces.
069         * @param systemId The system Id for this Source
070         */
071        public void setSystemId(String systemId) {
072            _systemId = systemId;
073            if (_source != null) {
074                _source.setSystemId(systemId);
075            }
076        }
077    
078        /**
079         * Implements javax.xml.transform.Source.getSystemId()
080         * Get the system identifier that was set with setSystemId.
081         * @return The system identifier that was set with setSystemId,
082         *         or null if setSystemId was not called.
083         */
084        public String getSystemId() {
085            if (_source != null) {
086                return _source.getSystemId();
087            }
088            else {
089                return(_systemId);
090            }
091        }
092        
093        /**
094         * Internal interface which returns a DOM for a given DTMManager and translet.
095         */
096        protected DOM getDOM(XSLTCDTMManager dtmManager, AbstractTranslet translet)
097            throws SAXException
098        {
099            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    }