Hai... Thaks for the reply....
I shall expalin to you clearly. I need to convert a Non-xml fiel to xml file. On the process of conversion as my saxEvents are generated I use an xslt to to the transformation and generate the xml output. I ahve attached a saple piece of code..please go thru it. I have an abstract (AbstractXMLReader) class which has an abstract parse(inputsource in)method. I extend this abstract class in my CSVParser which is used the parse the comma seperated non-xml file.As this class genetres the SAXEvents i use my transformer(SimpleCSVProcessor) class which has an xsl to do the transformations. The transformer transforms and genetares an XML file. When I execute the program I dont get the output. I have provided the code wirtten as an attachement. can you please let me know if there are any mistakes. My aim is to geneterate and XML {{{{ while reading the non-xml file and as SAX Events are generated I want the transformation to happen and produce an xml output. }}}} Plz revert back immediatel as possible as it is very urgent. rgds, satish
package parser; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.xml.sax.ContentHandler; import org.xml.sax.DTDHandler; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; public abstract class AbstractXMLReader implements org.xml.sax.XMLReader { private Map featureMap = new HashMap( ); private Map propertyMap = new HashMap( ); private EntityResolver entityResolver; private DTDHandler dtdHandler; protected ContentHandler ch; private ErrorHandler errorHandler; private String configuration; /** * The only abstract method in this class. Derived classes * can parse any source of data and emit SAX2 events to the * ContentHandler. */ public abstract void parse(InputSource input) throws IOException, SAXException; public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { Boolean featureValue = (Boolean) this.featureMap.get(name); return (featureValue == null) ? false : featureValue.booleanValue( ); } public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { this.featureMap.put(name, new Boolean(value)); } public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { return this.propertyMap.get(name); } public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { this.propertyMap.put(name, value); } public void setEntityResolver(EntityResolver entityResolver) { this.entityResolver = entityResolver; } public EntityResolver getEntityResolver( ) { return this.entityResolver; } public void setDTDHandler(DTDHandler dtdHandler) { this.dtdHandler = dtdHandler; } public DTDHandler getDTDHandler( ) { return this.dtdHandler; } public void setContentHandler(ContentHandler contentHandler) { this.ch = contentHandler; } public ContentHandler getContentHandler( ) { return this.ch; } public void setErrorHandler(ErrorHandler errorHandler) { this.errorHandler = errorHandler; } public ErrorHandler getErrorHandler( ) { return this.errorHandler; } public void setConfig(String config) { this.configuration = config; } protected String getConfig() { return this.configuration; } public void parse(String systemId) throws IOException, SAXException { parse(new InputSource(systemId)); } }
package parser; import java.io.BufferedReader; import java.io.Reader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URL; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; public class AbstractParser extends AbstractXMLReader{ private static final Attributes EMPTY_ATTR=new AttributesImpl(); private static final String encoding = "UTF-8"; public void parse(InputSource in) throws SAXException, IOException { ContentHandler ch=getContentHandler(); if (ch==null){ System.out.println("Content Handler Not Registered"); return; } BufferedReader br=null; if(in.getCharacterStream()!=null){ br=new BufferedReader(in.getCharacterStream()); }else if(in.getByteStream() != null){ br=new BufferedReader(new InputStreamReader(in.getByteStream())); }else if(in.getSystemId() !=null){ URL url=new URL(in.getSystemId()); br=new BufferedReader(new InputStreamReader(url.openStream())); }else{ throw new SAXException("Invalid InputSource "); } ch.startDocument( ); System.out.println("start"); ch.startElement("","","csv",EMPTY_ATTR); String curLine = null; while ((curLine = br.readLine( )) != null) { curLine = curLine.trim( ); if (curLine.length( ) > 0) { ch.startElement("","","line",EMPTY_ATTR); parseLine(curLine, ch); ch.endElement("","","line"); } } ch.endElement("","","csvFile"); ch.endDocument( ); } private void parseLine(String curLine, ContentHandler ch) throws IOException, SAXException { String firstToken = null; String remainderOfLine = null; int commaIndex = locateFirstDelimiter(curLine); if (commaIndex > -1) { firstToken = curLine.substring(0, commaIndex).trim( ); remainderOfLine = curLine.substring(commaIndex+1).trim( ); } else { firstToken = curLine; } firstToken = cleanupQuotes(firstToken); ch.startElement("","","value",EMPTY_ATTR); ch.characters(firstToken.toCharArray(), 0, firstToken.length( )); ch.endElement("","","value"); if (remainderOfLine != null) { parseLine(remainderOfLine, ch); } } private int locateFirstDelimiter(String curLine) { if (curLine.startsWith("\"")) { boolean inQuote = true; int numChars = curLine.length( ); for (int i=1; i<numChars; i++) { char curChar = curLine.charAt(i); if (curChar == '"') { inQuote = !inQuote; } else if (curChar == ',' && !inQuote) { return i; } } return -1; } else { return curLine.indexOf(','); } } private String cleanupQuotes(String token) { StringBuffer buf = new StringBuffer( ); int length = token.length( ); int curIndex = 0; if (token.startsWith("\"") && token.endsWith("\"")) { curIndex = 1; length--; } boolean oneQuoteFound = false; boolean twoQuotesFound = false; while (curIndex < length) { char curChar = token.charAt(curIndex); if (curChar == '"') { twoQuotesFound = (oneQuoteFound) ? true : false; oneQuoteFound = true; } else { oneQuoteFound = false; twoQuotesFound = false; } if (twoQuotesFound) { twoQuotesFound = false; oneQuoteFound = false; curIndex++; continue; } buf.append(curChar); curIndex++; } return buf.toString( ); } }
package parser; import java.io.File; import java.io.FileReader; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.xml.sax.InputSource; public class SimpleCSVProcessor { public static void main(String[] args) throws Exception { if (args.length == 0) { System.err.println("Usage: java " + SimpleCSVProcessor.class.getName( ) + " <csvFile> [xsltFile]"); System.err.println(" - csvFile is required"); System.err.println(" - xsltFile is optional"); System.exit(1); } String csvFileName = args[0]; String xsltFileName = (args.length > 1) ? args[1] : null; TransformerFactory transFact = TransformerFactory.newInstance( ); if (transFact.getFeature(SAXTransformerFactory.FEATURE)) { SAXTransformerFactory saxTransFact = (SAXTransformerFactory) transFact; TransformerHandler transHand = null; if (xsltFileName == null) { transHand = saxTransFact.newTransformerHandler( ); } else { transHand = saxTransFact.newTransformerHandler( new StreamSource(new File(xsltFileName))); } transHand.setResult(new StreamResult(System.out)); AbstractParser csvReader = new AbstractParser( ); InputSource csvInputSrc = new InputSource(new FileReader(csvFileName)); csvReader.setContentHandler(transHand); csvReader.parse(csvInputSrc); } else { System.err.println("SAXTransformerFactory is not supported."); System.exit(1); } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]