1 /* |
1 /* |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2014, 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 |
213 } catch (IOException e) { |
213 } catch (IOException e) { |
214 throw new RuntimeException("should not happen"); |
214 throw new RuntimeException("should not happen"); |
215 } |
215 } |
216 } |
216 } |
217 |
217 |
218 static final TransformerFactory transformerFactory = newTransformerFactory(); |
218 static final ContextClassloaderLocal<TransformerFactory> transformerFactory = new ContextClassloaderLocal<TransformerFactory>() { |
219 |
219 @Override |
220 static final SAXParserFactory saxParserFactory = newSAXParserFactory(true); |
220 protected TransformerFactory initialValue() throws Exception { |
221 |
221 return TransformerFactory.newInstance(); |
222 static { |
222 } |
223 saxParserFactory.setNamespaceAware(true); |
223 }; |
224 } |
224 |
|
225 static final ContextClassloaderLocal<SAXParserFactory> saxParserFactory = new ContextClassloaderLocal<SAXParserFactory>() { |
|
226 @Override |
|
227 protected SAXParserFactory initialValue() throws Exception { |
|
228 SAXParserFactory factory = SAXParserFactory.newInstance(); |
|
229 factory.setNamespaceAware(true); |
|
230 return factory; |
|
231 } |
|
232 }; |
225 |
233 |
226 /** |
234 /** |
227 * Creates a new identity transformer. |
235 * Creates a new identity transformer. |
228 */ |
236 */ |
229 public static Transformer newTransformer() { |
237 public static Transformer newTransformer() { |
230 try { |
238 try { |
231 return transformerFactory.newTransformer(); |
239 return transformerFactory.get().newTransformer(); |
232 } catch (TransformerConfigurationException tex) { |
240 } catch (TransformerConfigurationException tex) { |
233 throw new IllegalStateException("Unable to create a JAXP transformer"); |
241 throw new IllegalStateException("Unable to create a JAXP transformer"); |
234 } |
242 } |
235 } |
243 } |
236 |
244 |
241 T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException { |
249 T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException { |
242 if (src instanceof StreamSource) { |
250 if (src instanceof StreamSource) { |
243 // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing |
251 // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing |
244 // is not turned on by default |
252 // is not turned on by default |
245 StreamSource ssrc = (StreamSource) src; |
253 StreamSource ssrc = (StreamSource) src; |
246 TransformerHandler th = ((SAXTransformerFactory) transformerFactory).newTransformerHandler(); |
254 TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler(); |
247 th.setResult(result); |
255 th.setResult(result); |
248 XMLReader reader = saxParserFactory.newSAXParser().getXMLReader(); |
256 XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader(); |
249 reader.setContentHandler(th); |
257 reader.setContentHandler(th); |
250 reader.setProperty(LEXICAL_HANDLER_PROPERTY, th); |
258 reader.setProperty(LEXICAL_HANDLER_PROPERTY, th); |
251 reader.parse(toInputSource(ssrc)); |
259 reader.parse(toInputSource(ssrc)); |
252 } else { |
260 } else { |
253 newTransformer().transform(src, result); |
261 newTransformer().transform(src, result); |