refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / wizards / html / ElementWriter.java
1 /*
2  * $Id: ElementWriter.java,v 1.3 2006-10-21 23:18:43 pombredanne Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
7 import java.io.IOException;
8 import java.io.OutputStream;
9 import java.io.OutputStreamWriter;
10 import java.util.HashMap;
11
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;
17
18 /**
19  * ElementWriter provides destribute xml code.
20  */
21 public class ElementWriter {
22
23         final public static int BEGIN_CHANGELINE = 1, END_CHANGELINE = 2;
24
25         boolean trim = true;
26
27         HashMap expandOptions = new HashMap();
28
29         int defaultExpandOption;
30
31         String indent;
32
33         public ElementWriter() {
34                 this(0, "  ");
35         }
36
37         public ElementWriter(int defaultExpandOption, String indent) {
38                 this.defaultExpandOption = defaultExpandOption;
39                 this.indent = indent;
40         }
41
42         public void setExpandOption(String elementName, int value) {
43                 expandOptions.put(elementName, new Integer(value));
44         }
45
46         public int getExpandOption(String elementName) {
47                 if (expandOptions.containsKey(elementName)) {
48                         return ((Integer) expandOptions.get(elementName)).intValue();
49                 }
50                 return defaultExpandOption;
51         }
52
53         boolean isBeginChangeLine(String elementName) {
54                 return (getExpandOption(elementName) & BEGIN_CHANGELINE) != 0;
55         }
56
57         boolean isEndChangeLine(String elementName) {
58                 return (getExpandOption(elementName) & END_CHANGELINE) != 0;
59         }
60
61         public String expandTag(Element element) {
62                 StringBuffer buff = new StringBuffer();
63                 expandTag(element, 0, buff);
64                 return buff.toString();
65         }
66
67         public void writeTag(Element element, OutputStream out) throws IOException {
68                 OutputStreamWriter writer = new OutputStreamWriter(out);
69                 try {
70                         writer.write("<?xml version=\"1.0\"?>\n\n");
71                         writer.write(new ElementWriter().expandTag(element));
72                 } finally {
73                         if (writer != null) {
74                                 writer.close();
75                         }
76                 }
77         }
78
79         void expandTag(Element element, int level, StringBuffer buff) {
80                 expandIndent(level, buff);
81
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();
88                         if (v != null) {
89                                 buff.append(' ' + n.getNodeName() + "=\""
90                                                 + HTMLUtilities.escape(v) + "\"");
91                         }
92                 }
93
94                 boolean emptyElem = element.getChildNodes().getLength() == 0;
95                 if (emptyElem) {
96                         buff.append(" /");
97                 }
98                 buff.append('>');
99                 if (!emptyElem) {
100                         NodeList childElements = element.getChildNodes();
101                         if (isBeginChangeLine(elementName)) {
102                                 buff.append('\n');
103                         }
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) {
111                                                 continue;
112                                         }
113                                         buff.append(text);
114                                 }
115                         }
116                         expandIndent(level, buff);
117                         buff.append("</" + elementName + '>');
118                 }
119                 // already inserted change line.
120                 if (isEndChangeLine(elementName)) {
121                         buff.append('\n');
122                 }
123         }
124
125         void expandIndent(int level, StringBuffer buff) {
126                 if (indent != null) {
127                         for (int i = 0; i < level; i++) {
128                                 buff.append(indent);
129                         }
130                 }
131         }
132
133         public int getDefaultExpandOption() {
134                 return defaultExpandOption;
135         }
136
137         public void setDefaultExpandOption(int i) {
138                 defaultExpandOption = i;
139         }
140
141 }