sdeboy 2003/06/11 22:19:26 Modified: src/java/org/apache/log4j/xml XMLDecoder.java Log: Updated xmldecoder to correctly handle partial events when decodeevents(string) method is called (used by xmlsocketnode). Revision Changes Path 1.12 +45 -26 jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/XMLDecoder.java Index: XMLDecoder.java =================================================================== RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/XMLDecoder.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- XMLDecoder.java 22 May 2003 08:43:42 -0000 1.11 +++ XMLDecoder.java 12 Jun 2003 05:19:25 -0000 1.12 @@ -49,25 +49,11 @@ package org.apache.log4j.xml; -import org.apache.log4j.Decoder; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.apache.log4j.UtilLoggingLevel; -import org.apache.log4j.spi.LocationInfo; -import org.apache.log4j.spi.LoggingEvent; - -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import org.xml.sax.InputSource; - import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.io.StringReader; - import java.util.Collections; import java.util.Hashtable; import java.util.Map; @@ -77,6 +63,17 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.apache.log4j.Decoder; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.UtilLoggingLevel; +import org.apache.log4j.spi.LocationInfo; +import org.apache.log4j.spi.LoggingEvent; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + /** * Decodes Logging Events in XML formated into elements that are used by @@ -90,14 +87,16 @@ * */ public class XMLDecoder implements Decoder { - private static final String beginPart = + private static final String BEGINPART = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE log4j:eventSet SYSTEM \"log4j.dtd\"><log4j:eventSet version=\"1.2\" xmlns:log4j=\"http://jakarta.apache.org/log4j/\">"; - private static final String endPart = "</log4j:eventSet>"; + private static final String ENDPART = "</log4j:eventSet>"; + private static final String RECORD_END = "</log4j:event>"; private StringBuffer buf = new StringBuffer(); private DocumentBuilderFactory dbf; private DocumentBuilder docBuilder; private Map additionalProperties = Collections.EMPTY_MAP; - + private String partialEvent; + public XMLDecoder() { dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); @@ -148,9 +147,9 @@ */ buf = new StringBuffer(1024); - buf.append(beginPart); + buf.append(BEGINPART); buf.append(data); - buf.append(endPart); + buf.append(ENDPART); InputSource inputSource = new InputSource(new StringReader(buf.toString())); @@ -208,19 +207,39 @@ if (doc == null) { return null; } - return decodeEvents(doc); + return decodeEvents(fileContents); } public Vector decodeEvents(String document) { - if (document != null) { - Document doc = parse(document); + if (document != null) { + document = document.trim(); + + if (document.equals("")) { + return null; + } else { + String newDoc=null; + String newPartialEvent=null; + //separate the string into the last portion ending with </log4j:event> (which will + //be processed) and the partial event which will be combined and processed in the next section + if (document.lastIndexOf(RECORD_END) + RECORD_END.length() < document.length()) { + newDoc = document.substring(0, document.lastIndexOf(RECORD_END) + RECORD_END.length()); + newPartialEvent = document.substring(document.lastIndexOf(RECORD_END) + RECORD_END.length()); + } else { + newDoc = document; + } + if (partialEvent != null) { + newDoc=partialEvent + newDoc; + } + partialEvent=newPartialEvent; + + Document doc = parse(newDoc); if (doc == null) { return null; } - - return decodeEvents(doc); - } - return null; + return decodeEvents(doc); + } + } + return null; } /**
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]