2 * $Id: ElementWriter.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
8 import java.util.HashMap;
13 * ElementWriter provides destribute xml code.
15 public class ElementWriter {
17 final public static int
22 HashMap expandOptions = new HashMap();
23 int defaultExpandOption;
26 public ElementWriter() {
30 public ElementWriter(int defaultExpandOption, String indent) {
31 this.defaultExpandOption = defaultExpandOption;
35 public void setExpandOption(String elementName, int value){
36 expandOptions.put(elementName, new Integer(value));
39 public int getExpandOption(String elementName){
40 if( expandOptions.containsKey(elementName)){
41 return ((Integer)expandOptions.get(elementName)).intValue();
43 return defaultExpandOption;
46 boolean isBeginChangeLine(String elementName){
47 return (getExpandOption(elementName) & BEGIN_CHANGELINE) != 0;
50 boolean isEndChangeLine(String elementName){
51 return (getExpandOption(elementName) & END_CHANGELINE) != 0;
54 public String expandTag(Element element){
55 StringBuffer buff = new StringBuffer();
56 expandTag(element, 0, buff);
57 return buff.toString();
60 public void writeTag(Element element, OutputStream out) throws IOException {
61 OutputStreamWriter writer = new OutputStreamWriter(out);
63 writer.write("<?xml version=\"1.0\"?>\n\n");
64 writer.write(new ElementWriter().expandTag(element));
72 void expandTag(Element element, int level, StringBuffer buff){
73 expandIndent(level, buff);
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();
82 buff.append(' ' + n.getNodeName() + "=\"" + HTMLUtilities.escape(v) + "\"");
86 boolean emptyElem = element.getChildNodes().getLength() == 0;
92 NodeList childElements = element.getChildNodes();
93 if(isBeginChangeLine(elementName)){
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){
108 expandIndent(level, buff);
109 buff.append("</" + elementName + '>');
111 // already inserted change line.
112 if( isEndChangeLine(elementName) ){
117 void expandIndent(int level, StringBuffer buff){
119 for(int i=0; i<level; i++){
125 public int getDefaultExpandOption() {
126 return defaultExpandOption;
129 public void setDefaultExpandOption(int i) {
130 defaultExpandOption = i;