1 11 12 package org.eclipse.update.internal.configurator; 13 14 import java.io.*; 15 16 import org.w3c.dom.*; 17 18 19 public class XMLPrintHandler { 20 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 = "/"; 32 public static void printBeginElement(Writer xmlWriter, String elementString) throws IOException{ 33 StringBuffer temp = new StringBuffer (XML_BEGIN_TAG); 34 temp.append(elementString).append(XML_END_TAG).append("\n"); xmlWriter.write(temp.toString()); 36 37 } 38 39 public static void printEndElement(Writer xmlWriter, String elementString) throws IOException{ 40 StringBuffer temp = new StringBuffer (XML_BEGIN_TAG); 41 temp.append(XML_SLASH).append(elementString).append(XML_END_TAG).append("\n"); xmlWriter.write(temp.toString()); 43 44 } 45 46 47 public static void printText(Writer xmlWriter, String text) throws IOException{ 48 xmlWriter.write(encode(text).toString()); 49 } 50 51 public static void printComment(Writer xmlWriter, String comment)throws IOException { 52 StringBuffer temp = new StringBuffer (XML_COMMENT_BEGIN_TAG); 53 temp.append(encode(comment).toString()).append(XML_COMMENT_END_TAG).append("\n"); xmlWriter.write(temp.toString()); 55 } 56 57 public static void printHead(Writer xmlWriter, String encoding) throws IOException { 58 StringBuffer temp = new StringBuffer (XML_HEAD); 59 temp.append(encoding).append(XML_DBL_QUOTES).append(XML_HEAD_END_TAG).append("\n"); xmlWriter.write(temp.toString()); 61 } 62 63 public static String wrapAttributeForPrint(String attribute, String value) throws IOException { 64 StringBuffer temp = new StringBuffer (XML_SPACE); 65 temp.append(attribute).append(XML_EQUAL).append(XML_DBL_QUOTES) 66 .append(encode(value).toString()).append(XML_DBL_QUOTES); 67 return temp.toString(); 68 69 } 70 71 public static void printNode(Writer xmlWriter, Node node,String encoding) throws Exception { 72 if (node == null) { 73 return; 74 } 75 76 switch (node.getNodeType()) { 77 case Node.DOCUMENT_NODE: { 78 printHead(xmlWriter,encoding); 79 printNode(xmlWriter, ((Document) node).getDocumentElement(),encoding); 80 break; 81 } 82 case Node.ELEMENT_NODE: { 83 StringBuffer tempElementString = new StringBuffer (node.getNodeName()); 85 NamedNodeMap attributeList = node.getAttributes(); 86 if ( attributeList != null ) { 87 for(int i= 0; i <attributeList.getLength();i++){ 88 Node attribute = attributeList.item(i); 89 tempElementString.append(wrapAttributeForPrint(attribute.getNodeName(),attribute.getNodeValue())); 90 } 91 } 92 printBeginElement(xmlWriter,tempElementString.toString()); 93 94 NodeList childNodes = node.getChildNodes(); 96 if (childNodes != null) { 97 int length = childNodes.getLength(); 98 for (int i = 0; i < length; i++) { 99 printNode(xmlWriter, childNodes.item(i),encoding); 100 } 101 } 102 103 printEndElement(xmlWriter,node.getNodeName()); 104 break; 105 } 106 107 case Node.TEXT_NODE: { 108 xmlWriter.write(encode(node.getNodeValue()).toString()); 109 break; 110 } 111 default: { 112 throw new UnsupportedOperationException (Messages.XMLPrintHandler_unsupportedNodeType); 113 114 } 115 } 116 117 } 118 119 public static StringBuffer encode(String value) { 120 StringBuffer buf = new StringBuffer (); 121 for (int i = 0; i < value.length(); i++) { 122 char c = value.charAt(i); 123 switch (c) { 124 case '&' : 125 buf.append("&"); break; 127 case '<' : 128 buf.append("<"); break; 130 case '>' : 131 buf.append(">"); break; 133 case '\'' : 134 buf.append("'"); break; 136 case '\"' : 137 buf.append("""); break; 139 default : 140 buf.append(c); 141 break; 142 } 143 } 144 return buf; 145 } 146 } 147 | Popular Tags |