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