1 16 17 package org.apache.log4j.xml; 18 19 import org.apache.log4j.Category; 20 import org.apache.log4j.Layout; 21 import org.apache.log4j.PropertyConfigurator; 22 import org.apache.log4j.spi.LoggingEvent; 23 import org.apache.log4j.helpers.OptionConverter; 24 import org.apache.log4j.helpers.DateLayout; 25 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.Locator ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.XMLReader ; 30 import org.xml.sax.ext.LexicalHandler ; 31 import org.xml.sax.helpers.XMLReaderFactory ; 32 import org.xml.sax.SAXException ; 33 import org.apache.xerces.parsers.SAXParser; 34 35 import org.apache.trax.Processor; 36 import org.apache.trax.TemplatesBuilder; 37 import org.apache.trax.Templates; 38 import org.apache.trax.Transformer; 39 import org.apache.trax.Result; 40 import org.apache.trax.ProcessorException; 41 import org.apache.trax.ProcessorFactoryException; 42 import org.apache.trax.TransformException; 43 44 45 import org.apache.serialize.SerializerFactory; 46 import org.apache.serialize.Serializer; 47 import org.apache.serialize.OutputFormat; 48 import org.xml.sax.helpers.AttributesImpl ; 49 50 51 import java.io.FileOutputStream ; 52 import java.io.IOException ; 53 54 55 public class Transform { 56 57 public static void main(String [] args) throws Exception { 58 PropertyConfigurator.disableAll(); 59 PropertyConfigurator.configure("x.lcf"); 60 61 Processor processor = Processor.newInstance("xslt"); 63 64 66 XMLReader reader = XMLReaderFactory.createXMLReader(); 68 69 TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder(); 71 reader.setContentHandler(templatesBuilder); 72 73 if(templatesBuilder instanceof LexicalHandler ) { 78 reader.setProperty("http://xml.org/sax/properties/lexical-handler", 79 templatesBuilder); 80 } 81 82 reader.parse(args[0]); 84 85 Templates templates = templatesBuilder.getTemplates(); 87 88 Transformer transformer = templates.newTransformer(); 90 91 93 FileOutputStream fos = new FileOutputStream (args[2]); 95 Result result = new Result(fos); 96 Serializer serializer = SerializerFactory.getSerializer("xml"); 97 serializer.setOutputStream(fos); 98 99 transformer.setContentHandler(serializer.asContentHandler()); 100 101 org.xml.sax.ContentHandler chandler = transformer.getInputContentHandler(); 103 DC dc = new DC(chandler); 104 reader.setContentHandler(dc); 105 if(chandler instanceof LexicalHandler ) { 106 reader.setProperty("http://xml.org/sax/properties/lexical-handler", 107 chandler); 108 } else { 109 reader.setProperty("http://xml.org/sax/properties/lexical-handler", 110 null); 111 } 112 113 reader.parse(args[1]); 117 } 118 } 119 120 class DC implements ContentHandler { 121 122 static Category cat = Category.getInstance("DC"); 123 124 ContentHandler chandler; 125 126 DC(ContentHandler chandler) { 127 this.chandler = chandler; 128 } 129 130 131 public 132 void characters(char[] ch, int start, int length) 133 throws org.xml.sax.SAXException { 134 cat.debug("characters: ["+new String (ch, start, length)+ "] called"); 135 chandler.characters(ch, start, length); 136 137 } 138 139 public 140 void endDocument() throws org.xml.sax.SAXException { 141 cat.debug("endDocument called."); 142 chandler.endDocument(); 143 144 } 145 146 public 147 void endElement(String namespaceURI, String localName, String qName) 148 throws org.xml.sax.SAXException { 149 cat.debug("endElement("+namespaceURI+", "+localName+", "+qName+") called"); 150 chandler.endElement(namespaceURI, localName, qName); 151 } 152 153 public 154 void endPrefixMapping(String prefix) throws org.xml.sax.SAXException { 155 cat.debug("endPrefixMapping("+prefix+") called"); 156 chandler.endPrefixMapping(prefix); 157 } 158 159 public 160 void ignorableWhitespace(char[] ch, int start, int length) 161 throws org.xml.sax.SAXException { 162 cat.debug("ignorableWhitespace called"); 163 chandler.ignorableWhitespace(ch, start, length); 164 } 165 166 public 167 void processingInstruction(java.lang.String target, java.lang.String data) 168 throws org.xml.sax.SAXException { 169 cat.debug("processingInstruction called"); 170 chandler.processingInstruction(target, data); 171 } 172 173 public 174 void setDocumentLocator(Locator locator) { 175 cat.debug("setDocumentLocator called"); 176 chandler.setDocumentLocator(locator); 177 } 178 179 public 180 void skippedEntity(String name) throws org.xml.sax.SAXException { 181 cat.debug("skippedEntity("+name+") called"); 182 chandler.skippedEntity(name); 183 } 184 185 public 186 void startDocument() throws org.xml.sax.SAXException { 187 cat.debug("startDocument called"); 188 chandler.startDocument(); 189 } 190 191 public 192 void startElement(String namespaceURI, String localName, String qName, 193 Attributes atts) throws org.xml.sax.SAXException { 194 cat.debug("startElement("+namespaceURI+", "+localName+", "+qName+")called"); 195 196 if("log4j:event".equals(qName)) { 197 cat.debug("-------------"); 198 if(atts instanceof org.xml.sax.helpers.AttributesImpl ) { 199 AttributesImpl ai = (AttributesImpl ) atts; 200 int i = atts.getIndex("timestamp"); 201 ai.setValue(i, "hello"); 202 } 203 String ts = atts.getValue("timestamp"); 204 cat.debug("New timestamp is " + ts); 205 } 206 chandler.startElement(namespaceURI, localName, qName, atts); 207 } 208 209 public 210 void startPrefixMapping(String prefix, String uri) 211 throws org.xml.sax.SAXException { 212 cat.debug("startPrefixMapping("+prefix+", "+uri+") called"); 213 chandler.startPrefixMapping(prefix, uri); 214 } 215 216 217 } 218 219 | Popular Tags |