1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/test/com/sun/ebxml/registry/conformance/SAXContentHandler.java,v 1.5 2002/11/09 05:04:36 jasilva Exp $ 3 */ 4 package com.sun.ebxml.registry.conformance; 5 6 import java.io.*; 7 8 import org.xml.sax.*; 9 import org.xml.sax.helpers.*; 10 import java.util.*; 11 12 /*** 13 * SAX Content Handler used used to create new file in which values 14 * of some existing arguments will be changed on new values for those arguments 15 * 16 * @author Michal Zaremba 17 */ 18 public class SAXContentHandler extends DefaultHandler implements ContentHandler{ 19 private Locator locator; 20 private PrintWriter out; 21 private AttributesImpl impl = new AttributesImpl(); 22 private String filePath, presentArg, newArg; 23 24 public SAXContentHandler(String filePath, String presentArg, String newArg) { 25 this.filePath = filePath; 26 this.presentArg = presentArg; 27 this.newArg = newArg; 28 createFile(); 29 } 30 31 public void createFile() { 32 try { 33 String newFilePath = filePath.substring(0, filePath.lastIndexOf(".")) + "_new.xml"; 34 out = new PrintWriter(new BufferedWriter(new FileWriter(newFilePath))); 35 } catch(IOException ioe) { 36 ioe.printStackTrace(); 37 } 38 } 39 40 public void setDocumentLocator(Locator locator){ 41 this.locator = locator; 42 } 43 44 public void startElement(String uri, String sName, String qName, Attributes attrs) 45 throws SAXException { 46 47 String eName = sName; 48 if ("".equals(eName)) eName = qName; 49 emit("<" + eName); 50 if (attrs != null) { 51 for(int i=0; i <attrs.getLength(); i++) { 52 String aName = attrs.getLocalName(i); 53 if ("".equals(aName)) 54 aName = attrs.getQName(i); 55 if (attrs.getValue(i).equalsIgnoreCase(presentArg)){ 56 emit(" "); 57 emit(aName + "=\"" + newArg +"\""); 58 } 59 else { 60 emit(" "); 61 emit(aName+"=\"" +attrs.getValue(i)+ "\""); 62 } 63 } 64 emit(">"); 65 } 66 } 67 68 private void emit(String s) throws SAXException { 69 out.write(s); 70 out.flush(); 71 } 72 73 private void nl() throws SAXException { 74 String lineEnd = System.getProperty("line.separator"); 75 out.write(lineEnd); 76 } 77 78 public void startDocument() throws SAXException{ 79 emit("<?xml version='1.0' encoding='UTF-8'?>"); 80 nl(); 81 } 82 83 public void endDocument() throws SAXException { 84 nl(); 85 out.flush(); 86 out.close(); 87 } 88 89 public void endElement(String uri, String sName, String qName) throws SAXException { 90 emit("</" + sName+">"); 91 } 92 public void characters(char buf[], int offset, int len) throws SAXException { 93 String s = new String(buf, offset, len); 94 if (s.equalsIgnoreCase(presentArg)){ 95 emit(newArg); 96 } 97 else { 98 emit(s); 99 } 100 } 101 }

This page was automatically generated by Maven