bugged
stringlengths
6
599k
fixed
stringlengths
6
40.8M
__index_level_0__
int64
0
3.24M
getAlignedSizeRequirements(SizeRequirements[] children) { return null; // TODO }
getAlignedSizeRequirements(SizeRequirements[] children) { float minLeft = 0; float minRight = 0; float prefLeft = 0; float prefRight = 0; float maxLeft = 0; float maxRight = 0; for (int i = 0; i < children.length; i++) { float myMinLeft = children[i].minimum * children[i].alignment; float myMinRight = children[i].minimum - myMinLeft; minLeft = Math.max(myMinLeft, minLeft); minRight = Math.max(myMinRight, minRight); float myPrefLeft = children[i].preferred * children[i].alignment; float myPrefRight = children[i].preferred - myMinLeft; prefLeft = Math.max(myPrefLeft, prefLeft); prefRight = Math.max(myPrefRight, prefRight); float myMaxLeft = children[i].maximum * children[i].alignment; float myMaxRight = children[i].maximum - myMinLeft; maxLeft = Math.max(myMaxLeft, maxLeft); maxRight = Math.max(myMaxRight, maxRight); } int minSize = (int) (minLeft + minRight); int prefSize = (int) (prefLeft + prefRight); int maxSize = (int) (maxLeft + maxRight); return new SizeRequirements(minSize, prefSize, maxSize, 0.5F); // TODO }
15,311
TemplateNode clone(Stylesheet stylesheet) { return new ApplyImportsNode((children == null) ? null : children.clone(stylesheet), (next == null) ? null : next.clone(stylesheet)); }
TemplateNode clone(Stylesheet stylesheet) { return new ApplyImportsNode((children == null) ? null : children.clone(stylesheet), (next == null) ? null : next.clone(stylesheet)); }
15,312
public MappedBlockDeviceSupport(Device parent, long offset, long length) throws IOException { super(parent.getBus(), "mapped-" + parent.getId()); this.parent = parent; try { this.parentApi = (BlockDeviceAPI)parent.getAPI(BlockDeviceAPI.class); } catch (ApiNotFoundException ex) { throw new IOException("BlockDeviceAPI not found on parent device", ex); } this.offset = offset; this.length = length; if (offset < 0) { throw new IndexOutOfBoundsException("offset < 0"); } if (length < 0) { throw new IndexOutOfBoundsException("length < 0"); } if (offset + length > parentApi.getLength()) { throw new IndexOutOfBoundsException("offset + length > parent.length"); } registerAPI(BlockDeviceAPI.class, this); }
public MappedBlockDeviceSupport(Device parent, long offset, long length) throws IOException { super(parent.getBus(), "mapped-" + parent.getId()); this.parent = parent; try { this.parentApi = (BlockDeviceAPI)parent.getAPI(BlockDeviceAPI.class); } catch (ApiNotFoundException ex) { throw new IOException("BlockDeviceAPI not found on parent device", ex); } this.offset = offset; this.length = length; if (offset < 0) { throw new IndexOutOfBoundsException("offset < 0"); } if (length < 0) { throw new IndexOutOfBoundsException("length < 0"); } if (offset + length > parentApi.getLength()) { throw new IndexOutOfBoundsException("offset + length > parent.length"); } registerAPI(BlockDeviceAPI.class, this); }
15,314
public CharBuffer put (char[] src, int offset, int length) { checkArraySize(src.length, offset, length); checkForOverflow(length); for (int i = offset; i < offset + length; i++) put (src [i]); return this; }
public CharBuffer put (CharBuffer src) { checkArraySize(src.length, offset, length); checkForOverflow(length); for (int i = offset; i < offset + length; i++) put (src [i]); return this; }
15,315
public CharBuffer put (char[] src, int offset, int length) { checkArraySize(src.length, offset, length); checkForOverflow(length); for (int i = offset; i < offset + length; i++) put (src [i]); return this; }
public CharBuffer put (char[] src, int offset, int length) { checkArraySize(src.length, offset, length); checkForOverflow(length); for (int i = offset; i < offset + length; i++) put (src [i]); return this; }
15,316
final public static CharBuffer wrap(char[] array, int offset, int length) { return new CharBufferImpl(array, 0, array.length, offset + length, offset, -1, false); }
public static final CharBuffer wrap(char[] array, int offset, int length) { return new CharBufferImpl(array, 0, array.length, offset + length, offset, -1, false); }
15,317
public void installUI(JComponent c) { tree = (JTree) c; configureLayoutCache(); UIDefaults defaults = UIManager.getLookAndFeelDefaults(); tree.setFont(defaults.getFont("Tree.font")); tree.setForeground(defaults.getColor("Tree.foreground")); tree.setBackground(defaults.getColor("Tree.background")); tree.setOpaque(true); tree.setScrollsOnExpand(defaults.getBoolean("Tree.scrollsOnExpand")); rightChildIndent = defaults.getInt("Tree.rightChildIndent"); leftChildIndent = defaults.getInt("Tree.leftChildIndent"); setRowHeight(defaults.getInt("Tree.rowHeight")); tree.requestFocusInWindow(false); setExpandedIcon(defaults.getIcon("Tree.expandedIcon")); setCollapsedIcon(defaults.getIcon("Tree.collapsedIcon")); currentCellRenderer = createDefaultCellRenderer(); rendererPane = createCellRendererPane(); createdRenderer = true; setCellEditor(createDefaultCellEditor()); createdCellEditor = true; TreeModel mod = tree.getModel(); setModel(mod); tree.setRootVisible(true); if (mod != null) tree.expandPath(new TreePath(mod.getRoot())); treeSelectionModel = tree.getSelectionModel(); drawingCache = new Hashtable(); nodeDimensions = createNodeDimensions(); propertyChangeListener = createPropertyChangeListener(); focusListener = createFocusListener(); treeSelectionListener = createTreeSelectionListener(); mouseInputListener = new MouseInputHandler(null, null, null); keyListener = createKeyListener(); selectionModelPropertyChangeListener = createSelectionModelPropertyChangeListener(); componentListener = createComponentListener(); cellEditorListener = createCellEditorListener(); treeExpansionListener = createTreeExpansionListener(); treeModelListener = createTreeModelListener(); editingRow = -1; lastSelectedRow = -1; installKeyboardActions(); tree.addPropertyChangeListener(propertyChangeListener); tree.addFocusListener(focusListener); tree.addTreeSelectionListener(treeSelectionListener); tree.addMouseListener(mouseInputListener); tree.addKeyListener(keyListener); tree.addPropertyChangeListener(selectionModelPropertyChangeListener); tree.addComponentListener(componentListener); tree.addTreeExpansionListener(treeExpansionListener); if (treeModel != null) treeModel.addTreeModelListener(treeModelListener); completeUIInstall(); }
public void installUI(JComponent c) { tree = (JTree) c; configureLayoutCache(); UIDefaults defaults = UIManager.getLookAndFeelDefaults(); tree.setFont(defaults.getFont("Tree.font")); tree.setForeground(defaults.getColor("Tree.foreground")); tree.setBackground(defaults.getColor("Tree.background")); tree.setOpaque(true); tree.setScrollsOnExpand(defaults.getBoolean("Tree.scrollsOnExpand")); rightChildIndent = defaults.getInt("Tree.rightChildIndent"); leftChildIndent = defaults.getInt("Tree.leftChildIndent"); setRowHeight(defaults.getInt("Tree.rowHeight")); tree.requestFocusInWindow(false); setExpandedIcon(defaults.getIcon("Tree.expandedIcon")); setCollapsedIcon(defaults.getIcon("Tree.collapsedIcon")); currentCellRenderer = createDefaultCellRenderer(); rendererPane = createCellRendererPane(); createdRenderer = true; setCellEditor(createDefaultCellEditor()); createdCellEditor = true; TreeModel mod = tree.getModel(); setModel(mod); if (mod != null) tree.expandPath(new TreePath(mod.getRoot())); treeSelectionModel = tree.getSelectionModel(); drawingCache = new Hashtable(); nodeDimensions = createNodeDimensions(); propertyChangeListener = createPropertyChangeListener(); focusListener = createFocusListener(); treeSelectionListener = createTreeSelectionListener(); mouseInputListener = new MouseInputHandler(null, null, null); keyListener = createKeyListener(); selectionModelPropertyChangeListener = createSelectionModelPropertyChangeListener(); componentListener = createComponentListener(); cellEditorListener = createCellEditorListener(); treeExpansionListener = createTreeExpansionListener(); treeModelListener = createTreeModelListener(); editingRow = -1; lastSelectedRow = -1; installKeyboardActions(); tree.addPropertyChangeListener(propertyChangeListener); tree.addFocusListener(focusListener); tree.addTreeSelectionListener(treeSelectionListener); tree.addMouseListener(mouseInputListener); tree.addKeyListener(keyListener); tree.addPropertyChangeListener(selectionModelPropertyChangeListener); tree.addComponentListener(componentListener); tree.addTreeExpansionListener(treeExpansionListener); if (treeModel != null) treeModel.addTreeModelListener(treeModelListener); completeUIInstall(); }
15,318
public boolean requestFocusInWindow(boolean temporary) { return super.requestFocusInWindow(temporary); }
public boolean requestFocusInWindow() { return super.requestFocusInWindow(temporary); }
15,319
public boolean requestFocusInWindow(boolean temporary) { return super.requestFocusInWindow(temporary); }
public boolean requestFocusInWindow(boolean temporary) { if (isRequestFocusEnabled()) return super.requestFocusInWindow(); else return false; }
15,320
public TreePath(Object element) { // Create Path path = new Object[1]; path[0] = element; } // TreePath()
public TreePath(Object element) { // Create Path path = new Object[1]; path[0] = element; } // TreePath()
15,321
public TreePath(Object element) { // Create Path path = new Object[1]; path[0] = element; } // TreePath()
public TreePath(Object element) { // Create Path path = new Object[1]; path[0] = element; } // TreePath()
15,322
public void removeCellEditorListener(CellEditorListener listener);
public void removeCellEditorListener(CellEditorListener listener);
15,323
void convertCdataSectionElements(Document doc, Node node, List list) { if (node.getNodeType() == Node.ELEMENT_NODE) { boolean match = false; for (Iterator i = list.iterator(); i.hasNext(); ) { QName qname = (QName) i.next(); if (match(qname, node)) { match = true; break; } } if (match) { Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.getNodeType() == Node.TEXT_NODE) { Node cdata = doc.createCDATASection(ctx.getNodeValue()); node.replaceChild(cdata, ctx); ctx = cdata; } ctx = ctx.getNextSibling(); } } } Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.hasChildNodes()) { convertCdataSectionElements(doc, ctx, list); } ctx = ctx.getNextSibling(); } }
void convertCdataSectionElements(Document doc, Node node, List list) if (node.getNodeType() == Node.ELEMENT_NODE) boolean match = false; for (Iterator i = list.iterator(); i.hasNext(); ) QName qname = (QName) i.next(); if (match(qname, node)) match = true; break; } } if (match) Node ctx = node.getFirstChild(); while (ctx != null) if (ctx.getNodeType() == Node.TEXT_NODE) Node cdata = doc.createCDATASection(ctx.getNodeValue()); node.replaceChild(cdata, ctx); ctx = cdata; } ctx = ctx.getNextSibling(); } } } Node ctx = node.getFirstChild(); while (ctx != null) if (ctx.hasChildNodes()) convertCdataSectionElements(doc, ctx, list); } ctx = ctx.getNextSibling(); } }
15,324
void convertCdataSectionElements(Document doc, Node node, List list) { if (node.getNodeType() == Node.ELEMENT_NODE) { boolean match = false; for (Iterator i = list.iterator(); i.hasNext(); ) { QName qname = (QName) i.next(); if (match(qname, node)) { match = true; break; } } if (match) { Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.getNodeType() == Node.TEXT_NODE) { Node cdata = doc.createCDATASection(ctx.getNodeValue()); node.replaceChild(cdata, ctx); ctx = cdata; } ctx = ctx.getNextSibling(); } } } Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.hasChildNodes()) { convertCdataSectionElements(doc, ctx, list); } ctx = ctx.getNextSibling(); } }
void convertCdataSectionElements(Document doc, Node node, List list) { if (node.getNodeType() == Node.ELEMENT_NODE) { boolean match = false; for (Iterator i = list.iterator(); i.hasNext(); ) { QName qname = (QName) i.next(); if (match(qname, node)) { match = true; break; if (match) { Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.getNodeType() == Node.TEXT_NODE) { Node cdata = doc.createCDATASection(ctx.getNodeValue()); node.replaceChild(cdata, ctx); ctx = cdata; ctx = ctx.getNextSibling(); Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.hasChildNodes()) { convertCdataSectionElements(doc, ctx, list); ctx = ctx.getNextSibling(); }
15,325
public Object getParameter(String name) { if (stylesheet != null) { return stylesheet.bindings.get(new QName(null, name), null, 1, 1); } return null; }
public Object getParameter(String name) if (stylesheet != null) return stylesheet.bindings.get(new QName(null, name), null, 1, 1); } return null; }
15,326
public Object getParameter(String name) { if (stylesheet != null) { return stylesheet.bindings.get(new QName(null, name), null, 1, 1); } return null; }
public Object getParameter(String name) { if (stylesheet != null) { return stylesheet.bindings.get(new QName(null, name), null, 1, 1); return null; }
15,327
boolean match(QName qname, Node node) { String ln1 = qname.getLocalPart(); String ln2 = node.getLocalName(); if (ln2 == null) { return ln1.equals(node.getNodeName()); } else { String uri1 = qname.getNamespaceURI(); String uri2 = node.getNamespaceURI(); return (uri1.equals(uri2) && ln1.equals(ln2)); } }
boolean match(QName qname, Node node) String ln1 = qname.getLocalPart(); String ln2 = node.getLocalName(); if (ln2 == null) return ln1.equals(node.getNodeName()); } else String uri1 = qname.getNamespaceURI(); String uri2 = node.getNamespaceURI(); return (uri1.equals(uri2) && ln1.equals(ln2)); } }
15,328
boolean match(QName qname, Node node) { String ln1 = qname.getLocalPart(); String ln2 = node.getLocalName(); if (ln2 == null) { return ln1.equals(node.getNodeName()); } else { String uri1 = qname.getNamespaceURI(); String uri2 = node.getNamespaceURI(); return (uri1.equals(uri2) && ln1.equals(ln2)); } }
boolean match(QName qname, Node node) { String ln1 = qname.getLocalPart(); String ln2 = node.getLocalName(); if (ln2 == null) { return ln1.equals(node.getNodeName()); else { String uri1 = qname.getNamespaceURI(); String uri2 = node.getNamespaceURI(); return (uri1.equals(uri2) && ln1.equals(ln2)); }
15,329
void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) { if (textContent) { // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); } } else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) { buf.append(INDENT_WHITESPACE); } String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) { buf.append(INDENT_WHITESPACE); } node.appendChild(doc.createTextNode(ws)); } } } }
void reindent(Document doc, Node node, int offset) if (node.hasChildNodes()) boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) switch (ctx.getNodeType()) case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) if (textContent) // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) for (Iterator i = children.iterator(); i.hasNext(); ) ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); } } else StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) buf.append(INDENT_WHITESPACE); } String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) buf.append(INDENT_WHITESPACE); } node.appendChild(doc.createTextNode(ws)); } } } }
15,330
void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) { if (textContent) { // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); } } else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) { buf.append(INDENT_WHITESPACE); } String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) { buf.append(INDENT_WHITESPACE); } node.appendChild(doc.createTextNode(ws)); } } } }
void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; children.add(ctx); ctx = ctx.getNextSibling(); if (markupContent) { if (textContent) { // XXX handle mixed content differently? int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) { buf.append(INDENT_WHITESPACE); String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) { buf.append(INDENT_WHITESPACE); node.appendChild(doc.createTextNode(ws)); }
15,331
void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) { if (textContent) { // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); } } else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) { buf.append(INDENT_WHITESPACE); } String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) { buf.append(INDENT_WHITESPACE); } node.appendChild(doc.createTextNode(ws)); } } } }
void reindent(Document doc, Node node, int offset) if (node.hasChildNodes()) boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) switch (ctx.getNodeType()) case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) if (textContent) // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) for (Iterator i = children.iterator(); i.hasNext(); ) ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); } } else StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) buf.append(INDENT_WHITESPACE); } String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) buf.append(INDENT_WHITESPACE); } node.appendChild(doc.createTextNode(ws)); } } } }
15,332
void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) { if (textContent) { // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); } } else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) { buf.append(INDENT_WHITESPACE); } String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) { buf.append(INDENT_WHITESPACE); } node.appendChild(doc.createTextNode(ws)); } } } }
void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; children.add(ctx); ctx = ctx.getNextSibling(); if (markupContent) { if (textContent) { // XXX handle mixed content differently? int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset + 1); else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) { buf.append(INDENT_WHITESPACE); String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); buf = new StringBuffer(); buf.append('\n'); ws = buf.toString(); for (int i = 0; i < offset; i++) { buf.append(INDENT_WHITESPACE); node.appendChild(doc.createTextNode(ws)); }
15,333
public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) { outputProperties.clear(); } else { outputProperties.putAll(oformat); } }
public void setOutputProperties(Properties oformat) throws IllegalArgumentException if (oformat == null) outputProperties.clear(); } else outputProperties.putAll(oformat); } }
15,334
public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) { outputProperties.clear(); } else { outputProperties.putAll(oformat); } }
public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) { outputProperties.clear(); else { outputProperties.putAll(oformat); }
15,335
public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) { outputProperties.clear(); } else { outputProperties.putAll(oformat); } }
public void setOutputProperties(Properties oformat) throws IllegalArgumentException if (oformat == null) outputProperties.clear(); } else outputProperties.putAll(oformat); } }
15,336
public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) { outputProperties.clear(); } else { outputProperties.putAll(oformat); } }
public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) { outputProperties.clear(); else { outputProperties.putAll(oformat); }
15,337
public void setParameter(String name, Object value) { if (stylesheet != null) { stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM); } }
public void setParameter(String name, Object value) if (stylesheet != null) stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM); } }
15,338
public void setParameter(String name, Object value) { if (stylesheet != null) { stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM); } }
public void setParameter(String name, Object value) { if (stylesheet != null) { stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM); }
15,339
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
static boolean strip(Stylesheet stylesheet, Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,340
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,341
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); if (nextSibling != null) parent.insertBefore(child, nextSibling); else parent.appendChild(child); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,342
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } return true; } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,343
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { String text = node.getNodeValue(); String[] tokens = tokenizeWhitespace(text); if (tokens.length > 1) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,344
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.setNodeValue(tokens[0]); Node parent = node.getParentNode(); Node nextSibling = node.getNextSibling(); Document doc = node.getOwnerDocument(); for (int i = 1; i < tokens.length; i++) { Node newChild = (nt == Node.CDATA_SECTION_NODE) ? doc.createCDATASection(tokens[i]) : doc.createTextNode(tokens[i]); if (nextSibling != null) parent.insertBefore(newChild, nextSibling); else parent.appendChild(newChild); } } return !stylesheet.isPreserved((Text) node, true); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,345
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,346
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { boolean remove = strip(stylesheet, child); Node next = child.getNextSibling(); if (remove) node.removeChild(child); child = next; } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,347
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
void strip(Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node child = node.getFirstChild(); if (child != null) { strip(child); } while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { if (!stylesheet.isPreserved((Text) node)) { node.getParentNode().removeChild(node); } else { String text = node.getNodeValue(); String stripped = text.trim(); if (!text.equals(stripped)) { node.setNodeValue(stripped); } } } else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { strip(child); } } }
15,348
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget)throws TransformerException{// Get the source treeDOMSource source;synchronized (factory.resolver){factory.resolver.setUserResolver(uriResolver);factory.resolver.setUserListener(errorListener);source = factory.resolver.resolveDOM(xmlSource, null, null);}Node context = source.getNode();Document doc = (context instanceof Document) ? (Document) context :context.getOwnerDocument();if (doc instanceof DomDocument){// Suppress mutation events((DomDocument) doc).setBuilding(true);// TODO find a better/more generic way of doing this than// casting}// Get the result treeNode parent = null, nextSibling = null;if (outputTarget instanceof DOMResult){DOMResult dr = (DOMResult) outputTarget;parent = dr.getNode();nextSibling = dr.getNextSibling();Document rdoc = (parent instanceof Document) ? (Document) parent :parent.getOwnerDocument();if (rdoc instanceof DomDocument){// Suppress mutation events and allow multiple root elementsDomDocument drdoc = (DomDocument) rdoc;drdoc.setBuilding(true);drdoc.setCheckWellformedness(false);// TODO find a better/more generic way of doing this than// casting}}boolean created = false;// Transformationif (stylesheet != null){if (parent == null){// Create a new document to hold the resultDomDocument resultDoc = new DomDocument();resultDoc.setBuilding(true);resultDoc.setCheckWellformedness(false);parent = resultDoc;created = true;}// Make a copy of the source node, and strip itcontext = context.cloneNode(true);strip(context);// XSLT transformationtry{// Set output properties in the underlying stylesheet((TransformerOutputProperties) outputProperties).apply();stylesheet.initTopLevelVariables(context);TemplateNode t = stylesheet.getTemplate(null, context, false);if (t != null){stylesheet.current = context;t.apply(stylesheet, null, context, 1, 1, parent, nextSibling);}}catch (TransformerException e){// Done transforming, reset documentif (doc instanceof DomDocument){((DomDocument) doc).setBuilding(false);}throw e;}}else{// Identity transformNode clone = context.cloneNode(true);if (context.getNodeType() != Node.DOCUMENT_NODE){Document resultDoc;if (parent == null){// Create a new document to hold the resultDomDocument rd = new DomDocument();rd.setBuilding(true);rd.setCheckWellformedness(false);parent = resultDoc = rd;created = true;}else{resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();}Document sourceDoc = context.getOwnerDocument();if (sourceDoc != resultDoc){clone = resultDoc.adoptNode(clone);}if (nextSibling != null){parent.insertBefore(clone, nextSibling);}else{parent.appendChild(clone);}}else{// Cannot append document to another treeparent = clone;created = true;}}String method = outputProperties.getProperty(OutputKeys.METHOD);int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML :"text".equals(method) ? Stylesheet.OUTPUT_TEXT :Stylesheet.OUTPUT_XML;String encoding = outputProperties.getProperty(OutputKeys.ENCODING);String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC);String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM);String version = outputProperties.getProperty(OutputKeys.VERSION);boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION));boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE));String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);String cdataSectionElements =outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS);boolean indent ="yes".equals(outputProperties.getProperty(OutputKeys.INDENT));if (created){// Discover document elementDomDocument resultDoc = (DomDocument) parent;Node root = resultDoc.getDocumentElement();// Add doctype if specifiedif ((publicId != null || systemId != null) &&root != null){// We must know the name of the root element to// create the document typeresultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId));}resultDoc.setBuilding(false);resultDoc.setCheckWellformedness(true);}else if (publicId != null || systemId != null){switch (parent.getNodeType()){case Node.DOCUMENT_NODE:case Node.DOCUMENT_FRAGMENT_NODE:Document resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();DOMImplementation impl = resultDoc.getImplementation();DocumentType doctype =impl.createDocumentType(resultDoc.getNodeName(),publicId,systemId);// Try to insert doctype before first elementNode ctx = parent.getFirstChild();for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()){}if (ctx != null){parent.insertBefore(doctype, ctx);}else{parent.appendChild(doctype);}}}if (version != null){parent.setUserData("version", version, stylesheet);}if (omitXmlDeclaration){parent.setUserData("omit-xml-declaration", "yes", stylesheet);}if (standalone){parent.setUserData("standalone", "yes", stylesheet);}if (mediaType != null){parent.setUserData("media-type", mediaType, stylesheet);}if (cdataSectionElements != null){List list = new LinkedList();StringTokenizer st = new StringTokenizer(cdataSectionElements);while (st.hasMoreTokens()){String name = st.nextToken();String localName = name;String uri = null;String prefix = null;int ci = name.indexOf(':');if (ci != -1){// Use namespaces defined on xsl:output node to resolve// namespaces for QNameprefix = name.substring(0, ci);localName = name.substring(ci + 1);uri = stylesheet.output.lookupNamespaceURI(prefix);}list.add(new QName(uri, localName, prefix));}if (!list.isEmpty()){Document resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();convertCdataSectionElements(resultDoc, parent, list);}}if (indent){parent.normalize();strip(parent);Document resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();reindent(resultDoc, parent, 0);}// Render result to the target deviceif (outputTarget instanceof DOMResult){if (created){DOMResult dr = (DOMResult) outputTarget;dr.setNode(parent);dr.setNextSibling(null);}}else if (outputTarget instanceof StreamResult){StreamResult sr = (StreamResult) outputTarget;IOException ex = null;try{writeStreamResult(parent, sr, outputMethod, encoding);}catch (UnsupportedEncodingException e){try{writeStreamResult(parent, sr, outputMethod, "UTF-8");}catch (IOException e2){ex = e2;}}catch (IOException e){ex = e;}if (ex != null){if (errorListener != null){errorListener.error(new TransformerException(ex));}else{ex.printStackTrace(System.err);}}}else if (outputTarget instanceof SAXResult){SAXResult sr = (SAXResult) outputTarget;try{ContentHandler ch = sr.getHandler();LexicalHandler lh = sr.getLexicalHandler();if (lh == null && ch instanceof LexicalHandler){lh = (LexicalHandler) ch;}SAXSerializer serializer = new SAXSerializer();serializer.serialize(parent, ch, lh);}catch (SAXException e){if (errorListener != null){errorListener.error(new TransformerException(e));}else{e.printStackTrace(System.err);}}}}
15,349
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget)throws TransformerException{// Get the source treeDOMSource source;synchronized (factory.resolver){factory.resolver.setUserResolver(uriResolver);factory.resolver.setUserListener(errorListener);source = factory.resolver.resolveDOM(xmlSource, null, null);}Node context = source.getNode();Document doc = (context instanceof Document) ? (Document) context :context.getOwnerDocument();if (doc instanceof DomDocument){// Suppress mutation events((DomDocument) doc).setBuilding(true);// TODO find a better/more generic way of doing this than// casting}// Get the result treeNode parent = null, nextSibling = null;if (outputTarget instanceof DOMResult){DOMResult dr = (DOMResult) outputTarget;parent = dr.getNode();nextSibling = dr.getNextSibling();Document rdoc = (parent instanceof Document) ? (Document) parent :parent.getOwnerDocument();if (rdoc instanceof DomDocument){// Suppress mutation events and allow multiple root elementsDomDocument drdoc = (DomDocument) rdoc;drdoc.setBuilding(true);drdoc.setCheckWellformedness(false);// TODO find a better/more generic way of doing this than// casting}}boolean created = false;// Transformationif (stylesheet != null){if (parent == null){// Create a new document to hold the resultDomDocument resultDoc = new DomDocument();resultDoc.setBuilding(true);resultDoc.setCheckWellformedness(false);parent = resultDoc;created = true;}// Make a copy of the source node, and strip itcontext = context.cloneNode(true);strip(context);// XSLT transformationtry{// Set output properties in the underlying stylesheet((TransformerOutputProperties) outputProperties).apply();stylesheet.initTopLevelVariables(context);TemplateNode t = stylesheet.getTemplate(null, context, false);if (t != null){stylesheet.current = context;t.apply(stylesheet, null, context, 1, 1, parent, nextSibling);}}catch (TransformerException e){// Done transforming, reset documentif (doc instanceof DomDocument){((DomDocument) doc).setBuilding(false);}throw e;}}else{// Identity transformNode clone = context.cloneNode(true);if (context.getNodeType() != Node.DOCUMENT_NODE){Document resultDoc;if (parent == null){// Create a new document to hold the resultDomDocument rd = new DomDocument();rd.setBuilding(true);rd.setCheckWellformedness(false);parent = resultDoc = rd;created = true;}else{resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();}Document sourceDoc = context.getOwnerDocument();if (sourceDoc != resultDoc){clone = resultDoc.adoptNode(clone);}if (nextSibling != null){parent.insertBefore(clone, nextSibling);}else{parent.appendChild(clone);}}else{// Cannot append document to another treeparent = clone;created = true;}}String method = outputProperties.getProperty(OutputKeys.METHOD);int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML :"text".equals(method) ? Stylesheet.OUTPUT_TEXT :Stylesheet.OUTPUT_XML;String encoding = outputProperties.getProperty(OutputKeys.ENCODING);String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC);String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM);String version = outputProperties.getProperty(OutputKeys.VERSION);boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION));boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE));String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);String cdataSectionElements =outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS);boolean indent ="yes".equals(outputProperties.getProperty(OutputKeys.INDENT));if (created){// Discover document elementDomDocument resultDoc = (DomDocument) parent;Node root = resultDoc.getDocumentElement();// Add doctype if specifiedif ((publicId != null || systemId != null) &&root != null){// We must know the name of the root element to// create the document typeresultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId));}resultDoc.setBuilding(false);resultDoc.setCheckWellformedness(true);}else if (publicId != null || systemId != null){switch (parent.getNodeType()){case Node.DOCUMENT_NODE:case Node.DOCUMENT_FRAGMENT_NODE:Document resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();DOMImplementation impl = resultDoc.getImplementation();DocumentType doctype =impl.createDocumentType(resultDoc.getNodeName(),publicId,systemId);// Try to insert doctype before first elementNode ctx = parent.getFirstChild();for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()){}if (ctx != null){parent.insertBefore(doctype, ctx);}else{parent.appendChild(doctype);}}}if (version != null){parent.setUserData("version", version, stylesheet);}if (omitXmlDeclaration){parent.setUserData("omit-xml-declaration", "yes", stylesheet);}if (standalone){parent.setUserData("standalone", "yes", stylesheet);}if (mediaType != null){parent.setUserData("media-type", mediaType, stylesheet);}if (cdataSectionElements != null){List list = new LinkedList();StringTokenizer st = new StringTokenizer(cdataSectionElements);while (st.hasMoreTokens()){String name = st.nextToken();String localName = name;String uri = null;String prefix = null;int ci = name.indexOf(':');if (ci != -1){// Use namespaces defined on xsl:output node to resolve// namespaces for QNameprefix = name.substring(0, ci);localName = name.substring(ci + 1);uri = stylesheet.output.lookupNamespaceURI(prefix);}list.add(new QName(uri, localName, prefix));}if (!list.isEmpty()){Document resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();convertCdataSectionElements(resultDoc, parent, list);}}if (indent){parent.normalize();strip(parent);Document resultDoc = (parent instanceof Document) ?(Document) parent :parent.getOwnerDocument();reindent(resultDoc, parent, 0);}// Render result to the target deviceif (outputTarget instanceof DOMResult){if (created){DOMResult dr = (DOMResult) outputTarget;dr.setNode(parent);dr.setNextSibling(null);}}else if (outputTarget instanceof StreamResult){StreamResult sr = (StreamResult) outputTarget;IOException ex = null;try{writeStreamResult(parent, sr, outputMethod, encoding);}catch (UnsupportedEncodingException e){try{writeStreamResult(parent, sr, outputMethod, "UTF-8");}catch (IOException e2){ex = e2;}}catch (IOException e){ex = e;}if (ex != null){if (errorListener != null){errorListener.error(new TransformerException(ex));}else{ex.printStackTrace(System.err);}}}else if (outputTarget instanceof SAXResult){SAXResult sr = (SAXResult) outputTarget;try{ContentHandler ch = sr.getHandler();LexicalHandler lh = sr.getLexicalHandler();if (lh == null && ch instanceof LexicalHandler){lh = (LexicalHandler) ch;}SAXSerializer serializer = new SAXSerializer();serializer.serialize(parent, ch, lh);}catch (SAXException e){if (errorListener != null){errorListener.error(new TransformerException(e));}else{e.printStackTrace(System.err);}}}}
15,350
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(stylesheet, context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,351
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,352
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,353
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,354
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,355
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,356
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,357
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,358
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,359
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type DocumentType doctype = new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,360
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId); resultDoc.insertBefore(doctype, root); } } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,361
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(root.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,362
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,363
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,364
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,365
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,366
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,367
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,368
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,369
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,370
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,371
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(stylesheet, parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,372
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,373
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,374
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,375
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,376
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,377
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException // Get the source tree DOMSource source; synchronized (factory.resolver) factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) if (parent == null) // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) // Done transforming, reset document if (doc instanceof DomDocument) ((DomDocument) doc).setBuilding(false); } throw e; } } else // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) Document resultDoc; if (parent == null) // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) clone = resultDoc.adoptNode(clone); } if (nextSibling != null) parent.insertBefore(clone, nextSibling); } else parent.appendChild(clone); } } else // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) switch (parent.getNodeType()) case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) } if (ctx != null) parent.insertBefore(doctype, ctx); } else parent.appendChild(doctype); } } } if (version != null) parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) if (created) DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) try writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) ex = e2; } } catch (IOException e) ex = e; } if (ex != null) if (errorListener != null) errorListener.error(new TransformerException(ex)); } else ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) SAXResult sr = (SAXResult) outputTarget; try ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) if (errorListener != null) errorListener.error(new TransformerException(e)); } else e.printStackTrace(System.err); } } } }
15,378
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
15,379
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); } Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting } // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting } } boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; } // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); } } catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); } throw e; } } else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; } else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); } if (nextSibling != null) { parent.insertBefore(clone, nextSibling); } else { parent.appendChild(clone); } } else { // Cannot append document to another tree parent = clone; created = true; } } String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); } resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); } else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { } if (ctx != null) { parent.insertBefore(doctype, ctx); } else { parent.appendChild(doctype); } } } if (version != null) { parent.setUserData("version", version, stylesheet); } if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); } if (standalone) { parent.setUserData("standalone", "yes", stylesheet); } if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); } if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); } list.add(new QName(uri, localName, prefix)); } if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); } } if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); } // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); } } else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); } catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); } catch (IOException e2) { ex = e2; } } catch (IOException e) { ex = e; } if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); } else { ex.printStackTrace(System.err); } } } else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; } SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); } catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); } else { e.printStackTrace(System.err); } } } }
public void transform(Source xmlSource, Result outputTarget) throws TransformerException { // Get the source tree DOMSource source; synchronized (factory.resolver) { factory.resolver.setUserResolver(uriResolver); factory.resolver.setUserListener(errorListener); source = factory.resolver.resolveDOM(xmlSource, null, null); Node context = source.getNode(); Document doc = (context instanceof Document) ? (Document) context : context.getOwnerDocument(); if (doc instanceof DomDocument) { // Suppress mutation events ((DomDocument) doc).setBuilding(true); // TODO find a better/more generic way of doing this than // casting // Get the result tree Node parent = null, nextSibling = null; if (outputTarget instanceof DOMResult) { DOMResult dr = (DOMResult) outputTarget; parent = dr.getNode(); nextSibling = dr.getNextSibling(); Document rdoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); if (rdoc instanceof DomDocument) { // Suppress mutation events and allow multiple root elements DomDocument drdoc = (DomDocument) rdoc; drdoc.setBuilding(true); drdoc.setCheckWellformedness(false); // TODO find a better/more generic way of doing this than // casting boolean created = false; // Transformation if (stylesheet != null) { if (parent == null) { // Create a new document to hold the result DomDocument resultDoc = new DomDocument(); resultDoc.setBuilding(true); resultDoc.setCheckWellformedness(false); parent = resultDoc; created = true; // Make a copy of the source node, and strip it context = context.cloneNode(true); strip(context); // XSLT transformation try { // Set output properties in the underlying stylesheet ((TransformerOutputProperties) outputProperties).apply(); stylesheet.initTopLevelVariables(context); TemplateNode t = stylesheet.getTemplate(null, context, false); if (t != null) { stylesheet.current = context; t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); catch (TransformerException e) { // Done transforming, reset document if (doc instanceof DomDocument) { ((DomDocument) doc).setBuilding(false); throw e; else { // Identity transform Node clone = context.cloneNode(true); if (context.getNodeType() != Node.DOCUMENT_NODE) { Document resultDoc; if (parent == null) { // Create a new document to hold the result DomDocument rd = new DomDocument(); rd.setBuilding(true); rd.setCheckWellformedness(false); parent = resultDoc = rd; created = true; else { resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); Document sourceDoc = context.getOwnerDocument(); if (sourceDoc != resultDoc) { clone = resultDoc.adoptNode(clone); if (nextSibling != null) { parent.insertBefore(clone, nextSibling); else { parent.appendChild(clone); else { // Cannot append document to another tree parent = clone; created = true; String method = outputProperties.getProperty(OutputKeys.METHOD); int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : "text".equals(method) ? Stylesheet.OUTPUT_TEXT : Stylesheet.OUTPUT_XML; String encoding = outputProperties.getProperty(OutputKeys.ENCODING); String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); String version = outputProperties.getProperty(OutputKeys.VERSION); boolean omitXmlDeclaration = "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); boolean standalone = "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); String cdataSectionElements = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); boolean indent = "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); if (created) { // Discover document element DomDocument resultDoc = (DomDocument) parent; Node root = resultDoc.getDocumentElement(); // Add doctype if specified if ((publicId != null || systemId != null) && root != null) { // We must know the name of the root element to // create the document type resultDoc.appendChild(new DomDoctype(resultDoc, root.getNodeName(), publicId, systemId)); resultDoc.setBuilding(false); resultDoc.setCheckWellformedness(true); else if (publicId != null || systemId != null) { switch (parent.getNodeType()) { case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); DOMImplementation impl = resultDoc.getImplementation(); DocumentType doctype = impl.createDocumentType(resultDoc.getNodeName(), publicId, systemId); // Try to insert doctype before first element Node ctx = parent.getFirstChild(); for (; ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE; ctx = ctx.getNextSibling()) { if (ctx != null) { parent.insertBefore(doctype, ctx); else { parent.appendChild(doctype); if (version != null) { parent.setUserData("version", version, stylesheet); if (omitXmlDeclaration) { parent.setUserData("omit-xml-declaration", "yes", stylesheet); if (standalone) { parent.setUserData("standalone", "yes", stylesheet); if (mediaType != null) { parent.setUserData("media-type", mediaType, stylesheet); if (cdataSectionElements != null) { List list = new LinkedList(); StringTokenizer st = new StringTokenizer(cdataSectionElements); while (st.hasMoreTokens()) { String name = st.nextToken(); String localName = name; String uri = null; String prefix = null; int ci = name.indexOf(':'); if (ci != -1) { // Use namespaces defined on xsl:output node to resolve // namespaces for QName prefix = name.substring(0, ci); localName = name.substring(ci + 1); uri = stylesheet.output.lookupNamespaceURI(prefix); list.add(new QName(uri, localName, prefix)); if (!list.isEmpty()) { Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); convertCdataSectionElements(resultDoc, parent, list); if (indent) { parent.normalize(); strip(parent); Document resultDoc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); reindent(resultDoc, parent, 0); // Render result to the target device if (outputTarget instanceof DOMResult) { if (created) { DOMResult dr = (DOMResult) outputTarget; dr.setNode(parent); dr.setNextSibling(null); else if (outputTarget instanceof StreamResult) { StreamResult sr = (StreamResult) outputTarget; IOException ex = null; try { writeStreamResult(parent, sr, outputMethod, encoding); catch (UnsupportedEncodingException e) { try { writeStreamResult(parent, sr, outputMethod, "UTF-8"); catch (IOException e2) { ex = e2; catch (IOException e) { ex = e; if (ex != null) { if (errorListener != null) { errorListener.error(new TransformerException(ex)); else { ex.printStackTrace(System.err); else if (outputTarget instanceof SAXResult) { SAXResult sr = (SAXResult) outputTarget; try { ContentHandler ch = sr.getHandler(); LexicalHandler lh = sr.getLexicalHandler(); if (lh == null && ch instanceof LexicalHandler) { lh = (LexicalHandler) ch; SAXSerializer serializer = new SAXSerializer(); serializer.serialize(parent, ch, lh); catch (SAXException e) { if (errorListener != null) { errorListener.error(new TransformerException(e)); else { e.printStackTrace(System.err); }
15,380
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) { out = new WriterOutputStream(writer); } } if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) { out = new FileOutputStream(systemId); } catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally { try { if (out != null && created) { out.close(); } } catch (IOException e) { } } }
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException OutputStream out = null; boolean created = false; try out = sr.getOutputStream(); if (out == null) Writer writer = sr.getWriter(); if (writer != null) out = new WriterOutputStream(writer); } } if (out == null) String systemId = sr.getSystemId(); try URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) out = new FileOutputStream(systemId); } catch (UnknownServiceException e) URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally try if (out != null && created) out.close(); } } catch (IOException e) } } }
15,381
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) { out = new WriterOutputStream(writer); } } if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) { out = new FileOutputStream(systemId); } catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally { try { if (out != null && created) { out.close(); } } catch (IOException e) { } } }
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) { out = new WriterOutputStream(writer); if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); catch (MalformedURLException e) { out = new FileOutputStream(systemId); catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); created = true; out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); serializer.serialize(node, out); out.flush(); finally { try { if (out != null && created) { out.close(); catch (IOException e) { }
15,382
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) { out = new WriterOutputStream(writer); } } if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) { out = new FileOutputStream(systemId); } catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally { try { if (out != null && created) { out.close(); } } catch (IOException e) { } } }
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException OutputStream out = null; boolean created = false; try out = sr.getOutputStream(); if (out == null) Writer writer = sr.getWriter(); if (writer != null) out = new WriterOutputStream(writer); } } if (out == null) String systemId = sr.getSystemId(); try URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) out = new FileOutputStream(systemId); } catch (UnknownServiceException e) URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally try if (out != null && created) out.close(); } } catch (IOException e) } } }
15,383
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) { out = new WriterOutputStream(writer); } } if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) { out = new FileOutputStream(systemId); } catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally { try { if (out != null && created) { out.close(); } } catch (IOException e) { } } }
void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) { out = new WriterOutputStream(writer); if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); catch (MalformedURLException e) { out = new FileOutputStream(systemId); catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); created = true; out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); serializer.serialize(node, out); out.flush(); finally { try { if (out != null && created) { out.close(); catch (IOException e) { }
15,384
public abstract void setErrorListener(ErrorListener listener) throws IllegalArgumentException;
public abstract void setErrorListener(ErrorListener listener) throws IllegalArgumentException;
15,385
public abstract void setOutputProperties(Properties outputformat) throws IllegalArgumentException;
public abstract void setOutputProperties(Properties oformat) throws IllegalArgumentException;
15,386
public abstract void transform(Source source, Result result) throws TransformerException;
public abstract void transform(Source source, Result result) throws TransformerException;
15,387
public Node getNode() { return node; } // getNode()
public Node getNode() { return node; } // getNode()
15,388
public Node getNode() { return node; } // getNode()
public Node getNode() { return node; } // getNode()
15,389
public Node getNode() { return node; } // getNode()
public Node getNode() { return node; } // getNode()
15,390
public Node getNode() { return node; } // getNode()
public Node getNode() { return node; } // getNode()
15,391
public void setNode(Node node) { this.node = node; } // setNode()
public void setNode(Node node) { this.node = node; } // setNode()
15,392
public void setNode(Node node) { this.node = node; } // setNode()
public void setNode(Node node) { this.node = node; } // setNode()
15,393
public ContentHandler getHandler() { return handler; }
public ContentHandler getHandler() { return handler; }
15,394
public LexicalHandler getLexicalHandler() { return lexhandler; }
public LexicalHandler getLexicalHandler() { return lexhandler; }
15,395
public OutputStream getOutputStream() { return outputStream; }
public OutputStream getOutputStream() { return outputStream; }
15,396
public Writer getWriter() { return writer; }
public Writer getWriter() { return writer; }
15,397
public String getSystemId() { return systemId; }
public String getSystemId() { return systemId; }
15,398
public URLConnection openConnection() throws IOException { return handler.openConnection(this); }
public URLConnection openConnection() throws IOException { return handler.openConnection(this); }
15,399
public String getPath() { int quest = (file == null) ? -1 : file.indexOf('?'); return quest < 0 ? getFile() : file.substring(0, quest); }
public String getPath() { int quest = (file == null) ? -1 : file.indexOf('?'); return quest < 0 ? getFile() : file.substring(0, quest); }
15,400
public OID(String strRep) { this(strRep, false); }
public OID(int[] components) { this(strRep, false); }
15,401
public OID(String strRep) { this(strRep, false); }
public OID(String strRep) { this(components, false); }
15,402
public static final String toString(byte[] ba, int offset, int length) { char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length;) { k = ba[offset + i++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return new String(buf); }
public static String toString(byte[] ba) { char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length;) { k = ba[offset + i++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return new String(buf); }
15,403
public static final String toString(byte[] ba, int offset, int length) { char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length;) { k = ba[offset + i++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return new String(buf); }
public static final String toString(byte[] ba, int offset, int length) { char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length;) { k = ba[offset + i++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return new String(buf); }
15,404
public DERValue read() throws IOException { int tag = in.read(); if (tag == -1) throw new EOFException(); encBuf.write(tag); int len = readLength(); DERValue value = null; if ((tag & CONSTRUCTED) == CONSTRUCTED) { in.mark(2048); byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, CONSTRUCTED_VALUE, encBuf.toByteArray()); in.reset(); encBuf.reset(); return value; } switch (tag & 0xC0) { case UNIVERSAL: value = new DERValue(tag, len, readUniversal(tag, len), encBuf.toByteArray()); encBuf.reset(); break; case CONTEXT: byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, encoded, encBuf.toByteArray()); encBuf.reset(); break; case APPLICATION: // This should not be reached, since (I think) APPLICATION is // always constructed. throw new DEREncodingException("non-constructed APPLICATION data"); default: throw new DEREncodingException("PRIVATE class not supported"); } return value; }
public static DERValue read(byte[] encoded) throws IOException { int tag = in.read(); if (tag == -1) throw new EOFException(); encBuf.write(tag); int len = readLength(); DERValue value = null; if ((tag & CONSTRUCTED) == CONSTRUCTED) { in.mark(2048); byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, CONSTRUCTED_VALUE, encBuf.toByteArray()); in.reset(); encBuf.reset(); return value; } switch (tag & 0xC0) { case UNIVERSAL: value = new DERValue(tag, len, readUniversal(tag, len), encBuf.toByteArray()); encBuf.reset(); break; case CONTEXT: byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, encoded, encBuf.toByteArray()); encBuf.reset(); break; case APPLICATION: // This should not be reached, since (I think) APPLICATION is // always constructed. throw new DEREncodingException("non-constructed APPLICATION data"); default: throw new DEREncodingException("PRIVATE class not supported"); } return value; }
15,405
public DERValue read() throws IOException { int tag = in.read(); if (tag == -1) throw new EOFException(); encBuf.write(tag); int len = readLength(); DERValue value = null; if ((tag & CONSTRUCTED) == CONSTRUCTED) { in.mark(2048); byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, CONSTRUCTED_VALUE, encBuf.toByteArray()); in.reset(); encBuf.reset(); return value; } switch (tag & 0xC0) { case UNIVERSAL: value = new DERValue(tag, len, readUniversal(tag, len), encBuf.toByteArray()); encBuf.reset(); break; case CONTEXT: byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, encoded, encBuf.toByteArray()); encBuf.reset(); break; case APPLICATION: // This should not be reached, since (I think) APPLICATION is // always constructed. throw new DEREncodingException("non-constructed APPLICATION data"); default: throw new DEREncodingException("PRIVATE class not supported"); } return value; }
public DERValue read() throws IOException { int tag = in.read(); if (tag == -1) throw new EOFException(); encBuf.write(tag); int len = readLength(); DERValue value = null; if ((tag & CONSTRUCTED) == CONSTRUCTED) { in.mark(2048); byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, CONSTRUCTED_VALUE, encBuf.toByteArray()); in.reset(); encBuf.reset(); return value; } switch (tag & 0xC0) { case UNIVERSAL: value = new DERValue(tag, len, readUniversal(tag, len), encBuf.toByteArray()); encBuf.reset(); break; case CONTEXT: byte[] encoded = new byte[len]; in.read(encoded); encBuf.write(encoded); value = new DERValue(tag, len, encoded, encBuf.toByteArray()); encBuf.reset(); break; case APPLICATION: // This should not be reached, since (I think) APPLICATION is // always constructed. throw new DEREncodingException("non-constructed APPLICATION data"); default: throw new DEREncodingException("PRIVATE class not supported"); } return value; }
15,406
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException { Provider[] p = Security.getProviders(); for (int i = 0; i < p.length; i++) { try { return getInstance(algorithm, p[i]); } catch (NoSuchAlgorithmException ignored) {} } throw new NoSuchAlgorithmException(algorithm); }
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException { Provider[] p = Security.getProviders(); for (int i = 0; i < p.length; i++) { try { return getInstance(algorithm, p[i]); } catch (NoSuchAlgorithmException e) { } } throw new NoSuchAlgorithmException(algorithm); }
15,407
public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException { Provider[] p = Security.getProviders(); for (int i = 0; i < p.length; i++) { try { return getInstance(algorithm, p[i]); } catch (NoSuchAlgorithmException ignored) {} } throw new NoSuchAlgorithmException(algorithm); }
public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException { Provider[] p = Security.getProviders(); for (int i = 0; i < p.length; i++) { try { return getInstance(algorithm, p[i]); } catch (NoSuchAlgorithmException e) { } } throw new NoSuchAlgorithmException(algorithm); }
15,408
public FileInputStream(File file) throws FileNotFoundException { SecurityManager s = System.getSecurityManager(); if (s != null) s.checkRead(file.getPath()); ch = FileChannelImpl.create(file, FileChannelImpl.READ); }
public FileInputStream(String name) throws FileNotFoundException { SecurityManager s = System.getSecurityManager(); if (s != null) s.checkRead(file.getPath()); ch = FileChannelImpl.create(file, FileChannelImpl.READ); }
15,409
public FileInputStream(File file) throws FileNotFoundException { SecurityManager s = System.getSecurityManager(); if (s != null) s.checkRead(file.getPath()); ch = FileChannelImpl.create(file, FileChannelImpl.READ); }
public FileInputStream(File file) throws FileNotFoundException { SecurityManager s = System.getSecurityManager(); if (s != null) s.checkRead(file.getPath()); ch = FileChannelImpl.create(file, FileChannelImpl.READ); }
15,410
public static KeyStore getInstance(String type) throws KeyStoreException { Provider[] p = Security.getProviders(); for (int i = 0; i < p.length; i++) { try { return getInstance(type, p[i]); } catch (KeyStoreException ignore) { } } throw new KeyStoreException(type); }
public static KeyStore getInstance(String type) throws KeyStoreException { Provider[] p = Security.getProviders(); for (int i = 0; i < p.length; i++) { try { return getInstance(type, p[i]); } catch (KeyStoreException e) { } } throw new KeyStoreException(type); }
15,411