2 * $Id: ElementWriter.java,v 1.3 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import java.io.IOException;
8 import java.io.OutputStream;
9 import java.io.OutputStreamWriter;
10 import java.util.HashMap;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.NamedNodeMap;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16 import org.w3c.dom.Text;
19 * ElementWriter provides destribute xml code.
21 public class ElementWriter {
23 final public static int BEGIN_CHANGELINE = 1, END_CHANGELINE = 2;
27 HashMap expandOptions = new HashMap();
29 int defaultExpandOption;
33 public ElementWriter() {
37 public ElementWriter(int defaultExpandOption, String indent) {
38 this.defaultExpandOption = defaultExpandOption;
42 public void setExpandOption(String elementName, int value) {
43 expandOptions.put(elementName, new Integer(value));
46 public int getExpandOption(String elementName) {
47 if (expandOptions.containsKey(elementName)) {
48 return ((Integer) expandOptions.get(elementName)).intValue();
50 return defaultExpandOption;
53 boolean isBeginChangeLine(String elementName) {
54 return (getExpandOption(elementName) & BEGIN_CHANGELINE) != 0;
57 boolean isEndChangeLine(String elementName) {
58 return (getExpandOption(elementName) & END_CHANGELINE) != 0;
61 public String expandTag(Element element) {
62 StringBuffer buff = new StringBuffer();
63 expandTag(element, 0, buff);
64 return buff.toString();
67 public void writeTag(Element element, OutputStream out) throws IOException {
68 OutputStreamWriter writer = new OutputStreamWriter(out);
70 writer.write("<?xml version=\"1.0\"?>\n\n");
71 writer.write(new ElementWriter().expandTag(element));
79 void expandTag(Element element, int level, StringBuffer buff) {
80 expandIndent(level, buff);
82 String elementName = element.getNodeName();
83 buff.append('<' + elementName);
84 NamedNodeMap attrs = element.getAttributes();
85 for (int i = 0; i < attrs.getLength(); i++) {
86 Node n = attrs.item(i);
87 String v = n.getNodeValue();
89 buff.append(' ' + n.getNodeName() + "=\""
90 + HTMLUtilities.escape(v) + "\"");
94 boolean emptyElem = element.getChildNodes().getLength() == 0;
100 NodeList childElements = element.getChildNodes();
101 if (isBeginChangeLine(elementName)) {
104 for (int i = 0; i < childElements.getLength(); i++) {
105 Node node = childElements.item(i);
106 if (node instanceof Element) {
107 expandTag((Element) node, level + 1, buff);
108 } else if (node instanceof Text) {
109 String text = ((Text) node).getNodeValue();
110 if (trim && (text = text.trim()).length() == 0) {
116 expandIndent(level, buff);
117 buff.append("</" + elementName + '>');
119 // already inserted change line.
120 if (isEndChangeLine(elementName)) {
125 void expandIndent(int level, StringBuffer buff) {
126 if (indent != null) {
127 for (int i = 0; i < level; i++) {
133 public int getDefaultExpandOption() {
134 return defaultExpandOption;
137 public void setDefaultExpandOption(int i) {
138 defaultExpandOption = i;