DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://nagoya.apache.org/bugzilla/show_bug.cgi?id=3940>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=3940 Change in newline characters in CDATA section from DOS to UNIX format Summary: Change in newline characters in CDATA section from DOS to UNIX format Product: Xerces2-J Version: 2.0.0 [alpha] Platform: PC OS/Version: Windows NT/2K Status: NEW Severity: Major Priority: Other Component: SAX AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] If a XML file contains a CDATA section and I extract that data and write it to a file I have seen the new line characters are converted to UNIX format i.e. \r\n is converted to \n only which causes problem specially in my case where the data in the CDATA section has to be processed further on the basis newline characters. I think it should be OS based rather than always converting them to UNIX format. Here is the sample program: import java.io.*; import org.xml.sax.InputSource; import org.xml.sax.AttributeList; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.XMLReader; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; /** * @author Kamlesh Nanda * @version 1.0 */ public class TestParser extends DefaultHandler { // // Constants // /** Default parser name. */ private static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; // ********************* Members *********************** private String lastElementRead = null; private String mybody = null; // ********************* ToXML Method *********************** private void append(String P ) { if(mybody == null) { mybody = new String (P); } else if(mybody.trim().equals("")) { mybody = new String (P); } else mybody += (new String (P)); } public String getBody() { return mybody; } public TestParser(String filecontents) { StringReader sr = new StringReader (filecontents); InputSource is = new InputSource ( sr ); print(DEFAULT_PARSER_NAME, is, true); } // // Public static methods // /** Prints the output from the SAX callbacks. */ public void print(String parserName, InputSource uri, boolean validate) { try { XMLReader parser = XMLReaderFactory.createXMLReader (parserName); parser.setContentHandler(this); parser.setErrorHandler(this); parser.parse(uri); } catch (org.xml.sax.SAXParseException spe) { spe.printStackTrace(System.err); } catch (org.xml.sax.SAXException se) { if (se.getException() != null) se.getException().printStackTrace(System.err); else se.printStackTrace(System.err); } catch (Exception e) { e.printStackTrace(System.err); } } // print(String,String) // // DocumentHandler methods // /** Start element. */ public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes attrs) { lastElementRead = localName; } // startElement(String,AttributeList) /** Characters. */ public void characters(char ch[], int start, int length) { String strValue = new String(ch, start, length); if(lastElementRead.equals("mybody")) { if(!strValue.trim().equals("")) append(strValue); } } // characters(char[],int,int); // // ErrorHandler methods // /** Warning. */ public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage ()); } /** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXException { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); throw ex; } /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); } // getLocationString(SAXParseException):String // // Main // /** Main program entry point. */ public static void main(String argv[]) { // is there anything to do? if (argv.length == 0) { System.out.println("Syntax Error: Usage -> java TestParser <xml filename with mybody tag>"); System.exit(1); } // vars String parserName = DEFAULT_PARSER_NAME; // print uri boolean blnValidation = false; try { File fl = new File(argv[0]); if(!fl.exists()) { System.out.println("###File \"" + fl + "\" Does not exists"); } char c[] = new char[1024]; FileReader reader = new FileReader(fl); int charsRead = 0; StringBuffer strbuf = new StringBuffer(); while((charsRead = reader.read(c, 0, c.length)) != -1) { strbuf.append(c, 0, charsRead); } TestParser wrapper = new TestParser(strbuf.toString()); System.out.println(wrapper.getBody()); if(wrapper.getBody() != null) { if(argv.length == 2) { File fl1 = new File(argv[1]); FileOutputStream fout = new FileOutputStream(fl1); PrintWriter pout = new PrintWriter(fout, true); pout.write(wrapper.getBody()); pout.close(); fout.close(); fout = null; } } } catch(Exception excp) { excp.printStackTrace(); } } // main(String[]) } // class TestParser //=======================End of Class=========================================\ This is the sample file: <?xml version="1.0"?> <sample> <mybody><![CDATA[ this is a DOS file. This is a Dos File. ]]> </mybody> </sample> and the syntax to the program is: java TestParser sample.xml Sampleout.xml where sample.xml can be the above file and SampleOut.xml will be an output file. The output file gets converted to a UNIX format ASCII file. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
