1 package org.sapia.util.xml.idefix; 2 3 4 import org.sapia.util.xml.Attribute; 7 8 import java.io.IOException ; 11 import java.io.OutputStream ; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 18 31 public class XmlScribe { 32 33 private static final List EMPTY_LIST = new ArrayList (0); 34 35 36 private String _theStreamEncoding; 37 38 42 public XmlScribe() { 43 _theStreamEncoding = "UTF-8"; 44 } 45 46 51 public XmlScribe(String aStreamEncoding) { 52 _theStreamEncoding = aStreamEncoding; 53 } 54 55 60 public void setStreamEncoding(String anEncoding) { 61 _theStreamEncoding = anEncoding; 62 } 63 64 69 public String getStreamEncoding() { 70 return _theStreamEncoding; 71 } 72 73 80 public void composeXmlDeclaration(String aCharacterEncoding, 81 StringBuffer aBuffer) { 82 if ((aCharacterEncoding != null) && (aCharacterEncoding.length() > 0)) { 83 aBuffer.append("<?xml version=\"1.0\" encoding=\"") 84 .append(aCharacterEncoding).append("\" ?>"); 85 } else { 86 aBuffer.append("<?xml version=\"1.0\" ?>"); 87 } 88 } 89 90 98 public void composeXmlDeclaration(String aCharacterEncoding, 99 OutputStream aStream) throws IOException { 100 StringBuffer aBuffer = new StringBuffer (); 101 composeXmlDeclaration(aCharacterEncoding, aBuffer); 102 aStream.write(aBuffer.toString().getBytes(_theStreamEncoding)); 103 } 104 105 114 public void composeStartingElement(String aNamespacePrefix, 115 String anElementName, List someAttributes, boolean isEmptyElement, 116 StringBuffer aBuffer) { 117 aBuffer.append("<"); 119 composeQualifiedName(aNamespacePrefix, anElementName, aBuffer); 120 121 for (Iterator it = someAttributes.iterator(); it.hasNext();) { 123 Attribute anAttribute = (Attribute) it.next(); 124 125 if ((anAttribute.getValue() != null) && 127 (anAttribute.getValue().length() > 0)) { 128 aBuffer.append(" "); 129 composeQualifiedName(anAttribute.getNamespacePrefix(), 130 anAttribute.getName(), aBuffer); 131 aBuffer.append("=\""); 132 xmlEncode(anAttribute.getValue(), aBuffer); 133 aBuffer.append("\""); 134 } 135 } 136 137 if (isEmptyElement) { 139 aBuffer.append(" />"); 140 } else { 141 aBuffer.append(">"); 142 } 143 } 144 145 155 public void composeStartingElement(String aNamespacePrefix, 156 String anElementName, List someAttributes, boolean isEmptyElement, 157 OutputStream aStream) throws IOException { 158 StringBuffer aBuffer = new StringBuffer (); 159 composeStartingElement(aNamespacePrefix, anElementName, someAttributes, 160 isEmptyElement, aBuffer); 161 aStream.write(aBuffer.toString().getBytes(_theStreamEncoding)); 162 } 163 164 173 public void composeStartingElement(String aNamespacePrefix, 174 String anElementName, boolean isEmptyElement, StringBuffer aBuffer) { 175 composeStartingElement(aNamespacePrefix, anElementName, EMPTY_LIST, 176 isEmptyElement, aBuffer); 177 } 178 179 189 public void composeStartingElement(String aNamespacePrefix, 190 String anElementName, boolean isEmptyElement, OutputStream aStream) 191 throws IOException { 192 StringBuffer aBuffer = new StringBuffer (); 193 composeStartingElement(aNamespacePrefix, anElementName, EMPTY_LIST, 194 isEmptyElement, aBuffer); 195 aStream.write(aBuffer.toString().getBytes(_theStreamEncoding)); 196 } 197 198 205 public void composeEndingElement(String aNamespacePrefix, 206 String anElementName, StringBuffer aBuffer) { 207 aBuffer.append("</"); 208 composeQualifiedName(aNamespacePrefix, anElementName, aBuffer); 209 aBuffer.append(">"); 210 } 211 212 220 public void composeEndingElement(String aNamespacePrefix, 221 String anElementName, OutputStream aStream) throws IOException { 222 StringBuffer aBuffer = new StringBuffer (); 223 composeEndingElement(aNamespacePrefix, anElementName, aBuffer); 224 aStream.write(aBuffer.toString().getBytes(_theStreamEncoding)); 225 } 226 227 233 public void composeCData(String aContent, StringBuffer aBuffer) { 234 aBuffer.append("<![CDATA[").append(aContent).append("]]>"); 235 } 236 237 244 public void composeCData(String aContent, OutputStream aStream) 245 throws IOException { 246 StringBuffer aBuffer = new StringBuffer (); 247 composeCData(aContent, aBuffer); 248 aStream.write(aBuffer.toString().getBytes(_theStreamEncoding)); 249 } 250 251 257 public String xmlEncode(String aString) { 258 StringBuffer anXmlString = new StringBuffer (); 259 xmlEncode(aString, anXmlString); 260 261 return anXmlString.toString(); 262 } 263 264 271 public void xmlEncode(String aString, OutputStream aStream) 272 throws IOException { 273 StringBuffer aBuffer = new StringBuffer (); 274 xmlEncode(aString, aBuffer); 275 aStream.write(aBuffer.toString().getBytes(_theStreamEncoding)); 276 } 277 278 284 public void xmlEncode(String aString, StringBuffer anXmlString) { 285 for (int i = 0; i < aString.length(); ++i) { 286 int aChar = (int) aString.charAt(i); 287 288 if (((aChar & 0xfc00) == 0xd800) && ((i + 1) < aString.length())) { 289 int lowch = (int) aString.charAt(i + 1); 290 291 if ((lowch & 0xfc00) == 0xdc00) { 292 aChar = (0x10000 + ((aChar - 0xd800) << 10) + lowch) - 0xdc00; 293 i++; 294 } 295 } 296 297 String anEntityRef = getEntityReference(aChar); 298 299 if (anEntityRef != null) { 300 anXmlString.append('&').append(anEntityRef).append(';'); 301 } else if (((aChar >= ' ') && (aChar <= 0xF7)) || (aChar == '\n') || 302 (aChar == '\r') || (aChar == '\t')) { 303 if (aChar < 0x10000) { 307 anXmlString.append((char) aChar); 308 } else { 309 anXmlString.append((char) (((aChar - 0x10000) >> 10) + 0xd800)); 310 anXmlString.append((char) (((aChar - 0x10000) & 0x3ff) + 0xdc00)); 311 } 312 } else { 313 anXmlString.append("&#x").append(Integer.toHexString(aChar)).append(';'); 314 } 315 } 316 } 317 318 325 protected void composeQualifiedName(String aNamespacePrefix, String aName, 326 StringBuffer aBuffer) { 327 if ((aNamespacePrefix != null) && (aNamespacePrefix.length() > 0)) { 328 aBuffer.append(aNamespacePrefix).append(":"); 329 } 330 331 aBuffer.append(aName); 332 } 333 334 342 protected String getEntityReference(int aChar) { 343 switch (aChar) { 344 case '<': 345 return "lt"; 346 347 case '>': 348 return "gt"; 349 350 case '"': 351 return "quot"; 352 353 case '\'': 354 return "apos"; 355 356 case '&': 357 return "amp"; 358 } 359 360 return null; 361 } 362 } 363 | Popular Tags |