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.Attributes ; 65 import org.xml.sax.SAXException ; 66 import org.xml.sax.SAXParseException ; 67 import org.xml.sax.XMLReader ; 68 import org.xml.sax.helpers.DefaultHandler ; 69 70 import sax.helpers.AttributesImpl; 71 import util.Arguments; 72 73 80 public class SAX2Writer 81 extends DefaultHandler { 82 83 87 88 private static final String 89 DEFAULT_PARSER_NAME = "org.enhydra.apache.xerces.parsers.SAXParser"; 90 91 92 private static boolean setValidation = false; private static boolean setNameSpaces = true; 94 private static boolean setSchemaSupport = true; 95 private static boolean setSchemaFullSupport = false; 96 97 98 102 103 protected PrintWriter out; 104 105 106 protected boolean canonical; 107 108 112 113 public SAX2Writer(boolean canonical) throws UnsupportedEncodingException { 114 this(null, canonical); 115 } 116 117 protected SAX2Writer(String encoding, boolean canonical) throws UnsupportedEncodingException { 118 119 if (encoding == null) { 120 encoding = "UTF8"; 121 } 122 123 out = new PrintWriter (new OutputStreamWriter (System.out, encoding)); 124 this.canonical = canonical; 125 126 } 128 132 133 public static void print(String parserName, String uri, boolean canonical) { 134 135 try { 136 DefaultHandler handler = new SAX2Writer(canonical); 137 138 XMLReader parser = (XMLReader )Class.forName(parserName).newInstance(); 139 140 parser.setFeature( "http://xml.org/sax/features/validation", 141 setValidation); 142 parser.setFeature( "http://xml.org/sax/features/namespaces", 143 setNameSpaces ); 144 parser.setFeature( "http://apache.org/xml/features/validation/schema", 145 setSchemaSupport ); 146 parser.setFeature( "http://apache.org/xml/features/validation/schema-full-checking", 147 setSchemaFullSupport ); 148 149 150 parser.setContentHandler(handler); 151 parser.setErrorHandler(handler); 152 parser.parse(uri); 153 } 154 catch (Exception e) { 155 e.printStackTrace(System.err); 156 } 157 158 } 160 164 165 public void processingInstruction(String target, String data) { 166 167 out.print("<?"); 168 out.print(target); 169 if (data != null && data.length() > 0) { 170 out.print(' '); 171 out.print(data); 172 } 173 out.print("?>"); 174 out.flush(); 175 176 } 178 179 public void startDocument() { 180 181 if (!canonical) { 182 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 183 out.flush(); 184 } 185 186 } 188 189 public void startElement(String uri, String local, String raw, 190 Attributes attrs) { 191 192 out.print('<'); 193 out.print(raw); 194 if (attrs != null) { 195 attrs = sortAttributes(attrs); 196 int len = attrs.getLength(); 197 for (int i = 0; i < len; i++) { 198 out.print(' '); 199 out.print(attrs.getQName(i)); 200 out.print("=\""); 201 out.print(normalize(attrs.getValue(i))); 202 out.print('"'); 203 } 204 } 205 out.print('>'); 206 out.flush(); 207 208 } 210 211 public void characters(char ch[], int start, int length) { 212 213 out.print(normalize(new String (ch, start, length))); 214 out.flush(); 215 216 } 218 219 public void ignorableWhitespace(char ch[], int start, int length) { 220 221 characters(ch, start, length); 222 out.flush(); 223 224 } 226 227 public void endElement(String uri, String local, String raw) { 228 229 out.print("</"); 230 out.print(raw); 231 out.print('>'); 232 out.flush(); 233 234 } 236 240 241 public void warning(SAXParseException ex) { 242 System.err.println("[Warning] "+ 243 getLocationString(ex)+": "+ 244 ex.getMessage()); 245 } 246 247 248 public void error(SAXParseException ex) { 249 System.err.println("[Error] "+ 250 getLocationString(ex)+": "+ 251 ex.getMessage()); 252 } 253 254 255 public void fatalError(SAXParseException ex) throws SAXException { 256 System.err.println("[Fatal Error] "+ 257 getLocationString(ex)+": "+ 258 ex.getMessage()); 259 throw ex; 260 } 261 262 263 private String getLocationString(SAXParseException ex) { 264 StringBuffer str = new StringBuffer (); 265 266 String systemId = ex.getSystemId(); 267 if (systemId != null) { 268 int index = systemId.lastIndexOf('/'); 269 if (index != -1) 270 systemId = systemId.substring(index + 1); 271 str.append(systemId); 272 } 273 str.append(':'); 274 str.append(ex.getLineNumber()); 275 str.append(':'); 276 str.append(ex.getColumnNumber()); 277 278 return str.toString(); 279 280 } 282 286 287 protected String normalize(String s) { 288 StringBuffer str = new StringBuffer (); 289 290 int len = (s != null) ? s.length() : 0; 291 for (int i = 0; i < len; i++) { 292 char ch = s.charAt(i); 293 switch (ch) { 294 case '<': { 295 str.append("<"); 296 break; 297 } 298 case '>': { 299 str.append(">"); 300 break; 301 } 302 case '&': { 303 str.append("&"); 304 break; 305 } 306 case '"': { 307 str.append("""); 308 break; 309 } 310 case '\r': 311 case '\n': { 312 if (canonical) { 313 str.append("&#"); 314 str.append(Integer.toString(ch)); 315 str.append(';'); 316 break; 317 } 318 } 320 default: { 321 str.append(ch); 322 } 323 } 324 } 325 326 return str.toString(); 327 328 } 330 331 protected Attributes sortAttributes(Attributes attrs) { 332 333 AttributesImpl attributes = new AttributesImpl(); 334 335 int len = (attrs != null) ? attrs.getLength() : 0; 336 for (int i = 0; i < len; i++) { 337 String name = attrs.getQName(i); 338 int count = attributes.getLength(); 339 int j = 0; 340 while (j < count) { 341 if (name.compareTo(attributes.getQName(j)) < 0) { 342 break; 343 } 344 j++; 345 } 346 attributes.insertAttributeAt(j, name, attrs.getType(i), 347 attrs.getValue(i)); 348 } 349 350 return attributes; 351 352 } 354 358 359 public static void main(String argv[]) { 360 Arguments argopt = new Arguments(); 362 363 argopt.setUsage( new String [] { 364 "usage: java sax.SAX2Writer (options) uri ...","", 365 "options:", 366 " -n | -N Turn on/off namespace [default=on]", 367 " -v | -V Turn on/off validation [default=on]", 368 " -s | -S Turn on/off Schema support [default=on]", 369 " -f | -F Turn on/off Schema full consraint checking [default=off]", 370 " -c Canonical XML output.", 371 " -h This help screen."} ); 372 373 374 375 376 if (argv.length == 0) { 378 argopt.printUsage(); 379 System.exit(1); 380 } 381 382 boolean canonical = false; 384 String parserName = DEFAULT_PARSER_NAME; 385 386 387 argopt.parseArgumentTokens(argv, new char[] { 'p'} ); 388 389 int c; 390 String arg = null; 391 while ( ( arg = argopt.getlistFiles() ) != null ) { 392 393 outer: 394 while ( (c = argopt.getArguments()) != -1 ){ 395 switch (c) { 396 case 'c': 397 canonical = true; 398 break; 399 case 'C': 400 canonical = false; 401 break; 402 case 'v': 403 setValidation = true; 404 break; 405 case 'V': 406 setValidation = false; 407 break; 408 case 'N': 409 setNameSpaces = false; 410 break; 411 case 'n': 412 setNameSpaces = true; 413 break; 414 case 'p': 415 parserName = argopt.getStringParameter(); 416 break; 417 case 's': 418 setSchemaSupport = true; 419 break; 420 case 'S': 421 setSchemaSupport = false; 422 break; 423 case 'f': 424 setSchemaFullSupport = true; 425 break; 426 case 'F': 427 setSchemaFullSupport = false; 428 break; 429 case '?': 430 case 'h': 431 case '-': 432 argopt.printUsage(); 433 System.exit(1); 434 break; 435 case -1: 436 break outer; 437 default: 438 break; 439 } 440 } 441 System.err.println(arg+':'); 443 print(parserName, arg, canonical); 444 } 445 } 447 } | Popular Tags |