public static String getTextForNode(Node node) { StringBuilder sb = new StringBuilder(); NodeList children = node.getChildNodes(); if (children.getLength() == 0) return null; for (int i = 0; i < children.getLength(); ++i) { Node n = children.item(i); if (n instanceof Text) sb.append(n.getNodeValue()); else if (n instanceof EntityReference) { String s = getTextForNode(n); if (s == null) return null; else sb.append(s); } else return null; } return sb.toString(); }
/** * Extract the textual content from a Node. * This is rather like the XPath value of a Node. * @param node The node to extract the text from * @return The textual value of the node */ public static String getText(Node node) { StringBuffer reply = new StringBuffer(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if ((child instanceof CharacterData && !(child instanceof Comment)) || child instanceof EntityReference) { reply.append(child.getNodeValue()); } else if (child.getNodeType() == Node.ELEMENT_NODE) { reply.append(getText(child)); } } return reply.toString(); }
/** * End processing of given node * * * @param node Node we just finished processing * * @throws org.xml.sax.SAXException */ protected void endNode(Node node) throws org.xml.sax.SAXException { switch (node.getNodeType()) { case Node.DOCUMENT_NODE : break; case Node.DOCUMENT_TYPE_NODE : serializeDocType((DocumentType) node, false); break; case Node.ELEMENT_NODE : serializeElement((Element) node, false); break; case Node.CDATA_SECTION_NODE : break; case Node.ENTITY_REFERENCE_NODE : serializeEntityReference((EntityReference) node, false); break; default : } }
protected int moveToChild(final int currentChild) { content = getCurrentElement().getChildNodes().item(currentChild); if (content instanceof Text) { return CHARACTERS; } else if (content instanceof Element) { return START_ELEMENT; } else if (content instanceof CDATASection) { return CDATA; } else if (content instanceof Comment) { return CHARACTERS; } else if (content instanceof EntityReference) { return ENTITY_REFERENCE; } throw new IllegalStateException(); }
public void startEntity (String name) throws SAXException { // are we ignoring what would be contents of an // entity ref, since we can't populate it? if (currentEntity != null) return; // Are we hiding all entity boundaries? if (consumer.isHidingReferences ()) return; // SAX2 shows parameter entities; DOM hides them if (name.charAt (0) == '%' || "[dtd]".equals (name)) return; // Since we can't create a populated entity ref node in any // standard way, we create an unpopulated one. EntityReference ref = document.createEntityReference (name); top.appendChild (ref); top = ref; // ... allowing subclasses to populate them if (!canPopulateEntityRefs ()) currentEntity = name; }
/** * <b>DOM L1</b> * Returns a newly created reference to the specified entity. * The caller should populate this with the appropriate children * and then mark it as readonly. * * @see DomNode#makeReadonly */ public EntityReference createEntityReference(String name) { DomEntityReference ret = new DomEntityReference(this, name); DocumentType doctype = getDoctype(); if (doctype != null) { DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name); if (ent != null) { for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next) { ret.appendChild(ctx.cloneNode(true)); } } } ret.makeReadonly(); return ret; }
public static void appendNode(Node node, StringBuffer buf) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: appendElement((Element) node, buf); break; case Node.TEXT_NODE: appendText((Text) node, buf); break; case Node.CDATA_SECTION_NODE: appendCDATASection((CDATASection) node, buf); break; case Node.ENTITY_REFERENCE_NODE: appendEntityReference((EntityReference) node, buf); break; } }
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(); } }
@TestTargetNew( level = TestLevel.PARTIAL, notes = "Verifies that isSupported method returns false value.", method = "isSupported", args = {java.lang.String.class, java.lang.String.class} ) public void testIsSupported4() throws Throwable { Document doc; EntityReference entRef; boolean success; doc = (Document) load("staffNS", builder); entRef = doc.createEntityReference("ent1"); assertNotNull("createdEntRefNotNull", entRef); success = entRef.isSupported("XML CORE", ""); assertFalse("nodeissupported04", success); }
/** * @param xmlNode XML node * @param xmlStreamWriter XML stream writer * @throws XMLStreamException the XML stream exception */ public static void writeNode(Node xmlNode, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { if (xmlNode instanceof Element) { addElement((Element) xmlNode, xmlStreamWriter); } else if (xmlNode instanceof Text) { xmlStreamWriter.writeCharacters(xmlNode.getNodeValue()); } else if (xmlNode instanceof CDATASection) { xmlStreamWriter.writeCData(((CDATASection) xmlNode).getData()); } else if (xmlNode instanceof Comment) { xmlStreamWriter.writeComment(((Comment) xmlNode).getData()); } else if (xmlNode instanceof EntityReference) { xmlStreamWriter.writeEntityRef(xmlNode.getNodeValue()); } else if (xmlNode instanceof ProcessingInstruction) { ProcessingInstruction procInst = (ProcessingInstruction) xmlNode; xmlStreamWriter.writeProcessingInstruction(procInst.getTarget(), procInst.getData()); } else if (xmlNode instanceof Document) { writeToDocument((Document) xmlNode, xmlStreamWriter); } }
/** * * @param xmlNode * @param xmlStreamWriter * @throws XMLStreamException */ public static void writeNode(Node xmlNode, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { if (xmlNode instanceof Element) { addElement((Element) xmlNode, xmlStreamWriter); } else if (xmlNode instanceof Text) { xmlStreamWriter.writeCharacters(((Text) xmlNode).getNodeValue()); } else if (xmlNode instanceof CDATASection) { xmlStreamWriter.writeCData(((CDATASection) xmlNode).getData()); } else if (xmlNode instanceof Comment) { xmlStreamWriter.writeComment(((Comment) xmlNode).getData()); } else if (xmlNode instanceof EntityReference) { xmlStreamWriter.writeEntityRef(((EntityReference) xmlNode) .getNodeValue()); } else if (xmlNode instanceof ProcessingInstruction) { ProcessingInstruction procInst = (ProcessingInstruction) xmlNode; xmlStreamWriter.writeProcessingInstruction(procInst.getTarget(), procInst.getData()); } else if (xmlNode instanceof Document) { writeToDocument((Document) xmlNode, xmlStreamWriter); } }
public static String getTextForNode(Node node) { StringBuffer sb = new StringBuffer(); NodeList children = node.getChildNodes(); if (children.getLength() == 0) return null; for (int i = 0; i < children.getLength(); ++i) { Node n = children.item(i); if (n instanceof Text) sb.append(n.getNodeValue()); else if (n instanceof EntityReference) { String s = getTextForNode(n); if (s == null) return null; else sb.append(s); } else return null; } return sb.toString(); }
@Override public String getTextValue(Element valueEle) { if (valueEle != null) { StringBuilder sb = new StringBuilder(); NodeList nl = valueEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node item = nl.item(i); if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) { sb.append(item.getNodeValue()); } } return sb.toString().trim(); } return null; }
/** * Write. * * @param node * the node */ protected void write(EntityReference node) { printWriter.print('&'); printWriter.print(node.getNodeName()); printWriter.print(';'); printWriter.flush(); }
public void print(Node node) throws XMLStreamException { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: visitDocument((Document) node); break; case Node.DOCUMENT_FRAGMENT_NODE: visitDocumentFragment((DocumentFragment) node); break; case Node.ELEMENT_NODE: visitElement((Element) node); break; case Node.TEXT_NODE: visitText((Text) node); break; case Node.CDATA_SECTION_NODE: visitCDATASection((CDATASection) node); break; case Node.PROCESSING_INSTRUCTION_NODE: visitProcessingInstruction((ProcessingInstruction) node); break; case Node.ENTITY_REFERENCE_NODE: visitReference((EntityReference) node); break; case Node.COMMENT_NODE: visitComment((Comment) node); break; case Node.DOCUMENT_TYPE_NODE: break; case Node.ATTRIBUTE_NODE: case Node.ENTITY_NODE: default: throw new XMLStreamException("Unexpected DOM Node Type " + node.getNodeType() ); } }
/** * Factory method; creates an EntityReference having this Document * as its OwnerDoc. * * @param name The name of the Entity we wish to refer to * * @throws DOMException(NOT_SUPPORTED_ERR) for HTML documents, where * nonstandard entities are not permitted. (HTML not yet * implemented.) */ public EntityReference createEntityReference(String name) throws DOMException { if (errorChecking && !isXMLName(name,xml11Version)) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg); } return new EntityReferenceImpl(this, name); }
@Test public void testEntityReference() { try { Document xmlDocument = createNewDocument(); EntityReference erefNode = xmlDocument.createEntityReference("entityref"); String outerXML = getOuterXML(erefNode); System.out.println("OuterXML of Comment Node is:" + outerXML); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }