com.gargoylesoftware.htmlunit.javascript.host.xml.XMLSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-client-compiler-deps Show documentation
Show all versions of vaadin-client-compiler-deps Show documentation
Vaadin is a web application framework for Rich Internet Applications (RIA).
Vaadin enables easy development and maintenance of fast and
secure rich web
applications with a stunning look and feel and a wide browser support.
It features a server-side architecture with the majority of the logic
running
on the server. Ajax technology is used at the browser-side to ensure a
rich
and interactive user experience.
/*
* Copyright (c) 2002-2011 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.javascript.host.xml;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.w3c.dom.NamedNodeMap;
import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.html.DomAttr;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlAbbreviated;
import com.gargoylesoftware.htmlunit.html.HtmlAcronym;
import com.gargoylesoftware.htmlunit.html.HtmlAddress;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlApplet;
import com.gargoylesoftware.htmlunit.html.HtmlAudio;
import com.gargoylesoftware.htmlunit.html.HtmlBackgroundSound;
import com.gargoylesoftware.htmlunit.html.HtmlBidirectionalOverride;
import com.gargoylesoftware.htmlunit.html.HtmlBig;
import com.gargoylesoftware.htmlunit.html.HtmlBlink;
import com.gargoylesoftware.htmlunit.html.HtmlBlockQuote;
import com.gargoylesoftware.htmlunit.html.HtmlBody;
import com.gargoylesoftware.htmlunit.html.HtmlBold;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlCanvas;
import com.gargoylesoftware.htmlunit.html.HtmlCaption;
import com.gargoylesoftware.htmlunit.html.HtmlCenter;
import com.gargoylesoftware.htmlunit.html.HtmlCitation;
import com.gargoylesoftware.htmlunit.html.HtmlCode;
import com.gargoylesoftware.htmlunit.html.HtmlDefinition;
import com.gargoylesoftware.htmlunit.html.HtmlDefinitionDescription;
import com.gargoylesoftware.htmlunit.html.HtmlDefinitionList;
import com.gargoylesoftware.htmlunit.html.HtmlDefinitionTerm;
import com.gargoylesoftware.htmlunit.html.HtmlDeletedText;
import com.gargoylesoftware.htmlunit.html.HtmlDirectory;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlEmbed;
import com.gargoylesoftware.htmlunit.html.HtmlEmphasis;
import com.gargoylesoftware.htmlunit.html.HtmlExample;
import com.gargoylesoftware.htmlunit.html.HtmlFieldSet;
import com.gargoylesoftware.htmlunit.html.HtmlFont;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlFrame;
import com.gargoylesoftware.htmlunit.html.HtmlFrameSet;
import com.gargoylesoftware.htmlunit.html.HtmlHead;
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
import com.gargoylesoftware.htmlunit.html.HtmlHeading3;
import com.gargoylesoftware.htmlunit.html.HtmlHeading4;
import com.gargoylesoftware.htmlunit.html.HtmlHeading5;
import com.gargoylesoftware.htmlunit.html.HtmlHeading6;
import com.gargoylesoftware.htmlunit.html.HtmlHtml;
import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame;
import com.gargoylesoftware.htmlunit.html.HtmlInlineQuotation;
import com.gargoylesoftware.htmlunit.html.HtmlInsertedText;
import com.gargoylesoftware.htmlunit.html.HtmlIsIndex;
import com.gargoylesoftware.htmlunit.html.HtmlItalic;
import com.gargoylesoftware.htmlunit.html.HtmlKeyboard;
import com.gargoylesoftware.htmlunit.html.HtmlLabel;
import com.gargoylesoftware.htmlunit.html.HtmlLegend;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlListing;
import com.gargoylesoftware.htmlunit.html.HtmlMap;
import com.gargoylesoftware.htmlunit.html.HtmlMarquee;
import com.gargoylesoftware.htmlunit.html.HtmlMenu;
import com.gargoylesoftware.htmlunit.html.HtmlMultiColumn;
import com.gargoylesoftware.htmlunit.html.HtmlNoBreak;
import com.gargoylesoftware.htmlunit.html.HtmlNoEmbed;
import com.gargoylesoftware.htmlunit.html.HtmlNoFrames;
import com.gargoylesoftware.htmlunit.html.HtmlNoScript;
import com.gargoylesoftware.htmlunit.html.HtmlObject;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlOptionGroup;
import com.gargoylesoftware.htmlunit.html.HtmlOrderedList;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
import com.gargoylesoftware.htmlunit.html.HtmlPlainText;
import com.gargoylesoftware.htmlunit.html.HtmlPreformattedText;
import com.gargoylesoftware.htmlunit.html.HtmlS;
import com.gargoylesoftware.htmlunit.html.HtmlSample;
import com.gargoylesoftware.htmlunit.html.HtmlScript;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSmall;
import com.gargoylesoftware.htmlunit.html.HtmlSource;
import com.gargoylesoftware.htmlunit.html.HtmlSpacer;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlStrike;
import com.gargoylesoftware.htmlunit.html.HtmlStrong;
import com.gargoylesoftware.htmlunit.html.HtmlStyle;
import com.gargoylesoftware.htmlunit.html.HtmlSubscript;
import com.gargoylesoftware.htmlunit.html.HtmlSuperscript;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableBody;
import com.gargoylesoftware.htmlunit.html.HtmlTableColumn;
import com.gargoylesoftware.htmlunit.html.HtmlTableColumnGroup;
import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableFooter;
import com.gargoylesoftware.htmlunit.html.HtmlTableHeader;
import com.gargoylesoftware.htmlunit.html.HtmlTableHeaderCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTeletype;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTitle;
import com.gargoylesoftware.htmlunit.html.HtmlUnderlined;
import com.gargoylesoftware.htmlunit.html.HtmlUnorderedList;
import com.gargoylesoftware.htmlunit.html.HtmlVariable;
import com.gargoylesoftware.htmlunit.html.HtmlVideo;
import com.gargoylesoftware.htmlunit.html.HtmlWordBreak;
import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
import com.gargoylesoftware.htmlunit.javascript.host.Document;
import com.gargoylesoftware.htmlunit.javascript.host.DocumentFragment;
import com.gargoylesoftware.htmlunit.javascript.host.Element;
import com.gargoylesoftware.htmlunit.javascript.host.Node;
import com.gargoylesoftware.htmlunit.util.StringUtils;
/**
* A JavaScript object for XMLSerializer.
*
* @version $Revision: 6350 $
* @author Ahmed Ashour
* @author Darrell DeBoer
* @author Ronald Brill
*/
public class XMLSerializer extends SimpleScriptable {
// this is a bit strange but it is the way FF works
// output of empty tags are not allowed for this html tags
private static final Set NON_EMPTY_TAGS = new HashSet(Arrays.asList(
HtmlAbbreviated.TAG_NAME, HtmlAcronym.TAG_NAME,
HtmlAnchor.TAG_NAME, HtmlApplet.TAG_NAME, HtmlAddress.TAG_NAME, HtmlAudio.TAG_NAME,
HtmlBackgroundSound.TAG_NAME,
HtmlBidirectionalOverride.TAG_NAME, HtmlBig.TAG_NAME, HtmlBlink.TAG_NAME,
HtmlBlockQuote.TAG_NAME, HtmlBody.TAG_NAME, HtmlBold.TAG_NAME,
HtmlButton.TAG_NAME, HtmlCanvas.TAG_NAME, HtmlCaption.TAG_NAME,
HtmlCenter.TAG_NAME, HtmlCitation.TAG_NAME, HtmlCode.TAG_NAME,
HtmlDefinition.TAG_NAME, HtmlDefinitionDescription.TAG_NAME,
HtmlDeletedText.TAG_NAME, HtmlDirectory.TAG_NAME,
HtmlDivision.TAG_NAME,
HtmlDefinitionList.TAG_NAME,
HtmlDefinitionTerm.TAG_NAME, HtmlEmbed.TAG_NAME,
HtmlEmphasis.TAG_NAME, HtmlFieldSet.TAG_NAME,
HtmlFont.TAG_NAME, HtmlForm.TAG_NAME,
HtmlFrame.TAG_NAME, HtmlFrameSet.TAG_NAME, HtmlHeading1.TAG_NAME,
HtmlHeading2.TAG_NAME, HtmlHeading3.TAG_NAME,
HtmlHeading4.TAG_NAME, HtmlHeading5.TAG_NAME,
HtmlHeading6.TAG_NAME, HtmlHead.TAG_NAME,
HtmlHtml.TAG_NAME, HtmlInlineFrame.TAG_NAME,
HtmlInsertedText.TAG_NAME, HtmlIsIndex.TAG_NAME,
HtmlItalic.TAG_NAME, HtmlKeyboard.TAG_NAME, HtmlLabel.TAG_NAME,
HtmlLegend.TAG_NAME, HtmlListing.TAG_NAME, HtmlListItem.TAG_NAME,
HtmlMap.TAG_NAME, HtmlMarquee.TAG_NAME,
HtmlMenu.TAG_NAME, HtmlMultiColumn.TAG_NAME,
HtmlNoBreak.TAG_NAME, HtmlNoEmbed.TAG_NAME, HtmlNoFrames.TAG_NAME,
HtmlNoScript.TAG_NAME, HtmlObject.TAG_NAME, HtmlOrderedList.TAG_NAME,
HtmlOptionGroup.TAG_NAME, HtmlOption.TAG_NAME, HtmlParagraph.TAG_NAME,
HtmlPlainText.TAG_NAME, HtmlPreformattedText.TAG_NAME,
HtmlInlineQuotation.TAG_NAME, HtmlS.TAG_NAME, HtmlSample.TAG_NAME,
HtmlScript.TAG_NAME, HtmlSelect.TAG_NAME, HtmlSmall.TAG_NAME,
HtmlSource.TAG_NAME, HtmlSpacer.TAG_NAME, HtmlSpan.TAG_NAME,
HtmlStrike.TAG_NAME, HtmlStrong.TAG_NAME, HtmlStyle.TAG_NAME,
HtmlSubscript.TAG_NAME, HtmlSuperscript.TAG_NAME, HtmlTitle.TAG_NAME,
HtmlTable.TAG_NAME, HtmlTableColumn.TAG_NAME, HtmlTableColumnGroup.TAG_NAME,
HtmlTableBody.TAG_NAME, HtmlTableDataCell.TAG_NAME, HtmlTableHeaderCell.TAG_NAME,
HtmlTableRow.TAG_NAME, HtmlTextArea.TAG_NAME, HtmlTableFooter.TAG_NAME,
HtmlTableHeader.TAG_NAME, HtmlTeletype.TAG_NAME, HtmlUnderlined.TAG_NAME,
HtmlUnorderedList.TAG_NAME, HtmlVariable.TAG_NAME, HtmlVideo.TAG_NAME,
HtmlWordBreak.TAG_NAME, HtmlExample.TAG_NAME
));
/**
* JavaScript constructor.
*/
public void jsConstructor() {
// Empty.
}
/**
* The subtree rooted by the specified element is serialized to a string.
* @param root the root of the subtree to be serialized (this may be any node, even a document)
* @return the serialized string
*/
public String jsxFunction_serializeToString(Node root) {
if (root == null) {
return "";
}
if (root instanceof Document) {
root = ((Document) root).jsxGet_documentElement();
}
else if (root instanceof DocumentFragment) {
root = root.jsxGet_firstChild();
}
if (root instanceof Element) {
final StringBuilder buffer = new StringBuilder();
final DomNode node = root.getDomNodeOrDie();
final boolean nodeNameAsUpperCase =
getBrowserVersion().hasFeature(BrowserVersionFeatures.JS_XML_SERIALIZER_NODE_AS_UPPERCASE)
&& (node.getPage() instanceof HtmlPage);
final boolean appendCrlf =
getBrowserVersion().hasFeature(BrowserVersionFeatures.JS_XML_SERIALIZER_APPENDS_CRLF);
final boolean addXhtmlNamespace =
getBrowserVersion().hasFeature(BrowserVersionFeatures.JS_XML_SERIALIZER_ADD_XHTML_NAMESPACE);
String forcedNamespace = null;
if (addXhtmlNamespace) {
if (node.getPage() instanceof HtmlPage) {
forcedNamespace = "http://www.w3.org/1999/xhtml";
}
}
toXml(1, node, buffer, forcedNamespace, nodeNameAsUpperCase, appendCrlf);
if (appendCrlf) {
buffer.append("\r\n");
}
return buffer.toString();
}
return root.getDomNodeOrDie().asXml();
}
private void toXml(final int indent,
final DomNode node, final StringBuilder buffer,
final String foredNamespace, final boolean nodeNameAsUpperCase, final boolean appendCrLf) {
String nodeName = node.getNodeName();
if (nodeNameAsUpperCase) {
nodeName = nodeName.toUpperCase();
}
buffer.append('<').append(nodeName);
String optionalPrefix = "";
final String namespaceURI = node.getNamespaceURI();
final String prefix = node.getPrefix();
if (namespaceURI != null && prefix != null) {
boolean sameNamespace = false;
for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement;
parentNode = parentNode.getParentNode()) {
if (namespaceURI.equals(parentNode.getNamespaceURI())) {
sameNamespace = true;
}
}
if (node.getParentNode() == null || !sameNamespace) {
((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
}
}
else if (foredNamespace != null) {
buffer.append(" xmlns=\"").append(foredNamespace).append('"');
optionalPrefix = " ";
}
final NamedNodeMap attributesMap = node.getAttributes();
for (int i = 0; i < attributesMap.getLength(); i++) {
final DomAttr attrib = (DomAttr) attributesMap.item(i);
buffer.append(' ').append(attrib.getQualifiedName()).append('=')
.append('"').append(attrib.getValue()).append('"');
}
boolean startTagClosed = false;
for (final DomNode child : node.getChildren()) {
if (!startTagClosed) {
buffer.append(optionalPrefix).append('>');
startTagClosed = true;
}
switch (child.getNodeType()) {
case Node.ELEMENT_NODE:
toXml(indent + 1, child, buffer, null, nodeNameAsUpperCase, appendCrLf);
break;
case Node.TEXT_NODE:
String value = child.getNodeValue();
value = StringUtils.escapeXmlChars(value);
if (appendCrLf && org.apache.commons.lang.StringUtils.isBlank(value)) {
buffer.append("\r\n");
final DomNode sibling = child.getNextSibling();
if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
for (int i = 0; i < indent; i++) {
buffer.append('\t');
}
}
}
else {
buffer.append(value);
}
break;
case Node.CDATA_SECTION_NODE:
case Node.COMMENT_NODE:
buffer.append(child.asXml());
break;
default:
}
}
if (!startTagClosed) {
final String tagName = nodeName.toLowerCase(Locale.ENGLISH);
final boolean nonEmptyTagsSupported =
getBrowserVersion().hasFeature(BrowserVersionFeatures.JS_XML_SERIALIZER_NON_EMPTY_TAGS);
if (nonEmptyTagsSupported && NON_EMPTY_TAGS.contains(tagName)) {
buffer.append('>');
buffer.append("").append(nodeName).append('>');
}
else {
buffer.append(optionalPrefix).append("/>");
}
}
else {
buffer.append('<').append('/').append(nodeName).append('>');
}
}
}