public String copy(DatabaseObject dbo) throws EngineException { clipboardDocument = XMLUtils.getDefaultDocumentBuilder().newDocument(); ProcessingInstruction pi = clipboardDocument.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"ISO-8859-1\""); clipboardDocument.appendChild(pi); clipboardRootElement = clipboardDocument.createElement("convertigo-clipboard"); clipboardDocument.appendChild(clipboardRootElement); if (dbo != null) { copyDatabaseObject(dbo); } String strObject = XMLUtils.prettyPrintDOM(clipboardDocument); return strObject; }
String ecmaValue() { // TODO See ECMA 357 Section 9.1 if (isTextType()) { return ((org.w3c.dom.Text)dom).getData(); } else if (isAttributeType()) { return ((org.w3c.dom.Attr)dom).getValue(); } else if (isProcessingInstructionType()) { return ((org.w3c.dom.ProcessingInstruction)dom).getData(); } else if (isCommentType()) { return ((org.w3c.dom.Comment)dom).getNodeValue(); } else if (isElementType()) { throw new RuntimeException("Unimplemented ecmaValue() for elements."); } else { throw new RuntimeException("Unimplemented for node " + dom); } }
/** * Method getStrFromNode * * @param xpathnode * @return the string for the node. */ public static String getStrFromNode(Node xpathnode) { if (xpathnode.getNodeType() == Node.TEXT_NODE) { // we iterate over all siblings of the context node because eventually, // the text is "polluted" with pi's or comments StringBuilder sb = new StringBuilder(); for (Node currentSibling = xpathnode.getParentNode().getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) { if (currentSibling.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) currentSibling).getData()); } } return sb.toString(); } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { return ((Attr) xpathnode).getNodeValue(); } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return ((ProcessingInstruction) xpathnode).getNodeValue(); } return null; }
private void visit( Node n ) throws SAXException { setCurrentLocation( n ); // if a case statement gets too big, it should be made into a separate method. switch(n.getNodeType()) { case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: String value = n.getNodeValue(); receiver.characters( value.toCharArray(), 0, value.length() ); break; case Node.ELEMENT_NODE: visit( (Element)n ); break; case Node.ENTITY_REFERENCE_NODE: receiver.skippedEntity(n.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: ProcessingInstruction pi = (ProcessingInstruction)n; receiver.processingInstruction(pi.getTarget(),pi.getData()); break; } }
/** * Serializes an ProcessingInstruction Node. * * @param node The ProcessingInstruction Node to serialize */ protected void serializePI(ProcessingInstruction node) throws SAXException { ProcessingInstruction pi = node; String name = pi.getNodeName(); // well-formed=true if ((fFeatures & WELLFORMED) != 0) { isPIWellFormed(node); } // apply the LSSerializer filter if (!applyFilter(node, NodeFilter.SHOW_PROCESSING_INSTRUCTION)) { return; } // String data = pi.getData(); if (name.equals("xslt-next-is-raw")) { fNextIsRaw = true; } else { this.fSerializer.processingInstruction(name, pi.getData()); } }
@Test public void test() throws Exception { try { ByteArrayInputStream bais = new ByteArrayInputStream("<?xmltarget foo?><test></test>".getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder xmlParser = factory.newDocumentBuilder(); // DOMParser p = new DOMParser(); Document document = xmlParser.parse(new InputSource(bais)); String result = ((ProcessingInstruction) document.getFirstChild()).getData(); System.out.println(result); if (!result.equalsIgnoreCase("foo")) { Assert.fail("missing PI data"); } } catch (Exception e) { } }
@Test public void testWProlog() throws Exception { try { ByteArrayInputStream bais = new ByteArrayInputStream("<?xml version=\"1.1\" encoding=\"UTF-8\"?><?xmltarget foo?><test></test>".getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder xmlParser = factory.newDocumentBuilder(); // DOMParser p = new DOMParser(); Document document = xmlParser.parse(new InputSource(bais)); String result = ((ProcessingInstruction) document.getFirstChild()).getData(); System.out.println(result); if (!result.equalsIgnoreCase("foo")) { Assert.fail("missing PI data"); } } catch (Exception e) { } }
public void processingInstruction (String target, String data) throws SAXException { // we can't create populated entity ref nodes using // only public DOM APIs (they've got to be readonly) if (currentEntity != null) return; ProcessingInstruction pi; if (isL2 // && consumer.isUsingNamespaces () && target.indexOf (':') != -1) namespaceError ( "PI target name is namespace nonconformant: " + target); if (inDTD) return; pi = document.createProcessingInstruction (target, data); top.appendChild (pi); }
/** * <b>DOM L1</b> * Returns a newly created processing instruction. */ public ProcessingInstruction createProcessingInstruction(String target, String data) { if (checkingCharacters) { boolean xml11 = "1.1".equals(version); checkName(target, xml11); if ("xml".equalsIgnoreCase(target)) { throw new DomDOMException(DOMException.SYNTAX_ERR, "illegal PI target name", this, 0); } checkChar(data, xml11); } return new DomProcessingInstruction(this, target, data); }
protected static XNode parseNode(Node domNode) { if (domNode instanceof Element) { return XElement.parse((Element) domNode); } if (domNode instanceof Text) { return new XText(((Text) domNode).getTextContent()); } if (domNode instanceof Comment) { return new XComment(((Comment) domNode).getTextContent()); } if (domNode instanceof ProcessingInstruction) { return new XProcessingInstruction(((ProcessingInstruction) domNode).getTarget(), ((ProcessingInstruction) domNode).getData()); } if (domNode instanceof DocumentType) { return new XDocumentType(((DocumentType) domNode).getName(), ((DocumentType) domNode).getInternalSubset()); } throw new UnsupportedOperationException("implement " + domNode); }
protected Document initRepository ( final String processingType, final String type ) { final Document doc = this.xml.create (); { final ProcessingInstruction pi = doc.createProcessingInstruction ( processingType, "version=\"1.1.0\"" ); doc.appendChild ( pi ); } final Element root = doc.createElement ( "repository" ); doc.appendChild ( root ); root.setAttribute ( "name", this.title ); root.setAttribute ( "type", type ); root.setAttribute ( "version", "1" ); return doc; }
protected void print(Node node, Map namespaces, boolean endWithComma) { switch (node.getNodeType()) { case Node.ELEMENT_NODE : printElement((Element) node, namespaces, endWithComma); break; case Node.PROCESSING_INSTRUCTION_NODE : printPI((ProcessingInstruction) node, endWithComma); break; case Node.TEXT_NODE : printText((Text) node, endWithComma); break; case Node.COMMENT_NODE : printComment((Comment) node, endWithComma); break; } }
/** * Loads the given node in the current sequence. * * @param node The W3C DOM node to load. * * @throws LoadingException If thrown while parsing. */ private void loadNode(Node node) throws LoadingException { // dispatch to the correct loader performance: order by occurrence if (node instanceof Element) { load((Element)node); } if (node instanceof Text) { load((Text)node); } else if (node instanceof Attr) { load((Attr)node); } else if (node instanceof Document) { load((Document)node); } else if (node instanceof ProcessingInstruction) { load((ProcessingInstruction)node); // all other node types are ignored } }
static <R> R visitNode(Node node, NodeVisitor<R> visitor) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: return visitor.visitElement((Element) node); case Node.ATTRIBUTE_NODE: return visitor.visitAttr((Attr) node); case Node.TEXT_NODE: return visitor.visitText((Text) node); case Node.CDATA_SECTION_NODE: return visitor.visitCDATASection((CDATASection) node); case Node.ENTITY_REFERENCE_NODE: return visitor.visitEntityReference((EntityReference) node); case Node.ENTITY_NODE: return visitor.visitEntity((Entity) node); case Node.PROCESSING_INSTRUCTION_NODE: return visitor.visitProcessingInstruction((ProcessingInstruction) node); case Node.COMMENT_NODE: return visitor.visitComment((Comment) node); case Node.DOCUMENT_NODE: return visitor.visitDocument((Document) node); case Node.DOCUMENT_TYPE_NODE: return visitor.visitDocumentType((DocumentType) node); case Node.DOCUMENT_FRAGMENT_NODE: return visitor.visitDocumentFragment((DocumentFragment) node); case Node.NOTATION_NODE: return visitor.visitNotation((Notation) node); default: throw new RuntimeException(); } }
public final void testProcessingInstructionInHtml() { Document doc = DomParser.makeDocument(null, null); ProcessingInstruction pi = doc.createProcessingInstruction( "foo", "<script>alert(1)</script>"); Element el = doc.createElementNS(Namespaces.HTML_NAMESPACE_URI, "div"); el.appendChild(pi); assertEquals( "<div><?foo <script>alert(1)</script>?></div>", Nodes.render(el, MarkupRenderMode.XML)); try { Nodes.render(el, MarkupRenderMode.HTML); } catch (UncheckedUnrenderableException ex) { // OK return; } fail("Rendered in html"); }
public void testInvalidCharactersProcessingInstructionData() throws Exception { ErrorRecorder errorRecorder = new ErrorRecorder(); domConfiguration.setParameter("error-handler", errorRecorder); domConfiguration.setParameter("namespaces", false); Element root = document.createElement("foo"); document.appendChild(root); ProcessingInstruction pi = document.createProcessingInstruction("foo", ""); root.appendChild(pi); for (int c = 0; c <= Character.MAX_VALUE; c++) { pi.setData(new String(new char[] { 'A', 'B', (char) c})); document.normalizeDocument(); if (isValid((char) c)) { assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors); } else { errorRecorder.assertAllErrors("For character " + c, DOMError.SEVERITY_ERROR, "wf-invalid-character"); } } }
/** * Append inner html impl. * * @param buffer * the buffer */ protected void appendInnerHTMLImpl(StringBuilder buffer) { ArrayList<Node> nl = this.nodeList; int size; if (nl != null && (size = nl.size()) > 0) { for (int i = 0; i < size; i++) { Node child = nl.get(i); if (child instanceof HTMLElementImpl) { ((HTMLElementImpl) child).appendOuterHTMLImpl(buffer); } else if (child instanceof Comment) { buffer.append("<!--" + ((Comment) child).getTextContent() + "-->"); } else if (child instanceof Text) { String text = ((Text) child).getTextContent(); String encText = this.htmlEncodeChildText(text); buffer.append(encText); } else if (child instanceof ProcessingInstruction) { buffer.append(child.toString()); } } } }