/// <summary> /// Recursively appends a {@link Node} child to {@link DomNode} parent. /// </summary> /// <param name="page">the owner page of {@link DomElement}s to be created</param> /// <param name="parent">the parent DomNode</param> /// <param name="child">the child Node</param> /// <param name="handleXHTMLAsHTML">if true elements from the XHTML namespace are handled as HTML elements instead of DOM elements</param> public static void AppendChild(SgmlPage page, DomNode parent, XmlNode child, bool handleXHTMLAsHTML) { XmlDocumentType documentType = child.OwnerDocument.DocumentType; if (documentType != null && page is XmlPage) { DomDocumentType domDoctype = new DomDocumentType( page, documentType.Name, documentType.PublicId, documentType.SystemId); ((XmlPage)page).setDocumentType(domDoctype); } DomNode childXml = CreateFrom(page, child, handleXHTMLAsHTML); parent.appendChild(childXml); Copy(page, child, childXml, handleXHTMLAsHTML); }
/// <summary> /// Copy all children from 'source' to 'dest', within the context of the specified page. /// </summary> /// <param name="page">the page which the nodes belong to</param> /// <param name="source">the node to copy from</param> /// <param name="dest">the node to copy to</param> /// <param name="handleXHTMLAsHTML">if true elements from the XHTML namespace are handled as HTML elements instead of DOM elements</param> private static void Copy(SgmlPage page, XmlNode source, DomNode dest, bool handleXHTMLAsHTML) { XmlNodeList nodeChildren = source.ChildNodes; for (int i = 0; i < nodeChildren.Count; i++) { XmlNode child = nodeChildren.Item(i); switch (child.NodeType) { case XmlNodeType.Element: DomNode childXml = CreateFrom(page, child, handleXHTMLAsHTML); dest.appendChild(childXml); Copy(page, child, childXml, handleXHTMLAsHTML); break; case XmlNodeType.Text: dest.appendChild(new DomText(page, child.Value)); break; case XmlNodeType.CDATA: dest.appendChild(new DomCDataSection(page, child.Value)); break; case XmlNodeType.Comment: dest.appendChild(new DomComment(page, child.Value)); break; case XmlNodeType.ProcessingInstruction: dest.appendChild(new DomProcessingInstruction(page, child.Name, child.Value)); break; default: LOG.Warn("NodeType " + child.NodeType + " (" + child.Name + ") is not yet supported."); } } }
private static DomNode CreateFrom(SgmlPage page, XmlNode source, bool handleXHTMLAsHTML) { if (source.NodeType == XmlNodeType.Text) { return(new DomText(page, source.Value)); } if (source.NodeType == XmlNodeType.ProcessingInstruction) { return(new DomProcessingInstruction(page, source.Name, source.Value)); } if (source.NodeType == XmlNodeType.Comment) { return(new DomComment(page, source.Value)); } if (source.NodeType == XmlNodeType.DocumentType) { XmlDocumentType documentType = (XmlDocumentType)source; return(new DomDocumentType(page, documentType.Name, documentType.PublicId, documentType.SystemId)); } String ns = source.NamespaceURI; String localName = source.LocalName; if (handleXHTMLAsHTML && String.Equals(HTMLParser.XHTML_NAMESPACE, ns)) { ElementFactory factory = HTMLParser.getFactory(localName); return(factory.createElementNS(page, ns, localName, source.Attributes)); } XmlAttributeCollection nodeAttributes = source.Attributes; if (page != null && page.isHtmlPage()) { localName = localName.ToUpper(); // TODO : Locale.ENGLISH } String qualifiedName; if (source.Prefix == null) { qualifiedName = localName; } else { qualifiedName = source.Prefix + ':' + localName; } String namespaceURI = source.NamespaceURI; if (String.Equals(HTMLParser.SVG_NAMESPACE, namespaceURI)) { return(HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, nodeAttributes)); } Dictionary <String, DomAttr> attributes = new Dictionary <string, DomAttr>(); for (int i = 0; i < nodeAttributes.Count; i++) { XmlAttribute attribute = (XmlAttribute)nodeAttributes.Item(i); String attributeNamespaceURI = attribute.NamespaceURI; String attributeQualifiedName; if (attribute.Prefix != null) { attributeQualifiedName = attribute.Prefix + ':' + attribute.LocalName; } else { attributeQualifiedName = attribute.LocalName; } String value = attribute.Value; bool specified = attribute.Specified; DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.Add(attribute.Name, xmlAttribute); } return(new DomElement(namespaceURI, qualifiedName, page, attributes)); }
/// <summary> /// Copy all children from 'source' to 'dest', within the context of the specified page. /// </summary> /// <param name="page">the page which the nodes belong to</param> /// <param name="source">the node to copy from</param> /// <param name="dest">the node to copy to</param> /// <param name="handleXHTMLAsHTML">if true elements from the XHTML namespace are handled as HTML elements instead of DOM elements</param> private static void Copy(SgmlPage page, XmlNode source, DomNode dest, bool handleXHTMLAsHTML) { XmlNodeList nodeChildren = source.ChildNodes; for (int i = 0; i < nodeChildren.Count; i++) { XmlNode child = nodeChildren.Item(i); switch (child.NodeType) { case XmlNodeType.Element: DomNode childXml = CreateFrom(page, child, handleXHTMLAsHTML); dest.appendChild(childXml); Copy(page, child, childXml, handleXHTMLAsHTML); break; case XmlNodeType.Text: dest.appendChild(new DomText(page, child.Value)); break; case XmlNodeType.CDATA: dest.appendChild(new DomCDataSection(page, child.Value)); break; case XmlNodeType.Comment: dest.appendChild(new DomComment(page, child.Value)); break; case XmlNodeType.ProcessingInstruction: dest.appendChild(new DomProcessingInstruction(page, child.Name, child.Value)); break; default: LOG.Warn("NodeType " + child.NodeType + " (" + child.Name + ") is not yet supported."); } } }
private static DomNode CreateFrom(SgmlPage page, XmlNode source, bool handleXHTMLAsHTML) { if (source.NodeType == XmlNodeType.Text) { return new DomText(page, source.Value); } if (source.NodeType == XmlNodeType.ProcessingInstruction) { return new DomProcessingInstruction(page, source.Name, source.Value); } if (source.NodeType == XmlNodeType.Comment) { return new DomComment(page, source.Value); } if (source.NodeType == XmlNodeType.DocumentType) { XmlDocumentType documentType = (XmlDocumentType)source; return new DomDocumentType(page, documentType.Name, documentType.PublicId, documentType.SystemId); } String ns = source.NamespaceURI; String localName = source.LocalName; if (handleXHTMLAsHTML && String.Equals(HTMLParser.XHTML_NAMESPACE, ns)) { ElementFactory factory = HTMLParser.getFactory(localName); return factory.createElementNS(page, ns, localName, source.Attributes); } XmlAttributeCollection nodeAttributes = source.Attributes; if (page != null && page.isHtmlPage()) { localName = localName.ToUpper(); // TODO : Locale.ENGLISH } String qualifiedName; if (source.Prefix == null) { qualifiedName = localName; } else { qualifiedName = source.Prefix + ':' + localName; } String namespaceURI = source.NamespaceURI; if (String.Equals(HTMLParser.SVG_NAMESPACE, namespaceURI)) { return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, nodeAttributes); } Dictionary<String, DomAttr> attributes = new Dictionary<string, DomAttr>(); for (int i = 0; i < nodeAttributes.Count; i++) { XmlAttribute attribute = (XmlAttribute)nodeAttributes.Item(i); String attributeNamespaceURI = attribute.NamespaceURI; String attributeQualifiedName; if (attribute.Prefix != null) { attributeQualifiedName = attribute.Prefix + ':' + attribute.LocalName; } else { attributeQualifiedName = attribute.LocalName; } String value = attribute.Value; bool specified = attribute.Specified; DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.Add(attribute.Name, xmlAttribute); } return new DomElement(namespaceURI, qualifiedName, page, attributes); }
/// <summary> /// Recursively appends a {@link Node} child to {@link DomNode} parent. /// </summary> /// <param name="page">the owner page of {@link DomElement}s to be created</param> /// <param name="parent">the parent DomNode</param> /// <param name="child">the child Node</param> /// <param name="handleXHTMLAsHTML">if true elements from the XHTML namespace are handled as HTML elements instead of DOM elements</param> public static void AppendChild(SgmlPage page, DomNode parent, XmlNode child, bool handleXHTMLAsHTML) { XmlDocumentType documentType = child.OwnerDocument.DocumentType; if (documentType != null && page is XmlPage) { DomDocumentType domDoctype = new DomDocumentType( page, documentType.Name, documentType.PublicId, documentType.SystemId); ((XmlPage)page).setDocumentType(domDoctype); } DomNode childXml = CreateFrom(page, child, handleXHTMLAsHTML); parent.appendChild(childXml); Copy(page, child, childXml, handleXHTMLAsHTML); }