1 11 12 package org.eclipse.pde.internal.core; 13 14 import java.io.File ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.OutputStream ; 18 import java.io.OutputStreamWriter ; 19 import java.io.Writer ; 20 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.NamedNodeMap ; 23 import org.w3c.dom.Node ; 24 import org.w3c.dom.NodeList ; 25 26 27 public class XMLPrintHandler { 28 public static final String XML_COMMENT_END_TAG = "-->"; public static final String XML_COMMENT_BEGIN_TAG = "<!--"; public static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\""; public static final String XML_HEAD_END_TAG = "?>"; public static final String XML_DBL_QUOTES = "\""; public static final String XML_SPACE = " "; public static final String XML_BEGIN_TAG = "<"; public static final String XML_END_TAG = ">"; public static final String XML_EQUAL = "="; public static final String XML_SLASH = "/"; public static final String XML_INDENT = " "; 41 42 46 public static String generateIndent(int level) { 47 StringBuffer buffer = new StringBuffer (); 48 for (int i = 0; i < level; i++) { 49 buffer.append(XML_INDENT); 50 } 51 return buffer.toString(); 52 } 53 54 public static void printBeginElement(Writer xmlWriter, String elementString, String indent, boolean terminate) throws IOException { 55 StringBuffer temp = new StringBuffer (indent); 56 temp.append(XML_BEGIN_TAG); 57 temp.append(elementString); 58 if (terminate) 59 temp.append(XML_SLASH); 60 temp.append(XML_END_TAG); 61 temp.append("\n"); xmlWriter.write(temp.toString()); 63 64 } 65 66 public static void printEndElement(Writer xmlWriter, String elementString, String indent) throws IOException { 67 StringBuffer temp = new StringBuffer (indent); 68 temp.append(XML_BEGIN_TAG); 69 temp.append(XML_SLASH).append(elementString).append(XML_END_TAG).append("\n"); xmlWriter.write(temp.toString()); 71 72 } 73 74 public static void printText(Writer xmlWriter, String text, String indent) throws IOException { 75 StringBuffer temp = new StringBuffer (indent); 76 temp.append(encode(text).toString()); 77 temp.append("\n"); xmlWriter.write(temp.toString()); 79 } 80 81 public static void printComment(Writer xmlWriter, String comment, String indent)throws IOException { 82 StringBuffer temp = new StringBuffer ("\n"); temp.append(indent); 84 temp.append(XML_COMMENT_BEGIN_TAG); 85 temp.append(encode(comment).toString()).append(XML_COMMENT_END_TAG).append("\n\n"); xmlWriter.write(temp.toString()); 87 } 88 89 public static void printHead(Writer xmlWriter, String encoding) throws IOException { 90 StringBuffer temp = new StringBuffer (XML_HEAD); 91 temp.append(encoding).append(XML_DBL_QUOTES).append(XML_HEAD_END_TAG).append("\n"); xmlWriter.write(temp.toString()); 93 } 94 95 public static String wrapAttributeForPrint(String attribute, String value) throws IOException { 96 StringBuffer temp = new StringBuffer (XML_SPACE); 97 temp.append(attribute).append(XML_EQUAL).append(XML_DBL_QUOTES) 98 .append(encode(value).toString()).append(XML_DBL_QUOTES); 99 return temp.toString(); 100 } 101 102 public static String wrapAttribute(String attribute, String value) { 103 StringBuffer buffer = new StringBuffer (XML_SPACE); 104 buffer.append(attribute); 105 buffer.append(XML_EQUAL); 106 buffer.append(XML_DBL_QUOTES); 107 buffer.append(value); 108 buffer.append(XML_DBL_QUOTES); 109 return buffer.toString(); 110 } 111 112 public static void printNode(Writer xmlWriter, Node node,String encoding, String indent) throws IOException { 113 if (node == null) { 114 return; 115 } 116 117 switch (node.getNodeType()) { 118 case Node.DOCUMENT_NODE: { 119 printHead(xmlWriter,encoding); 120 printNode(xmlWriter, ((Document ) node).getDocumentElement(),encoding, indent); 121 break; 122 } 123 case Node.ELEMENT_NODE: { 124 StringBuffer tempElementString = new StringBuffer (node.getNodeName()); 126 NamedNodeMap attributeList = node.getAttributes(); 127 if ( attributeList != null ) { 128 for(int i= 0; i <attributeList.getLength();i++){ 129 Node attribute = attributeList.item(i); 130 tempElementString.append(wrapAttributeForPrint(attribute.getNodeName(),attribute.getNodeValue())); 131 } 132 } 133 134 NodeList childNodes = node.getChildNodes(); 136 int length = childNodes.getLength(); 137 printBeginElement(xmlWriter,tempElementString.toString(), indent, length == 0); 138 139 for (int i = 0; i < length; i++) 140 printNode(xmlWriter, childNodes.item(i),encoding, indent + "\t"); 142 if (length > 0) 143 printEndElement(xmlWriter,node.getNodeName(), indent); 144 break; 145 } 146 147 case Node.TEXT_NODE: { 148 xmlWriter.write(encode(node.getNodeValue()).toString()); 149 break; 150 } 151 default: { 152 throw new UnsupportedOperationException ("Unsupported XML Node Type."); } 154 } 155 156 } 157 158 public static StringBuffer encode(String value) { 159 StringBuffer buf = new StringBuffer (); 160 for (int i = 0; i < value.length(); i++) { 161 char c = value.charAt(i); 162 switch (c) { 163 case '&' : 164 buf.append("&"); break; 166 case '<' : 167 buf.append("<"); break; 169 case '>' : 170 buf.append(">"); break; 172 case '\'' : 173 buf.append("'"); break; 175 case '\"' : 176 buf.append("""); break; 178 default : 179 buf.append(c); 180 break; 181 } 182 } 183 return buf; 184 } 185 186 public static void writeFile(Document doc, File file) throws IOException { 187 Writer writer = null; 188 OutputStream out = null; 189 try { 190 out = new FileOutputStream (file); 191 writer = new OutputStreamWriter (out, "UTF-8"); XMLPrintHandler.printNode(writer, doc, "UTF-8", ""); } finally { 194 try { 195 if (writer != null) 196 writer.close(); 197 } catch (IOException e1) { 198 } 199 try { 200 if (out != null) 201 out.close(); 202 } catch (IOException e1) { 203 } 204 } 205 } 206 207 } 208 | Popular Tags |