1 /*
2 * Copyright (c) 2012, 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: XSLTC.java,v 1.2.4.1 2005/09/05 09:51:38 pvedula Exp $
22 */
23
24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
25
26 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
27 import com.sun.org.apache.xalan.internal.XalanConstants;
28 import com.sun.org.apache.xalan.internal.utils.FeatureManager;
29 import com.sun.org.apache.xalan.internal.utils.FeatureManager.Feature;
30 import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
31 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
34 import com.sun.org.apache.xml.internal.dtm.DTM;
35 import java.io.BufferedOutputStream;
36 import java.io.ByteArrayOutputStream;
37 import java.io.File;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.net.URL;
42 import java.security.AccessController;
43 import java.security.PrivilegedAction;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.Map;
49 import java.util.Properties;
50 import java.util.Objects;
51 import java.util.Vector;
52 import java.util.jar.JarEntry;
53 import java.util.jar.JarOutputStream;
54 import java.util.jar.Manifest;
55 import javax.xml.XMLConstants;
56 import org.xml.sax.InputSource;
57 import org.xml.sax.XMLReader;
58
59 /**
60 * @author Jacek Ambroziak
61 * @author Santiago Pericas-Geertsen
62 * @author G. Todd Miller
63 * @author Morten Jorgensen
64 * @author John Howard (johnh@schemasoft.com)
65 */
66 public final class XSLTC {
67
68 // A reference to the main stylesheet parser object.
69 private Parser _parser;
70
71 // A reference to an external XMLReader (SAX parser) passed to us
72 private XMLReader _reader = null;
73
74 // A reference to an external SourceLoader (for use with include/import)
75 private SourceLoader _loader = null;
84 private int _stepPatternSerial = 1;
85 private int _helperClassSerial = 0;
86 private int _attributeSetSerial = 0;
87
88 private int[] _numberFieldIndexes;
89
90 // Name index tables
91 private int _nextGType; // Next available element type
92 private Vector _namesIndex; // Index of all registered QNames
93 private Map<String, Integer> _elements; // Map of all registered elements
94 private Map<String, Integer> _attributes; // Map of all registered attributes
95
96 // Namespace index tables
97 private int _nextNSType; // Next available namespace type
98 private Vector _namespaceIndex; // Index of all registered namespaces
99 private Map<String, Integer> _namespaces; // Map of all registered namespaces
100 private Map<String, Integer> _namespacePrefixes;// Map of all registered namespace prefixes
101
102
103 // All literal text in the stylesheet
104 private Vector m_characterData;
105
106 // These define the various methods for outputting the translet
107 public static final int FILE_OUTPUT = 0;
108 public static final int JAR_OUTPUT = 1;
109 public static final int BYTEARRAY_OUTPUT = 2;
110 public static final int CLASSLOADER_OUTPUT = 3;
111 public static final int BYTEARRAY_AND_FILE_OUTPUT = 4;
112 public static final int BYTEARRAY_AND_JAR_OUTPUT = 5;
113
114
115 // Compiler options (passed from command line or XSLTC client)
116 private boolean _debug = false; // -x
117 private String _jarFileName = null; // -j <jar-file-name>
118 private String _className = null; // -o <class-name>
119 private String _packageName = "die.verwandlung"; // override with -p <package-name>
120 private File _destDir = null; // -d <directory-name>
121 private int _outputType = FILE_OUTPUT; // by default
122
123 private Vector _classes;
124 private Vector _bcelClasses;
135 private boolean _templateInlining = false;
136
137 /**
138 * State of the secure processing feature.
139 */
140 private boolean _isSecureProcessing = false;
141
142 private boolean _useServicesMechanism = true;
143
144 /**
145 * protocols allowed for external references set by the stylesheet processing instruction, Import and Include element.
146 */
147 private String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
148 /**
149 * protocols allowed for external DTD references in source file and/or stylesheet.
150 */
151 private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
152
153 private XMLSecurityManager _xmlSecurityManager;
154
155 private final FeatureManager _featureManager;
156
157 /**
158 * Extension function class loader variables
159 */
160
161 /* Class loader reference that will be used to external extension functions loading */
162 private ClassLoader _extensionClassLoader;
163
164 /**
165 * HashSet with the loaded classes
166 */
167 private final Map<String, Class> _externalExtensionFunctions;
168
169 /**
170 * XSLTC compiler constructor
171 */
172 public XSLTC(boolean useServicesMechanism, FeatureManager featureManager) {
173 _parser = new Parser(this, useServicesMechanism);
174 _featureManager = featureManager;
175 _extensionClassLoader = null;
176 _externalExtensionFunctions = new HashMap<>();
177 }
178
179 /**
180 * Set the state of the secure processing feature.
181 */
182 public void setSecureProcessing(boolean flag) {
183 _isSecureProcessing = flag;
184 }
185
186 /**
187 * Return the state of the secure processing feature.
188 */
189 public boolean isSecureProcessing() {
190 return _isSecureProcessing;
191 }
192 /**
193 * Return the state of the services mechanism feature.
194 */
195 public boolean useServicesMechnism() {
196 return _useServicesMechanism;
197 }
198
199 /**
200 * Set the state of the services mechanism feature.
201 */
202 public void setServicesMechnism(boolean flag) {
203 _useServicesMechanism = flag;
204 }
205
206 /**
207 * Return the value of the specified feature
208 * @param name name of the feature
209 * @return true if the feature is enabled, false otherwise
210 */
211 public boolean getFeature(Feature name) {
212 return _featureManager.isFeatureEnabled(name);
213 }
214
215 /**
216 * Return allowed protocols for accessing external stylesheet.
217 */
218 public Object getProperty(String name) {
219 if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
220 return _accessExternalStylesheet;
221 }
222 else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) {
223 return _accessExternalDTD;
224 } else if (name.equals(XalanConstants.SECURITY_MANAGER)) {
225 return _xmlSecurityManager;
226 } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
227 return _extensionClassLoader;
228 }
229 return null;
230 }
231
232 /**
233 * Set allowed protocols for accessing external stylesheet.
234 */
235 public void setProperty(String name, Object value) {
236 if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
237 _accessExternalStylesheet = (String)value;
238 }
239 else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) {
240 _accessExternalDTD = (String)value;
241 } else if (name.equals(XalanConstants.SECURITY_MANAGER)) {
242 _xmlSecurityManager = (XMLSecurityManager)value;
243 } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
244 _extensionClassLoader = (ClassLoader) value;
245 /* Clear the external extension functions HashMap if extension class
246 loader was changed */
247 _externalExtensionFunctions.clear();
248 }
249 }
250
251 /**
252 * Only for user by the internal TrAX implementation.
253 */
254 public Parser getParser() {
255 return _parser;
256 }
257
258 /**
259 * Only for user by the internal TrAX implementation.
260 */
261 public void setOutputType(int type) {
262 _outputType = type;
263 }
264
265 /**
266 * Only for user by the internal TrAX implementation.
267 */
587
588 /**
589 * Set the XMLReader to use for parsing the next input stylesheet
590 * @param reader XMLReader (SAX2 parser) to use
591 */
592 public void setXMLReader(XMLReader reader) {
593 _reader = reader;
594 }
595
596 /**
597 * Get the XMLReader to use for parsing the next input stylesheet
598 */
599 public XMLReader getXMLReader() {
600 return _reader ;
601 }
602
603 /**
604 * Get a Vector containing all compile error messages
605 * @return A Vector containing all compile error messages
606 */
607 public Vector getErrors() {
608 return _parser.getErrors();
609 }
610
611 /**
612 * Get a Vector containing all compile warning messages
613 * @return A Vector containing all compile error messages
614 */
615 public Vector getWarnings() {
616 return _parser.getWarnings();
617 }
618
619 /**
620 * Print all compile error messages to standard output
621 */
622 public void printErrors() {
623 _parser.printErrors();
624 }
625
626 /**
627 * Print all compile warning messages to standard output
628 */
629 public void printWarnings() {
630 _parser.printWarnings();
631 }
632
633 /**
634 * This method is called by the XPathParser when it encounters a call
635 * to the document() function. Affects the DOM used by the translet.
974 _debug = debug;
975 }
976
977 /**
978 * Get current debugging message setting
979 */
980 public boolean debug() {
981 return _debug;
982 }
983
984
985 /**
986 * Retrieve a string representation of the character data to be stored
987 * in the translet as a <code>char[]</code>. There may be more than
988 * one such array required.
989 * @param index The index of the <code>char[]</code>. Zero-based.
990 * @return String The character data to be stored in the corresponding
991 * <code>char[]</code>.
992 */
993 public String getCharacterData(int index) {
994 return ((StringBuffer) m_characterData.elementAt(index)).toString();
995 }
996
997 /**
998 * Get the number of char[] arrays, thus far, that will be created to
999 * store literal text in the stylesheet.
1000 */
1001 public int getCharacterDataCount() {
1002 return (m_characterData != null) ? m_characterData.size() : 0;
1003 }
1004
1005 /**
1006 * Add literal text to char arrays that will be used to store character
1007 * data in the stylesheet.
1008 * @param newData String data to be added to char arrays.
1009 * Pre-condition: <code>newData.length() ≤ 21845</code>
1010 * @return int offset at which character data will be stored
1011 */
1012 public int addCharacterData(String newData) {
1013 StringBuffer currData;
1014 if (m_characterData == null) {
1015 m_characterData = new Vector();
1016 currData = new StringBuffer();
1017 m_characterData.addElement(currData);
1018 } else {
1019 currData = (StringBuffer) m_characterData
1020 .elementAt(m_characterData.size()-1);
1021 }
1022
1023 // Character data could take up to three-times as much space when
1024 // written to the class file as UTF-8. The maximum size for a
1025 // constant is 65535/3. If we exceed that,
1026 // (We really should use some "bin packing".)
1027 if (newData.length() + currData.length() > 21845) {
1028 currData = new StringBuffer();
1029 m_characterData.addElement(currData);
1030 }
1031
1032 int newDataOffset = currData.length();
1033 currData.append(newData);
1034
1035 return newDataOffset;
1036 }
1037 }
|
1 /*
2 * Copyright (c) 2012, 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 * $Id: XSLTC.java,v 1.2.4.1 2005/09/05 09:51:38 pvedula Exp $
22 */
23
24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
25
26 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
27 import com.sun.org.apache.xalan.internal.XalanConstants;
28 import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
29 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
32 import com.sun.org.apache.xml.internal.dtm.DTM;
33 import java.io.BufferedOutputStream;
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.URL;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Date;
43 import java.util.Enumeration;
44 import java.util.HashMap;
45 import java.util.Map;
46 import java.util.Objects;
47 import java.util.Properties;
48 import java.util.Vector;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarOutputStream;
51 import java.util.jar.Manifest;
52 import javax.xml.XMLConstants;
53 import javax.xml.catalog.CatalogFeatures;
54 import jdk.xml.internal.JdkXmlFeatures;
55 import jdk.xml.internal.JdkXmlUtils;
56 import org.xml.sax.InputSource;
57 import org.xml.sax.XMLReader;
58
59 /**
60 * @author Jacek Ambroziak
61 * @author Santiago Pericas-Geertsen
62 * @author G. Todd Miller
63 * @author Morten Jorgensen
64 * @author John Howard (johnh@schemasoft.com)
65 */
66 public final class XSLTC {
67
68 // A reference to the main stylesheet parser object.
69 private Parser _parser;
70
71 // A reference to an external XMLReader (SAX parser) passed to us
72 private XMLReader _reader = null;
73
74 // A reference to an external SourceLoader (for use with include/import)
75 private SourceLoader _loader = null;
84 private int _stepPatternSerial = 1;
85 private int _helperClassSerial = 0;
86 private int _attributeSetSerial = 0;
87
88 private int[] _numberFieldIndexes;
89
90 // Name index tables
91 private int _nextGType; // Next available element type
92 private Vector _namesIndex; // Index of all registered QNames
93 private Map<String, Integer> _elements; // Map of all registered elements
94 private Map<String, Integer> _attributes; // Map of all registered attributes
95
96 // Namespace index tables
97 private int _nextNSType; // Next available namespace type
98 private Vector _namespaceIndex; // Index of all registered namespaces
99 private Map<String, Integer> _namespaces; // Map of all registered namespaces
100 private Map<String, Integer> _namespacePrefixes;// Map of all registered namespace prefixes
101
102
103 // All literal text in the stylesheet
104 private ArrayList<StringBuilder> m_characterData;
105
106 // These define the various methods for outputting the translet
107 public static final int FILE_OUTPUT = 0;
108 public static final int JAR_OUTPUT = 1;
109 public static final int BYTEARRAY_OUTPUT = 2;
110 public static final int CLASSLOADER_OUTPUT = 3;
111 public static final int BYTEARRAY_AND_FILE_OUTPUT = 4;
112 public static final int BYTEARRAY_AND_JAR_OUTPUT = 5;
113
114
115 // Compiler options (passed from command line or XSLTC client)
116 private boolean _debug = false; // -x
117 private String _jarFileName = null; // -j <jar-file-name>
118 private String _className = null; // -o <class-name>
119 private String _packageName = "die.verwandlung"; // override with -p <package-name>
120 private File _destDir = null; // -d <directory-name>
121 private int _outputType = FILE_OUTPUT; // by default
122
123 private Vector _classes;
124 private Vector _bcelClasses;
135 private boolean _templateInlining = false;
136
137 /**
138 * State of the secure processing feature.
139 */
140 private boolean _isSecureProcessing = false;
141
142 private boolean _useServicesMechanism = true;
143
144 /**
145 * protocols allowed for external references set by the stylesheet processing instruction, Import and Include element.
146 */
147 private String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
148 /**
149 * protocols allowed for external DTD references in source file and/or stylesheet.
150 */
151 private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
152
153 private XMLSecurityManager _xmlSecurityManager;
154
155 private final JdkXmlFeatures _xmlFeatures;
156
157 /**
158 * Extension function class loader variables
159 */
160
161 /* Class loader reference that will be used to external extension functions loading */
162 private ClassLoader _extensionClassLoader;
163
164 /**
165 * HashSet with the loaded classes
166 */
167 private final Map<String, Class> _externalExtensionFunctions;
168
169 /**
170 * Catalog features
171 */
172 CatalogFeatures _catalogFeatures;
173
174 /**
175 * XSLTC compiler constructor
176 */
177 public XSLTC(boolean useServicesMechanism, JdkXmlFeatures featureManager) {
178 _parser = new Parser(this, useServicesMechanism);
179 _xmlFeatures = featureManager;
180 _extensionClassLoader = null;
181 _externalExtensionFunctions = new HashMap<>();
182 }
183
184 /**
185 * Set the state of the secure processing feature.
186 */
187 public void setSecureProcessing(boolean flag) {
188 _isSecureProcessing = flag;
189 }
190
191 /**
192 * Return the state of the secure processing feature.
193 */
194 public boolean isSecureProcessing() {
195 return _isSecureProcessing;
196 }
197 /**
198 * Return the state of the services mechanism feature.
199 */
200 public boolean useServicesMechnism() {
201 return _useServicesMechanism;
202 }
203
204 /**
205 * Set the state of the services mechanism feature.
206 */
207 public void setServicesMechnism(boolean flag) {
208 _useServicesMechanism = flag;
209 }
210
211 /**
212 * Return the value of the specified feature
213 * @param name name of the feature
214 * @return true if the feature is enabled, false otherwise
215 */
216 public boolean getFeature(JdkXmlFeatures.XmlFeature name) {
217 return _xmlFeatures.getFeature(name);
218 }
219
220 /**
221 * Return allowed protocols for accessing external stylesheet.
222 * @param name the name of the property
223 * @return the value of the property
224 */
225 public Object getProperty(String name) {
226 if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
227 return _accessExternalStylesheet;
228 }
229 else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) {
230 return _accessExternalDTD;
231 } else if (name.equals(XalanConstants.SECURITY_MANAGER)) {
232 return _xmlSecurityManager;
233 } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
234 return _extensionClassLoader;
235 } else if (JdkXmlFeatures.CATALOG_FEATURES.equals(name)) {
236 return _catalogFeatures;
237 }
238 return null;
239 }
240
241 /**
242 * Set allowed protocols for accessing external stylesheet.
243 * @param name the name of the property
244 * @param value the value of the property
245 */
246 public void setProperty(String name, Object value) {
247 if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
248 _accessExternalStylesheet = (String)value;
249 }
250 else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) {
251 _accessExternalDTD = (String)value;
252 } else if (name.equals(XalanConstants.SECURITY_MANAGER)) {
253 _xmlSecurityManager = (XMLSecurityManager)value;
254 } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
255 _extensionClassLoader = (ClassLoader) value;
256 /* Clear the external extension functions HashMap if extension class
257 loader was changed */
258 _externalExtensionFunctions.clear();
259 } else if (JdkXmlFeatures.CATALOG_FEATURES.equals(name)) {
260 _catalogFeatures = (CatalogFeatures)value;
261 }
262 }
263
264 /**
265 * Only for user by the internal TrAX implementation.
266 */
267 public Parser getParser() {
268 return _parser;
269 }
270
271 /**
272 * Only for user by the internal TrAX implementation.
273 */
274 public void setOutputType(int type) {
275 _outputType = type;
276 }
277
278 /**
279 * Only for user by the internal TrAX implementation.
280 */
600
601 /**
602 * Set the XMLReader to use for parsing the next input stylesheet
603 * @param reader XMLReader (SAX2 parser) to use
604 */
605 public void setXMLReader(XMLReader reader) {
606 _reader = reader;
607 }
608
609 /**
610 * Get the XMLReader to use for parsing the next input stylesheet
611 */
612 public XMLReader getXMLReader() {
613 return _reader ;
614 }
615
616 /**
617 * Get a Vector containing all compile error messages
618 * @return A Vector containing all compile error messages
619 */
620 public ArrayList<ErrorMsg> getErrors() {
621 return _parser.getErrors();
622 }
623
624 /**
625 * Get a Vector containing all compile warning messages
626 * @return A Vector containing all compile error messages
627 */
628 public ArrayList<ErrorMsg> getWarnings() {
629 return _parser.getWarnings();
630 }
631
632 /**
633 * Print all compile error messages to standard output
634 */
635 public void printErrors() {
636 _parser.printErrors();
637 }
638
639 /**
640 * Print all compile warning messages to standard output
641 */
642 public void printWarnings() {
643 _parser.printWarnings();
644 }
645
646 /**
647 * This method is called by the XPathParser when it encounters a call
648 * to the document() function. Affects the DOM used by the translet.
987 _debug = debug;
988 }
989
990 /**
991 * Get current debugging message setting
992 */
993 public boolean debug() {
994 return _debug;
995 }
996
997
998 /**
999 * Retrieve a string representation of the character data to be stored
1000 * in the translet as a <code>char[]</code>. There may be more than
1001 * one such array required.
1002 * @param index The index of the <code>char[]</code>. Zero-based.
1003 * @return String The character data to be stored in the corresponding
1004 * <code>char[]</code>.
1005 */
1006 public String getCharacterData(int index) {
1007 return (m_characterData.get(index)).toString();
1008 }
1009
1010 /**
1011 * Get the number of char[] arrays, thus far, that will be created to
1012 * store literal text in the stylesheet.
1013 */
1014 public int getCharacterDataCount() {
1015 return (m_characterData != null) ? m_characterData.size() : 0;
1016 }
1017
1018 /**
1019 * Add literal text to char arrays that will be used to store character
1020 * data in the stylesheet.
1021 * @param newData String data to be added to char arrays.
1022 * Pre-condition: <code>newData.length() ≤ 21845</code>
1023 * @return int offset at which character data will be stored
1024 */
1025 public int addCharacterData(String newData) {
1026 StringBuilder currData;
1027 if (m_characterData == null) {
1028 m_characterData = new ArrayList<>();
1029 currData = new StringBuilder();
1030 m_characterData.add(currData);
1031 } else {
1032 currData = m_characterData.get(m_characterData.size()-1);
1033 }
1034
1035 // Character data could take up to three-times as much space when
1036 // written to the class file as UTF-8. The maximum size for a
1037 // constant is 65535/3. If we exceed that,
1038 // (We really should use some "bin packing".)
1039 if (newData.length() + currData.length() > 21845) {
1040 currData = new StringBuilder();
1041 m_characterData.add(currData);
1042 }
1043
1044 int newDataOffset = currData.length();
1045 currData.append(newData);
1046
1047 return newDataOffset;
1048 }
1049 }
|