1 23 24 package org.infoglue.cms.util; 25 26 import java.io.BufferedInputStream ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 32 import org.apache.xerces.parsers.DOMParser; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.SAXException ; 39 40 46 47 public class XMLHelper 48 { 49 50 60 61 public static StringBuffer serializeDom(Node node, StringBuffer writeString) 62 { 63 int type = node.getNodeType(); 64 try 65 { 66 switch (type) 67 { 68 case Node.DOCUMENT_NODE: 70 { 71 writeString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 72 writeString = serializeDom(((Document )node).getDocumentElement(), writeString); 73 break; 74 } 75 case Node.ELEMENT_NODE: 77 { 78 writeString.append("<"); 79 writeString.append(node.getNodeName()); 80 NamedNodeMap attrs = node.getAttributes(); 81 for (int i = 0; i < attrs.getLength(); i++) 82 { 83 Node attr = attrs.item(i); 84 String outString = " " + attr.getNodeName() + "=\"" + replaceSpecialCharacters(attr.getNodeValue()) + "\""; 85 writeString.append(outString); 86 } 87 writeString.append(">"); 88 NodeList children = node.getChildNodes(); 89 if (children != null) 90 { 91 int len = children.getLength(); 92 for (int i = 0; i < len; i++) 93 writeString = serializeDom(children.item(i), writeString); 94 } 95 break; 96 } 97 case Node.ENTITY_REFERENCE_NODE: 99 { 100 String outString = "&" + node.getNodeName() + ";"; 101 writeString.append(outString); 102 break; 103 } 104 case Node.CDATA_SECTION_NODE: 106 { 107 String outString = "<![CDATA[" + node.getNodeValue() + "]]>"; 108 writeString.append(outString); 109 break; 110 } 111 case Node.TEXT_NODE: 113 { 114 writeString.append(replaceSpecialCharacters(node.getNodeValue())); 115 break; 116 } 117 case Node.PROCESSING_INSTRUCTION_NODE: 119 { 120 String data = node.getNodeValue(); 121 String outString = "<?" + node.getNodeName() + " " + data + "?>"; 122 writeString.append(outString); 123 break; 124 } 125 } 126 if (type == Node.ELEMENT_NODE) 127 { 128 String outString = "</" + node.getNodeName() + ">"; 129 writeString.append(outString); 130 } 131 } 132 catch (Exception e) 133 { 134 135 } 136 return writeString; 137 } 138 139 140 143 144 public static String replaceSpecialCharacters(String value) 145 { 146 if(value == null) 147 return ""; 148 149 150 StringBuffer newValue = new StringBuffer (); 151 for (int i = 0; i < value.length(); i++) 152 { 153 char c = value.charAt(i); 154 if (c == '&') 155 { 156 newValue.append("&"); 157 } 158 else if (c == '<') 159 { 160 newValue.append("<"); 161 } 162 else if (c == '>') 163 { 164 newValue.append(">"); 165 } 166 else if (c == '\'') 167 { 168 newValue.append("'"); 169 } 170 else if (c == '\"') 171 { 172 newValue.append("""); 173 } 174 else 175 { 176 if(isLegalCharacter((int)c)) 177 newValue.append(c); 178 } 179 } 180 return newValue.toString(); 181 } 182 183 184 private static boolean isLegalCharacter(int ch) 185 { 186 return ch == 0x9 || ch == 0xA || ch == 0xD || (ch >= 0x20 && ch <= 0xFFFD) || (ch >= 0x1000 && ch <= 0x7FFFFFFF); 187 188 } 189 190 197 198 public static Document readDocumentFromFile(File xmlFile) throws IOException , SAXException 199 { 200 InputSource xmlSource = new InputSource (new FileReader (xmlFile)); 201 DOMParser parser = new DOMParser(); 202 parser.parse(xmlSource); 203 return parser.getDocument(); 204 } 205 206 207 214 215 public static Document readDocumentFromByteArray(byte[] xml) throws IOException , SAXException 216 { 217 InputSource xmlSource = new InputSource (new BufferedInputStream (new ByteArrayInputStream (xml))); 218 DOMParser parser = new DOMParser(); 219 parser.parse(xmlSource); 220 Document document = parser.getDocument(); 221 parser = null; 222 return document; 223 } 224 225 } | Popular Tags |