1 /* |
1 /* |
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
39 |
39 |
40 /** |
40 /** |
41 * A JAXP {@link javax.xml.transform.Source} implementation that wraps |
41 * A JAXP {@link javax.xml.transform.Source} implementation that wraps |
42 * the specified {@link javax.xml.stream.XMLStreamReader} or |
42 * the specified {@link javax.xml.stream.XMLStreamReader} or |
43 * {@link javax.xml.stream.XMLEventReader} for use by applications that |
43 * {@link javax.xml.stream.XMLEventReader} for use by applications that |
44 * expext a {@link javax.xml.transform.Source}. |
44 * expect a {@link javax.xml.transform.Source}. |
45 * |
45 * |
46 * <p> |
46 * <p> |
47 * The fact that StAXSource derives from SAXSource is an implementation |
47 * The fact that StAXSource derives from SAXSource is an implementation |
48 * detail. Thus in general applications are strongly discouraged from |
48 * detail. Thus in general applications are strongly discouraged from |
49 * accessing methods defined on SAXSource. In particular: |
49 * accessing methods defined on SAXSource. In particular: |
64 * <pre> |
64 * <pre> |
65 // create a StAXSource |
65 // create a StAXSource |
66 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileReader(args[0])); |
66 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileReader(args[0])); |
67 Source staxSource = new StAXSource(reader); |
67 Source staxSource = new StAXSource(reader); |
68 |
68 |
69 // createa StreamResult |
69 // create a StreamResult |
70 Result streamResult = new StreamResult(System.out); |
70 Result streamResult = new StreamResult(System.out); |
71 |
71 |
72 // run the transform |
72 // run the transform |
73 TransformerFactory.newInstance().newTransformer().transform(staxSource, streamResult); |
73 TransformerFactory.newInstance().newTransformer().transform(staxSource, streamResult); |
74 * </pre> |
74 * </pre> |
85 private final XMLStreamReader staxReader; |
85 private final XMLStreamReader staxReader; |
86 |
86 |
87 // SAX allows ContentHandler to be changed during the parsing, |
87 // SAX allows ContentHandler to be changed during the parsing, |
88 // but JAXB doesn't. So this repeater will sit between those |
88 // but JAXB doesn't. So this repeater will sit between those |
89 // two components. |
89 // two components. |
90 private XMLFilterImpl repeater = new XMLFilterImpl(); |
90 private final XMLFilterImpl repeater = new XMLFilterImpl(); |
91 |
91 |
92 // this object will pretend as an XMLReader. |
92 // this object will pretend as an XMLReader. |
93 // no matter what parameter is specified to the parse method, |
93 // no matter what parameter is specified to the parse method, |
94 // it will just read from the StAX reader. |
94 // it will just read from the StAX reader. |
95 private final XMLReader pseudoParser = new XMLReader() { |
95 private final XMLReader pseudoParser = new XMLReader() { |
|
96 |
|
97 @Override |
96 public boolean getFeature(String name) throws SAXNotRecognizedException { |
98 public boolean getFeature(String name) throws SAXNotRecognizedException { |
97 throw new SAXNotRecognizedException(name); |
99 throw new SAXNotRecognizedException(name); |
98 } |
100 } |
99 |
101 |
|
102 @Override |
100 public void setFeature(String name, boolean value) throws SAXNotRecognizedException { |
103 public void setFeature(String name, boolean value) throws SAXNotRecognizedException { |
101 // Should support these two features according to XMLReader javadoc. |
104 // Should support these two features according to XMLReader javadoc. |
102 if (name.equals("http://xml.org/sax/features/namespaces") && value) { |
105 if (name.equals("http://xml.org/sax/features/namespaces") && value) { |
103 // Ignore for now |
106 // Ignore for now |
104 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) { |
107 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) { |
106 } else { |
109 } else { |
107 throw new SAXNotRecognizedException(name); |
110 throw new SAXNotRecognizedException(name); |
108 } |
111 } |
109 } |
112 } |
110 |
113 |
|
114 @Override |
111 public Object getProperty(String name) throws SAXNotRecognizedException { |
115 public Object getProperty(String name) throws SAXNotRecognizedException { |
112 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
116 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
113 return lexicalHandler; |
117 return lexicalHandler; |
114 } |
118 } |
115 throw new SAXNotRecognizedException(name); |
119 throw new SAXNotRecognizedException(name); |
116 } |
120 } |
117 |
121 |
|
122 @Override |
118 public void setProperty(String name, Object value) throws SAXNotRecognizedException { |
123 public void setProperty(String name, Object value) throws SAXNotRecognizedException { |
119 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
124 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
120 this.lexicalHandler = (LexicalHandler)value; |
125 this.lexicalHandler = (LexicalHandler)value; |
121 return; |
126 return; |
122 } |
127 } |
125 |
130 |
126 private LexicalHandler lexicalHandler; |
131 private LexicalHandler lexicalHandler; |
127 |
132 |
128 // we will store this value but never use it by ourselves. |
133 // we will store this value but never use it by ourselves. |
129 private EntityResolver entityResolver; |
134 private EntityResolver entityResolver; |
|
135 |
|
136 @Override |
130 public void setEntityResolver(EntityResolver resolver) { |
137 public void setEntityResolver(EntityResolver resolver) { |
131 this.entityResolver = resolver; |
138 this.entityResolver = resolver; |
132 } |
139 } |
|
140 |
|
141 @Override |
133 public EntityResolver getEntityResolver() { |
142 public EntityResolver getEntityResolver() { |
134 return entityResolver; |
143 return entityResolver; |
135 } |
144 } |
136 |
145 |
137 private DTDHandler dtdHandler; |
146 private DTDHandler dtdHandler; |
|
147 |
|
148 @Override |
138 public void setDTDHandler(DTDHandler handler) { |
149 public void setDTDHandler(DTDHandler handler) { |
139 this.dtdHandler = handler; |
150 this.dtdHandler = handler; |
140 } |
151 } |
|
152 @Override |
141 public DTDHandler getDTDHandler() { |
153 public DTDHandler getDTDHandler() { |
142 return dtdHandler; |
154 return dtdHandler; |
143 } |
155 } |
144 |
156 |
|
157 @Override |
145 public void setContentHandler(ContentHandler handler) { |
158 public void setContentHandler(ContentHandler handler) { |
146 repeater.setContentHandler(handler); |
159 repeater.setContentHandler(handler); |
147 } |
160 } |
|
161 |
|
162 @Override |
148 public ContentHandler getContentHandler() { |
163 public ContentHandler getContentHandler() { |
149 return repeater.getContentHandler(); |
164 return repeater.getContentHandler(); |
150 } |
165 } |
151 |
166 |
152 private ErrorHandler errorHandler; |
167 private ErrorHandler errorHandler; |
|
168 |
|
169 @Override |
153 public void setErrorHandler(ErrorHandler handler) { |
170 public void setErrorHandler(ErrorHandler handler) { |
154 this.errorHandler = handler; |
171 this.errorHandler = handler; |
155 } |
172 } |
|
173 @Override |
156 public ErrorHandler getErrorHandler() { |
174 public ErrorHandler getErrorHandler() { |
157 return errorHandler; |
175 return errorHandler; |
158 } |
176 } |
159 |
177 |
|
178 @Override |
160 public void parse(InputSource input) throws SAXException { |
179 public void parse(InputSource input) throws SAXException { |
161 parse(); |
180 parse(); |
162 } |
181 } |
163 |
182 |
|
183 @Override |
164 public void parse(String systemId) throws SAXException { |
184 public void parse(String systemId) throws SAXException { |
165 parse(); |
185 parse(); |
166 } |
186 } |
167 |
187 |
168 public void parse() throws SAXException { |
188 public void parse() throws SAXException { |