1 57 58 package sax; 59 60 import java.io.OutputStreamWriter ; 61 import java.io.PrintWriter ; 62 import java.io.UnsupportedEncodingException ; 63 64 import org.xml.sax.AttributeList ; 65 import org.xml.sax.HandlerBase ; 66 import org.xml.sax.Parser ; 67 import org.xml.sax.SAXException ; 68 import org.xml.sax.SAXParseException ; 69 import org.xml.sax.XMLReader ; 70 import org.xml.sax.helpers.ParserFactory ; 71 72 import sax.helpers.AttributeListImpl; 73 import util.Arguments; 74 75 76 77 84 public class SAXWriter 85 extends HandlerBase { 86 87 91 92 private static final String 93 DEFAULT_PARSER_NAME = "org.enhydra.apache.xerces.parsers.SAXParser"; 94 95 private static boolean setValidation = false; private static boolean setNameSpaces = true; 97 private static boolean setSchemaSupport = true; 98 private static boolean setSchemaFullSupport = false; 99 100 101 102 103 107 108 protected PrintWriter out; 109 110 111 protected boolean canonical; 112 113 117 118 public SAXWriter(boolean canonical) throws UnsupportedEncodingException { 119 this(null, canonical); 120 } 121 122 protected SAXWriter(String encoding, boolean canonical) throws UnsupportedEncodingException { 123 124 if (encoding == null) { 125 encoding = "UTF8"; 126 } 127 128 out = new PrintWriter (new OutputStreamWriter (System.out, encoding)); 129 this.canonical = canonical; 130 131 } 133 137 138 public static void print(String parserName, String uri, boolean canonical) { 139 140 try { 141 HandlerBase handler = new SAXWriter(canonical); 142 143 Parser parser = ParserFactory.makeParser(parserName); 144 145 146 if ( parser instanceof XMLReader ){ 147 ((XMLReader )parser).setFeature( "http://xml.org/sax/features/validation", 148 setValidation); 149 ((XMLReader )parser).setFeature( "http://xml.org/sax/features/namespaces", 150 setNameSpaces ); 151 ((XMLReader )parser).setFeature( "http://apache.org/xml/features/validation/schema", 152 setSchemaSupport ); 153 ((XMLReader )parser).setFeature( "http://apache.org/xml/features/validation/schema-full-checking", 154 setSchemaFullSupport ); 155 156 } 157 158 159 160 161 parser.setDocumentHandler(handler); 162 parser.setErrorHandler(handler); 163 parser.parse(uri); 164 } catch (Exception e) { 165 e.printStackTrace(System.err); 166 } 167 168 } 170 174 175 public void processingInstruction(String target, String data) { 176 177 out.print("<?"); 178 out.print(target); 179 if (data != null && data.length() > 0) { 180 out.print(' '); 181 out.print(data); 182 } 183 out.print("?>"); 184 185 } 187 188 public void startDocument() { 189 190 if (!canonical) { 191 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 192 } 193 194 } 196 197 public void startElement(String name, AttributeList attrs) { 198 199 out.print('<'); 200 out.print(name); 201 if (attrs != null) { 202 attrs = sortAttributes(attrs); 203 int len = attrs.getLength(); 204 for (int i = 0; i < len; i++) { 205 out.print(' '); 206 out.print(attrs.getName(i)); 207 out.print("=\""); 208 out.print(normalize(attrs.getValue(i))); 209 out.print('"'); 210 } 211 } 212 out.print('>'); 213 214 } 216 217 public void characters(char ch[], int start, int length) { 218 219 out.print(normalize(new String (ch, start, length))); 220 221 } 223 224 public void ignorableWhitespace(char ch[], int start, int length) { 225 226 characters(ch, start, length); 227 228 } 230 231 public void endElement(String name) { 232 233 out.print("</"); 234 out.print(name); 235 out.print('>'); 236 237 } 239 240 public void endDocument() { 241 242 out.flush(); 243 244 } 246 250 251 public void warning(SAXParseException ex) { 252 System.err.println("[Warning] "+ 253 getLocationString(ex)+": "+ 254 ex.getMessage()); 255 } 256 257 258 public void error(SAXParseException ex) { 259 System.err.println("[Error] "+ 260 getLocationString(ex)+": "+ 261 ex.getMessage()); 262 } 263 264 265 public void fatalError(SAXParseException ex) throws SAXException { 266 System.err.println("[Fatal Error] "+ 267 getLocationString(ex)+": "+ 268 ex.getMessage()); 269 throw ex; 270 } 271 272 273 private String getLocationString(SAXParseException ex) { 274 StringBuffer str = new StringBuffer (); 275 276 String systemId = ex.getSystemId(); 277 if (systemId != null) { 278 int index = systemId.lastIndexOf('/'); 279 if (index != -1) 280 systemId = systemId.substring(index + 1); 281 str.append(systemId); 282 } 283 str.append(':'); 284 str.append(ex.getLineNumber()); 285 str.append(':'); 286 str.append(ex.getColumnNumber()); 287 288 return str.toString(); 289 290 } 292 296 297 protected String normalize(String s) { 298 StringBuffer str = new StringBuffer (); 299 300 int len = (s != null) ? s.length() : 0; 301 for (int i = 0; i < len; i++) { 302 char ch = s.charAt(i); 303 switch (ch) { 304 case '<': { 305 str.append("<"); 306 break; 307 } 308 case '>': { 309 str.append(">"); 310 break; 311 } 312 case '&': { 313 str.append("&"); 314 break; 315 } 316 case '"': { 317 str.append("""); 318 break; 319 } 320 case '\'': { 321 str.append("'"); 322 break; 323 } 324 case '\r': 325 case '\n': { 326 if (canonical) { 327 str.append("&#"); 328 str.append(Integer.toString(ch)); 329 str.append(';'); 330 break; 331 } 332 } 334 default: { 335 str.append(ch); 336 } 337 } 338 } 339 340 return str.toString(); 341 342 } 344 345 protected AttributeList sortAttributes(AttributeList attrs) { 346 347 AttributeListImpl attributes = new AttributeListImpl(); 348 int len = (attrs != null) ? attrs.getLength() : 0; 349 for (int i = 0; i < len; i++) { 350 String name = attrs.getName(i); 351 int count = attributes.getLength(); 352 int j = 0; 353 while (j < count) { 354 if (name.compareTo(attributes.getName(j)) < 0) { 355 break; 356 } 357 j++; 358 } 359 attributes.insertAttributeAt(j, name, attrs.getType(i), 360 attrs.getValue(i)); 361 } 362 363 return attributes; 364 365 } 367 371 372 public static void main(String argv[]) { 373 Arguments argopt = new Arguments(); 375 376 argopt.setUsage( new String [] { 377 "usage: java sax.SAXWriter (options) uri ...","", 378 "options:", 379 " -n | -N Turn on/off namespace [default=on]", 380 " -v | -V Turn on/off validation [default=off]", 381 " -s | -S Turn on/off Schema support [default=on]", 382 " -f | -F Turn on/off Schema full consraint checking [default=off]", 383 " -c Canonical XML output.", 384 " -h This help screen."} ); 385 386 387 388 389 if (argv.length == 0) { 391 argopt.printUsage(); 392 System.exit(1); 393 } 394 395 boolean canonical = false; 397 String parserName = DEFAULT_PARSER_NAME; 398 399 400 argopt.parseArgumentTokens(argv, new char[] { 'p'} ); 401 402 int c; 403 String arg = null; 404 while ( ( arg = argopt.getlistFiles() ) != null ) { 405 406 outer: 407 while ( (c = argopt.getArguments()) != -1 ){ 408 switch (c) { 409 case 'c': 410 canonical = true; 411 break; 412 case 'C': 413 canonical = false; 414 break; 415 case 'v': 416 setValidation = true; 417 break; 418 case 'V': 419 setValidation = false; 420 break; 421 case 'N': 422 setNameSpaces = false; 423 break; 424 case 'n': 425 setNameSpaces = true; 426 break; 427 case 'p': 428 parserName = argopt.getStringParameter(); 429 break; 430 case 's': 431 setSchemaSupport = true; 432 break; 433 case 'S': 434 setSchemaSupport = false; 435 break; 436 case 'f': 437 setSchemaFullSupport = true; 438 break; 439 case 'F': 440 setSchemaFullSupport = false; 441 break; 442 case '?': 443 case 'h': 444 case '-': 445 argopt.printUsage(); 446 System.exit(1); 447 break; 448 case -1: 449 break outer; 450 default: 451 break; 452 } 453 } 454 System.err.println(arg+':'); 456 print(parserName, arg, canonical); 457 } 458 } 460 } | Popular Tags |