1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 /*
21 * $Id: SAXImpl.java,v 1.5 2005/09/28 13:48:37 pvedula Exp $
22 */
23
24 package com.sun.org.apache.xalan.internal.xsltc.dom;
25
26 import com.sun.org.apache.xalan.internal.xsltc.DOM;
27 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
28 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
29 import com.sun.org.apache.xalan.internal.xsltc.TransletException;
30 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
31 import com.sun.org.apache.xml.internal.dtm.Axis;
32 import com.sun.org.apache.xml.internal.dtm.DTM;
33 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
34 import com.sun.org.apache.xml.internal.dtm.DTMManager;
35 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
36 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIterNodeList;
37 import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase;
38 import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;
39 import com.sun.org.apache.xml.internal.dtm.ref.EmptyIterator;
40 import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2DTM2;
41 import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
42 import com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler;
65 * information. They are used when the processing behavior between DOM and
66 * SAX has to be different. Examples of these include id function and
67 * unparsed entity.
68 *
69 * <p>SAXImpl extends SAX2DTM2 instead of SAX2DTM for better performance.
70 * @author Jacek Ambroziak
71 * @author Santiago Pericas-Geertsen
72 * @author Morten Jorgensen
73 * @author Douglas Sellers <douglasjsellers@hotmail.com>
74 */
75 public final class SAXImpl extends SAX2DTM2
76 implements DOMEnhancedForDTM, DOMBuilder
77 {
78
79 /* ------------------------------------------------------------------- */
80 /* DOMBuilder fields BEGIN */
81 /* ------------------------------------------------------------------- */
82
83 // Namespace prefix-to-uri mapping stuff
84 private int _uriCount = 0;
85 private int _prefixCount = 0;
86
87 // Stack used to keep track of what whitespace text nodes are protected
88 // by xml:space="preserve" attributes and which nodes that are not.
89 private int[] _xmlSpaceStack;
90 private int _idx = 1;
91 private boolean _preserve = false;
92
93 private static final String XML_STRING = "xml:";
94 private static final String XML_PREFIX = "xml";
95 private static final String XMLSPACE_STRING = "xml:space";
96 private static final String PRESERVE_STRING = "preserve";
97 private static final String XMLNS_PREFIX = "xmlns";
98 private static final String XML_URI = "http://www.w3.org/XML/1998/namespace";
99
100 private boolean _escaping = true;
101 private boolean _disableEscaping = false;
102 private int _textNodeToProcess = DTM.NULL;
103
104 /* ------------------------------------------------------------------- */
105 /* DOMBuilder fields END */
106 /* ------------------------------------------------------------------- */
107
108 // empty String for null attribute values
109 private final static String EMPTYSTRING = "";
110
111 // empty iterator to be returned when there are no children
112 private final static DTMAxisIterator EMPTYITERATOR = EmptyIterator.getInstance();
113 // The number of expanded names
114 private int _namesSize = -1;
115
116 // Namespace related stuff
117 private Map<Integer, Integer> _nsIndex = new HashMap<>();
118
119 // The initial size of the text buffer
120 private int _size = 0;
121
122 // Tracks which textnodes are not escaped
123 private BitArray _dontEscape = null;
124
125 // The URI to this document
126 private String _documentURI = null;
127 static private int _documentURIIndex = 0;
128
129 // The owner Document when the input source is DOMSource.
130 private Document _document;
131
132 // The Map for org.w3c.dom.Node to node id mapping.
133 // This is only used when the input is a DOMSource and the
134 // buildIdIndex flag is true.
135 private Map<Node, Integer> _node2Ids = null;
136
137 // True if the input source is a DOMSource.
138 private boolean _hasDOMSource = false;
139
140 // The DTMManager
141 private XSLTCDTMManager _dtmManager;
142
143 // Support for access/navigation through org.w3c.dom API
144 private Node[] _nodes;
145 private NodeList[] _nodeLists;
146 private final static String XML_LANG_ATTRIBUTE =
147 "http://www.w3.org/XML/1998/namespace:@lang";
148
149 /**
150 * Define the origin of the document from which the tree was built
151 */
152 public void setDocumentURI(String uri) {
153 if (uri != null) {
154 setDocumentBaseURI(SystemIDResolver.getAbsoluteURI(uri));
155 }
156 }
157
158 /**
159 * Returns the origin of the document from which the tree was built
160 */
161 public String getDocumentURI() {
162 String baseURI = getDocumentBaseURI();
163 return (baseURI != null) ? baseURI : "rtf" + _documentURIIndex++;
164 }
165
166 public String getDocumentURI(int node) {
167 return getDocumentURI();
474 if (s == null) {
475 return 0;
476 }
477 int eType = getIdForNamespace(s);
478 return _nsIndex.get(new Integer(eType));
479 }
480
481
482
483 /**
484 * Returns the namespace type of a specific node
485 */
486 public int getNamespaceType(final int node)
487 {
488 return super.getNamespaceType(node);
489 }
490
491 /**
492 * Sets up a translet-to-dom type mapping table
493 */
494 private int[] setupMapping(String[] names, String[] uris, int[] types, int nNames) {
495 // Padding with number of names, because they
496 // may need to be added, i.e for RTFs. See copy03
497 final int[] result = new int[m_expandedNameTable.getSize()];
498 for (int i = 0; i < nNames; i++) {
499 //int type = getGeneralizedType(namesArray[i]);
500 int type = m_expandedNameTable.getExpandedTypeID(uris[i], names[i], types[i], false);
501 result[type] = type;
502 }
503 return result;
504 }
505
506 /**
507 * Returns the internal type associated with an expanded QName
508 */
509 public int getGeneralizedType(final String name) {
510 return getGeneralizedType(name, true);
511 }
512
513 /**
514 * Returns the internal type associated with an expanded QName
515 */
516 public int getGeneralizedType(final String name, boolean searchOnly) {
517 String lName, ns = null;
518 int index = -1;
519 int code;
520
521 // Is there a prefix?
522 if ((index = name.lastIndexOf(":"))> -1) {
523 ns = name.substring(0, index);
524 }
1213 case Axis.NAMESPACE:
1214 return new TypedNamespaceIterator(type);
1215 case Axis.ROOT:
1216 return new TypedRootIterator(type);
1217 default:
1218 BasisLibrary.runTimeError(BasisLibrary.TYPED_AXIS_SUPPORT_ERR,
1219 Axis.getNames(axis));
1220 }
1221 return null;
1222 }
1223
1224 /**
1225 * Do not think that this returns an iterator for the namespace axis.
1226 * It returns an iterator with nodes that belong in a certain namespace,
1227 * such as with <xsl:apply-templates select="blob/foo:*"/>
1228 * The 'axis' specifies the axis for the base iterator from which the
1229 * nodes are taken, while 'ns' specifies the namespace URI type.
1230 */
1231 public DTMAxisIterator getNamespaceAxisIterator(int axis, int ns)
1232 {
1233
1234 DTMAxisIterator iterator = null;
1235
1236 if (ns == NO_TYPE) {
1237 return EMPTYITERATOR;
1238 }
1239 else {
1240 switch (axis) {
1241 case Axis.CHILD:
1242 return new NamespaceChildrenIterator(ns);
1243 case Axis.ATTRIBUTE:
1244 return new NamespaceAttributeIterator(ns);
1245 default:
1246 return new NamespaceWildcardIterator(axis, ns);
1247 }
1248 }
1249 }
1250
1251 /**
1252 * Iterator that handles node tests that test for a namespace, but have
1253 * a wild card for the local name of the node, i.e., node tests of the
1254 * form <axis>::<prefix>:*
1255 */
1529 _currentNode = nextNode;
1530
1531 return returnNode(node);
1532 }
1533 } // end of NamespaceAttributeIterator
1534
1535 /**
1536 * Returns an iterator with all descendants of a node that are of
1537 * a given type.
1538 */
1539 public DTMAxisIterator getTypedDescendantIterator(int type)
1540 {
1541 return new TypedDescendantIterator(type);
1542 }
1543
1544 /**
1545 * Returns the nth descendant of a node
1546 */
1547 public DTMAxisIterator getNthDescendant(int type, int n, boolean includeself)
1548 {
1549 DTMAxisIterator source = (DTMAxisIterator) new TypedDescendantIterator(type);
1550 return new NthDescendantIterator(n);
1551 }
1552
1553 /**
1554 * Copy the string value of a node directly to an output handler
1555 */
1556 public void characters(final int node, SerializationHandler handler)
1557 throws TransletException
1558 {
1559 if (node != DTM.NULL) {
1560 try {
1561 dispatchCharactersEvents(node, handler, false);
1562 } catch (SAXException e) {
1563 throw new TransletException(e);
1564 }
1565 }
1566 }
1567
1568 /**
1569 * Copy a node-set to an output handler
1865
1866 if (entity == null) {
1867 return uri;
1868 }
1869
1870 String notationName = entity.getNotationName();
1871 if (notationName != null) {
1872 uri = entity.getSystemId();
1873 if (uri == null) {
1874 uri = entity.getPublicId();
1875 }
1876 }
1877 }
1878 return uri;
1879 }
1880 else {
1881 return super.getUnparsedEntityURI(name);
1882 }
1883 }
1884
1885 }
|
1 /*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xalan.internal.xsltc.dom;
22
23 import com.sun.org.apache.xalan.internal.xsltc.DOM;
24 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
25 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
26 import com.sun.org.apache.xalan.internal.xsltc.TransletException;
27 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
28 import com.sun.org.apache.xml.internal.dtm.Axis;
29 import com.sun.org.apache.xml.internal.dtm.DTM;
30 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
31 import com.sun.org.apache.xml.internal.dtm.DTMManager;
32 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
33 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIterNodeList;
34 import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase;
35 import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;
36 import com.sun.org.apache.xml.internal.dtm.ref.EmptyIterator;
37 import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2DTM2;
38 import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
39 import com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler;
62 * information. They are used when the processing behavior between DOM and
63 * SAX has to be different. Examples of these include id function and
64 * unparsed entity.
65 *
66 * <p>SAXImpl extends SAX2DTM2 instead of SAX2DTM for better performance.
67 * @author Jacek Ambroziak
68 * @author Santiago Pericas-Geertsen
69 * @author Morten Jorgensen
70 * @author Douglas Sellers <douglasjsellers@hotmail.com>
71 */
72 public final class SAXImpl extends SAX2DTM2
73 implements DOMEnhancedForDTM, DOMBuilder
74 {
75
76 /* ------------------------------------------------------------------- */
77 /* DOMBuilder fields BEGIN */
78 /* ------------------------------------------------------------------- */
79
80 // Namespace prefix-to-uri mapping stuff
81 private int _uriCount = 0;
82 // private int _prefixCount = 0;
83
84 // Stack used to keep track of what whitespace text nodes are protected
85 // by xml:space="preserve" attributes and which nodes that are not.
86 private int[] _xmlSpaceStack;
87 private int _idx = 1;
88 private boolean _preserve = false;
89
90 // private static final String XML_STRING = "xml:";
91 private static final String XML_PREFIX = "xml";
92 private static final String XMLSPACE_STRING = "xml:space";
93 private static final String PRESERVE_STRING = "preserve";
94 // private static final String XMLNS_PREFIX = "xmlns";
95 private static final String XML_URI = "http://www.w3.org/XML/1998/namespace";
96
97 private boolean _escaping = true;
98 private boolean _disableEscaping = false;
99 private int _textNodeToProcess = DTM.NULL;
100
101 /* ------------------------------------------------------------------- */
102 /* DOMBuilder fields END */
103 /* ------------------------------------------------------------------- */
104
105 // empty String for null attribute values
106 private final static String EMPTYSTRING = "";
107
108 // empty iterator to be returned when there are no children
109 private final static DTMAxisIterator EMPTYITERATOR = EmptyIterator.getInstance();
110 // The number of expanded names
111 private int _namesSize = -1;
112
113 // Namespace related stuff
114 private Map<Integer, Integer> _nsIndex = new HashMap<>();
115
116 // The initial size of the text buffer
117 private int _size = 0;
118
119 // Tracks which textnodes are not escaped
120 private BitArray _dontEscape = null;
121
122 // The URI to this document
123 // private String _documentURI = null;
124 static private int _documentURIIndex = 0;
125
126 // The owner Document when the input source is DOMSource.
127 private Document _document;
128
129 // The Map for org.w3c.dom.Node to node id mapping.
130 // This is only used when the input is a DOMSource and the
131 // buildIdIndex flag is true.
132 private Map<Node, Integer> _node2Ids = null;
133
134 // True if the input source is a DOMSource.
135 private boolean _hasDOMSource = false;
136
137 // The DTMManager
138 private XSLTCDTMManager _dtmManager;
139
140 // Support for access/navigation through org.w3c.dom API
141 private Node[] _nodes;
142 private NodeList[] _nodeLists;
143 // private final static String XML_LANG_ATTRIBUTE = "http://www.w3.org/XML/1998/namespace:@lang";
144
145 /**
146 * Define the origin of the document from which the tree was built
147 */
148 public void setDocumentURI(String uri) {
149 if (uri != null) {
150 setDocumentBaseURI(SystemIDResolver.getAbsoluteURI(uri));
151 }
152 }
153
154 /**
155 * Returns the origin of the document from which the tree was built
156 */
157 public String getDocumentURI() {
158 String baseURI = getDocumentBaseURI();
159 return (baseURI != null) ? baseURI : "rtf" + _documentURIIndex++;
160 }
161
162 public String getDocumentURI(int node) {
163 return getDocumentURI();
470 if (s == null) {
471 return 0;
472 }
473 int eType = getIdForNamespace(s);
474 return _nsIndex.get(new Integer(eType));
475 }
476
477
478
479 /**
480 * Returns the namespace type of a specific node
481 */
482 public int getNamespaceType(final int node)
483 {
484 return super.getNamespaceType(node);
485 }
486
487 /**
488 * Sets up a translet-to-dom type mapping table
489 */
490 /*
491 private int[] setupMapping(String[] names, String[] uris, int[] types, int nNames) {
492 // Padding with number of names, because they
493 // may need to be added, i.e for RTFs. See copy03
494 final int[] result = new int[m_expandedNameTable.getSize()];
495 for (int i = 0; i < nNames; i++) {
496 //int type = getGeneralizedType(namesArray[i]);
497 int type = m_expandedNameTable.getExpandedTypeID(uris[i], names[i], types[i], false);
498 result[type] = type;
499 }
500 return result;
501 }
502 */
503
504 /**
505 * Returns the internal type associated with an expanded QName
506 */
507 public int getGeneralizedType(final String name) {
508 return getGeneralizedType(name, true);
509 }
510
511 /**
512 * Returns the internal type associated with an expanded QName
513 */
514 public int getGeneralizedType(final String name, boolean searchOnly) {
515 String lName, ns = null;
516 int index = -1;
517 int code;
518
519 // Is there a prefix?
520 if ((index = name.lastIndexOf(":"))> -1) {
521 ns = name.substring(0, index);
522 }
1211 case Axis.NAMESPACE:
1212 return new TypedNamespaceIterator(type);
1213 case Axis.ROOT:
1214 return new TypedRootIterator(type);
1215 default:
1216 BasisLibrary.runTimeError(BasisLibrary.TYPED_AXIS_SUPPORT_ERR,
1217 Axis.getNames(axis));
1218 }
1219 return null;
1220 }
1221
1222 /**
1223 * Do not think that this returns an iterator for the namespace axis.
1224 * It returns an iterator with nodes that belong in a certain namespace,
1225 * such as with <xsl:apply-templates select="blob/foo:*"/>
1226 * The 'axis' specifies the axis for the base iterator from which the
1227 * nodes are taken, while 'ns' specifies the namespace URI type.
1228 */
1229 public DTMAxisIterator getNamespaceAxisIterator(int axis, int ns)
1230 {
1231 if (ns == NO_TYPE) {
1232 return EMPTYITERATOR;
1233 }
1234 else {
1235 switch (axis) {
1236 case Axis.CHILD:
1237 return new NamespaceChildrenIterator(ns);
1238 case Axis.ATTRIBUTE:
1239 return new NamespaceAttributeIterator(ns);
1240 default:
1241 return new NamespaceWildcardIterator(axis, ns);
1242 }
1243 }
1244 }
1245
1246 /**
1247 * Iterator that handles node tests that test for a namespace, but have
1248 * a wild card for the local name of the node, i.e., node tests of the
1249 * form <axis>::<prefix>:*
1250 */
1524 _currentNode = nextNode;
1525
1526 return returnNode(node);
1527 }
1528 } // end of NamespaceAttributeIterator
1529
1530 /**
1531 * Returns an iterator with all descendants of a node that are of
1532 * a given type.
1533 */
1534 public DTMAxisIterator getTypedDescendantIterator(int type)
1535 {
1536 return new TypedDescendantIterator(type);
1537 }
1538
1539 /**
1540 * Returns the nth descendant of a node
1541 */
1542 public DTMAxisIterator getNthDescendant(int type, int n, boolean includeself)
1543 {
1544 return new NthDescendantIterator(n);
1545 }
1546
1547 /**
1548 * Copy the string value of a node directly to an output handler
1549 */
1550 public void characters(final int node, SerializationHandler handler)
1551 throws TransletException
1552 {
1553 if (node != DTM.NULL) {
1554 try {
1555 dispatchCharactersEvents(node, handler, false);
1556 } catch (SAXException e) {
1557 throw new TransletException(e);
1558 }
1559 }
1560 }
1561
1562 /**
1563 * Copy a node-set to an output handler
1859
1860 if (entity == null) {
1861 return uri;
1862 }
1863
1864 String notationName = entity.getNotationName();
1865 if (notationName != null) {
1866 uri = entity.getSystemId();
1867 if (uri == null) {
1868 uri = entity.getPublicId();
1869 }
1870 }
1871 }
1872 return uri;
1873 }
1874 else {
1875 return super.getUnparsedEntityURI(name);
1876 }
1877 }
1878
1879 public void release() {
1880 _dtmManager.release(this, true);
1881 }
1882 }
|