Merged "Open PHP Declaration" and "Open Include File" into one action "Open Declarati...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / ElementWriter.java
1 /*
2  * $Id: ElementWriter.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
7 import java.io.*;
8 import java.util.HashMap;
9
10 import org.w3c.dom.*;
11
12 /**
13  * ElementWriter provides destribute xml code.
14  */
15 public class ElementWriter {
16
17         final public static int
18                 BEGIN_CHANGELINE = 1,
19                 END_CHANGELINE = 2;
20         
21         boolean trim = true;
22         HashMap expandOptions = new HashMap();
23         int defaultExpandOption;
24         String indent;
25         
26         public ElementWriter() {
27                 this(0, "  ");
28         }
29         
30         public ElementWriter(int defaultExpandOption, String indent) {
31                 this.defaultExpandOption = defaultExpandOption;
32                 this.indent = indent;
33         }
34
35         public void setExpandOption(String elementName, int value){
36                 expandOptions.put(elementName, new Integer(value));
37         }
38         
39         public int getExpandOption(String elementName){
40                 if( expandOptions.containsKey(elementName)){
41                         return ((Integer)expandOptions.get(elementName)).intValue();
42                 }
43                 return defaultExpandOption;
44         }
45         
46         boolean isBeginChangeLine(String elementName){
47                 return (getExpandOption(elementName) & BEGIN_CHANGELINE) != 0;
48         }
49         
50         boolean isEndChangeLine(String elementName){
51                 return (getExpandOption(elementName) & END_CHANGELINE) != 0;
52         }
53
54         public String expandTag(Element element){
55                 StringBuffer buff = new StringBuffer();
56                 expandTag(element, 0, buff);
57                 return buff.toString();
58         }
59         
60         public void writeTag(Element element, OutputStream out) throws IOException {
61                 OutputStreamWriter writer = new OutputStreamWriter(out);
62                 try{
63                         writer.write("<?xml version=\"1.0\"?>\n\n");
64                         writer.write(new ElementWriter().expandTag(element));
65                 }finally{
66                         if(writer != null){
67                                 writer.close();
68                         }
69                 }
70         }
71         
72         void expandTag(Element element, int level, StringBuffer buff){
73                 expandIndent(level, buff);
74                 
75                 String elementName = element.getNodeName();
76                 buff.append('<' + elementName );
77                 NamedNodeMap attrs = element.getAttributes();
78                 for(int i=0; i<attrs.getLength(); i++){
79                         Node n = attrs.item(i);
80                         String v = n.getNodeValue();
81                         if(v != null){
82                                 buff.append(' ' + n.getNodeName() + "=\"" + HTMLUtilities.escape(v) + "\"");
83                         }
84                 }
85                 
86                 boolean emptyElem = element.getChildNodes().getLength() == 0;
87                 if(emptyElem){
88                         buff.append(" /");
89                 }
90                 buff.append('>');
91                 if(!emptyElem ){
92                         NodeList childElements = element.getChildNodes();
93                         if(isBeginChangeLine(elementName)){
94                                 buff.append('\n');
95                         }
96                         for(int i=0; i<childElements.getLength(); i++){
97                                 Node node = childElements.item(i);
98                                 if( node instanceof Element){
99                                         expandTag( (Element)node, level+1, buff);
100                                 }else if (node instanceof Text){
101                                         String text = ((Text)node).getNodeValue();
102                                         if(trim && (text = text.trim()).length() == 0){
103                                                 continue;
104                                         }
105                                         buff.append(text);
106                                 }
107                         }
108                         expandIndent(level, buff);
109                         buff.append("</" + elementName + '>');
110                 }
111                 // already inserted change line.
112                 if( isEndChangeLine(elementName) ){
113                         buff.append('\n');
114                 }
115         }
116         
117         void expandIndent(int level, StringBuffer buff){
118                 if(indent != null){
119                         for(int i=0; i<level; i++){
120                                 buff.append(indent);
121                         }
122                 }
123         }
124
125         public int getDefaultExpandOption() {
126                 return defaultExpandOption;
127         }
128
129         public void setDefaultExpandOption(int i) {
130                 defaultExpandOption = i;
131         }
132
133 }