1 19 package org.netbeans.modules.mdrxml.util; 20 21 import javax.jmi.reflect.*; 22 import xmlmodel.*; 23 24 import java.util.*; 25 import java.io.*; 26 27 public class XMLGenerator { 28 29 private static final char QUOTE = '\''; 30 private static final String INDENT = " "; 31 private static final int INDENT_LENGTH = INDENT.length (); 32 36 private static final int MAX = 70; 37 38 private PrintStream stream; 40 private boolean hasContent = true; private boolean hasCharacters = false; 44 private String indentSpaces = ""; 46 private int charsCount = 0; 48 49 53 58 private void write (String text) { 59 stream.print (text); 60 } 61 62 65 private void writeln () { 66 stream.println (); 67 charsCount = 0; 68 } 69 70 74 private String replaceSpecialChars (String s) { 75 int length = s.length (); 76 char [] chars = new char [6 * length]; 77 int n = 0; 78 for (int x = 0; x < length; x++) { 79 char c = s.charAt (x); 80 switch (c) { 81 case '&': 82 chars [n] = '&'; n++; chars [n] = 'a'; n++; 83 chars [n] = 'm'; n++; chars [n] = 'p'; n++; 84 chars [n] = ';'; n++; 85 break; 86 case '\'': 87 chars [n] = '&'; n++; chars [n] = 'a'; n++; 88 chars [n] = 'p'; n++; chars [n] = 'o'; n++; 89 chars [n] = 's'; n++; chars [n] = ';'; n++; 90 break; 91 case '\"': 92 chars [n] = '&'; n++; chars [n] = 'q'; n++; 93 chars [n] = 'u'; n++; chars [n] = 'o'; n++; 94 chars [n] = 't'; n++; chars [n] = ';'; n++; 95 break; 96 case '<': 97 chars [n] = '&'; n++; chars [n] = 'l'; n++; 98 chars [n] = 't'; n++; chars [n] = ';'; n++; 99 break; 100 case '>': 101 chars [n] = '&'; n++; chars [n] = 'g'; n++; 102 chars [n] = 't'; n++; chars [n] = ';'; n++; 103 break; 104 default: 105 chars [n] = c; n++; 106 } } return new String (chars, 0, n); 109 } 110 111 116 private void startElement (String name) { 117 if (!hasContent && !hasCharacters) { 118 write (">"); 119 writeln (); 120 } 121 hasContent = false; 122 hasCharacters = false; 123 write (indentSpaces); 124 write ("<" + name); 125 charsCount += name.length () + 1; 126 indentSpaces = indentSpaces + INDENT; 127 } 128 129 134 private void endElement (String name) { 135 indentSpaces = indentSpaces.substring (0, indentSpaces.length () - INDENT_LENGTH); 136 if (hasContent) { 137 write (indentSpaces); 138 write ("</" + name + ">"); 139 } else if (hasCharacters) { 140 write ("</" + name + ">"); 141 } else 142 write ("/>"); 143 writeln (); 144 hasContent = true; 145 } 146 147 153 private void addAttribute (String name, String value) { 154 value = replaceSpecialChars (value); 155 if (charsCount > MAX) { 157 writeln (); 158 write (indentSpaces); 159 } else { 160 write (" "); 161 charsCount++; 162 } 163 write (name + " = " + QUOTE + value + QUOTE); 164 charsCount += name.length () + value.length () + 5; 165 } 166 167 174 private void characters (String text) { 175 text = replaceSpecialChars (text); 176 if (!hasContent) 177 write (">"); 178 write (text); 179 hasCharacters = true; 180 } 181 182 184 public void generateXML (OutputStream os, ElementNode elementNode) { 186 stream = new PrintStream (os); 187 write ("<?xml version = '1.0' encoding = 'ISO-8859-1' ?>\n"); 188 generateElement (elementNode); 189 } 190 191 private void generateElement (ElementNode element) { 192 String name = element.getName (); 193 startElement (name); 194 List nodes = element.getNodes (); 195 Iterator iter = nodes.iterator (); 196 while (iter.hasNext ()) { 197 Object subNode = iter.next (); 198 if (subNode instanceof AttributeNode) { 199 AttributeNode attrNode = (AttributeNode) subNode; 200 addAttribute (attrNode.getName (), attrNode.getValue ()); 201 } } iter = nodes.iterator (); 204 while (iter.hasNext ()) { 205 Object subNode = iter.next (); 206 if (subNode instanceof TextNode) 207 characters (((TextNode) subNode).getName ()); 208 else if (subNode instanceof ElementNode) 209 generateElement ((ElementNode) subNode); 210 } 211 endElement (name); 212 } 213 214 } | Popular Tags |