Organized imports.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / ElementWriter.java
1 /*
2  * $Id: ElementWriter.java,v 1.2 2005-05-06 00:57:33 stefanbjarni 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
24                 BEGIN_CHANGELINE = 1,
25                 END_CHANGELINE = 2;
26         
27         boolean trim = true;
28         HashMap expandOptions = new HashMap();
29         int defaultExpandOption;
30         String indent;
31         
32         public ElementWriter() {
33                 this(0, "  ");
34         }
35         
36         public ElementWriter(int defaultExpandOption, String indent) {
37                 this.defaultExpandOption = defaultExpandOption;
38                 this.indent = indent;
39         }
40
41         public void setExpandOption(String elementName, int value){
42                 expandOptions.put(elementName, new Integer(value));
43         }
44         
45         public int getExpandOption(String elementName){
46                 if( expandOptions.containsKey(elementName)){
47                         return ((Integer)expandOptions.get(elementName)).intValue();
48                 }
49                 return defaultExpandOption;
50         }
51         
52         boolean isBeginChangeLine(String elementName){
53                 return (getExpandOption(elementName) & BEGIN_CHANGELINE) != 0;
54         }
55         
56         boolean isEndChangeLine(String elementName){
57                 return (getExpandOption(elementName) & END_CHANGELINE) != 0;
58         }
59
60         public String expandTag(Element element){
61                 StringBuffer buff = new StringBuffer();
62                 expandTag(element, 0, buff);
63                 return buff.toString();
64         }
65         
66         public void writeTag(Element element, OutputStream out) throws IOException {
67                 OutputStreamWriter writer = new OutputStreamWriter(out);
68                 try{
69                         writer.write("<?xml version=\"1.0\"?>\n\n");
70                         writer.write(new ElementWriter().expandTag(element));
71                 }finally{
72                         if(writer != null){
73                                 writer.close();
74                         }
75                 }
76         }
77         
78         void expandTag(Element element, int level, StringBuffer buff){
79                 expandIndent(level, buff);
80                 
81                 String elementName = element.getNodeName();
82                 buff.append('<' + elementName );
83                 NamedNodeMap attrs = element.getAttributes();
84                 for(int i=0; i<attrs.getLength(); i++){
85                         Node n = attrs.item(i);
86                         String v = n.getNodeValue();
87                         if(v != null){
88                                 buff.append(' ' + n.getNodeName() + "=\"" + HTMLUtilities.escape(v) + "\"");
89                         }
90                 }
91                 
92                 boolean emptyElem = element.getChildNodes().getLength() == 0;
93                 if(emptyElem){
94                         buff.append(" /");
95                 }
96                 buff.append('>');
97                 if(!emptyElem ){
98                         NodeList childElements = element.getChildNodes();
99                         if(isBeginChangeLine(elementName)){
100                                 buff.append('\n');
101                         }
102                         for(int i=0; i<childElements.getLength(); i++){
103                                 Node node = childElements.item(i);
104                                 if( node instanceof Element){
105                                         expandTag( (Element)node, level+1, buff);
106                                 }else if (node instanceof Text){
107                                         String text = ((Text)node).getNodeValue();
108                                         if(trim && (text = text.trim()).length() == 0){
109                                                 continue;
110                                         }
111                                         buff.append(text);
112                                 }
113                         }
114                         expandIndent(level, buff);
115                         buff.append("</" + elementName + '>');
116                 }
117                 // already inserted change line.
118                 if( isEndChangeLine(elementName) ){
119                         buff.append('\n');
120                 }
121         }
122         
123         void expandIndent(int level, StringBuffer buff){
124                 if(indent != null){
125                         for(int i=0; i<level; i++){
126                                 buff.append(indent);
127                         }
128                 }
129         }
130
131         public int getDefaultExpandOption() {
132                 return defaultExpandOption;
133         }
134
135         public void setDefaultExpandOption(int i) {
136                 defaultExpandOption = i;
137         }
138
139 }