PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/contribs/CekiGulcu/Transform.java

#
Java | 219 lines | 140 code | 46 blank | 33 comment | 5 complexity | 33c5db97f0d38b0fcb54ee2add5329a4 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.log4j.xml;
  18. import org.apache.log4j.Category;
  19. import org.apache.log4j.Layout;
  20. import org.apache.log4j.PropertyConfigurator;
  21. import org.apache.log4j.spi.LoggingEvent;
  22. import org.apache.log4j.helpers.OptionConverter;
  23. import org.apache.log4j.helpers.DateLayout;
  24. import org.xml.sax.ContentHandler;
  25. import org.xml.sax.Locator;
  26. import org.xml.sax.Attributes;
  27. import org.xml.sax.XMLReader;
  28. import org.xml.sax.ext.LexicalHandler;
  29. import org.xml.sax.helpers.XMLReaderFactory;
  30. import org.xml.sax.SAXException;
  31. import org.apache.xerces.parsers.SAXParser;
  32. import org.apache.trax.Processor;
  33. import org.apache.trax.TemplatesBuilder;
  34. import org.apache.trax.Templates;
  35. import org.apache.trax.Transformer;
  36. import org.apache.trax.Result;
  37. import org.apache.trax.ProcessorException;
  38. import org.apache.trax.ProcessorFactoryException;
  39. import org.apache.trax.TransformException;
  40. import org.apache.serialize.SerializerFactory;
  41. import org.apache.serialize.Serializer;
  42. import org.apache.serialize.OutputFormat;
  43. import org.xml.sax.helpers.AttributesImpl;
  44. import java.io.FileOutputStream;
  45. import java.io.IOException;
  46. public class Transform {
  47. public static void main(String[] args) throws Exception {
  48. PropertyConfigurator.disableAll();
  49. PropertyConfigurator.configure("x.lcf");
  50. // I. Instantiate a stylesheet processor.
  51. Processor processor = Processor.newInstance("xslt");
  52. // II. Process the stylesheet. producing a Templates object.
  53. // Get the XMLReader.
  54. XMLReader reader = XMLReaderFactory.createXMLReader();
  55. // Set the ContentHandler.
  56. TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder();
  57. reader.setContentHandler(templatesBuilder);
  58. // Set the ContentHandler to also function as a LexicalHandler, which
  59. // includes "lexical" (e.g., comments and CDATA) events. The Xalan
  60. // TemplatesBuilder -- org.apache.xalan.processor.StylesheetHandler -- is
  61. // also a LexicalHandler).
  62. if(templatesBuilder instanceof LexicalHandler) {
  63. reader.setProperty("http://xml.org/sax/properties/lexical-handler",
  64. templatesBuilder);
  65. }
  66. // Parse the stylesheet.
  67. reader.parse(args[0]);
  68. //Get the Templates object from the ContentHandler.
  69. Templates templates = templatesBuilder.getTemplates();
  70. // III. Use the Templates object to instantiate a Transformer.
  71. Transformer transformer = templates.newTransformer();
  72. // IV. Perform the transformation.
  73. // Set up the ContentHandler for the output.
  74. FileOutputStream fos = new FileOutputStream(args[2]);
  75. Result result = new Result(fos);
  76. Serializer serializer = SerializerFactory.getSerializer("xml");
  77. serializer.setOutputStream(fos);
  78. transformer.setContentHandler(serializer.asContentHandler());
  79. // Set up the ContentHandler for the input.
  80. org.xml.sax.ContentHandler chandler = transformer.getInputContentHandler();
  81. DC dc = new DC(chandler);
  82. reader.setContentHandler(dc);
  83. if(chandler instanceof LexicalHandler) {
  84. reader.setProperty("http://xml.org/sax/properties/lexical-handler",
  85. chandler);
  86. } else {
  87. reader.setProperty("http://xml.org/sax/properties/lexical-handler",
  88. null);
  89. }
  90. // Parse the XML input document. The input ContentHandler and
  91. // output ContentHandler work in separate threads to optimize
  92. // performance.
  93. reader.parse(args[1]);
  94. }
  95. }
  96. class DC implements ContentHandler {
  97. static Category cat = Category.getInstance("DC");
  98. ContentHandler chandler;
  99. DC(ContentHandler chandler) {
  100. this.chandler = chandler;
  101. }
  102. public
  103. void characters(char[] ch, int start, int length)
  104. throws org.xml.sax.SAXException {
  105. cat.debug("characters: ["+new String(ch, start, length)+ "] called");
  106. chandler.characters(ch, start, length);
  107. }
  108. public
  109. void endDocument() throws org.xml.sax.SAXException {
  110. cat.debug("endDocument called.");
  111. chandler.endDocument();
  112. }
  113. public
  114. void endElement(String namespaceURI, String localName, String qName)
  115. throws org.xml.sax.SAXException {
  116. cat.debug("endElement("+namespaceURI+", "+localName+", "+qName+") called");
  117. chandler.endElement(namespaceURI, localName, qName);
  118. }
  119. public
  120. void endPrefixMapping(String prefix) throws org.xml.sax.SAXException {
  121. cat.debug("endPrefixMapping("+prefix+") called");
  122. chandler.endPrefixMapping(prefix);
  123. }
  124. public
  125. void ignorableWhitespace(char[] ch, int start, int length)
  126. throws org.xml.sax.SAXException {
  127. cat.debug("ignorableWhitespace called");
  128. chandler.ignorableWhitespace(ch, start, length);
  129. }
  130. public
  131. void processingInstruction(java.lang.String target, java.lang.String data)
  132. throws org.xml.sax.SAXException {
  133. cat.debug("processingInstruction called");
  134. chandler.processingInstruction(target, data);
  135. }
  136. public
  137. void setDocumentLocator(Locator locator) {
  138. cat.debug("setDocumentLocator called");
  139. chandler.setDocumentLocator(locator);
  140. }
  141. public
  142. void skippedEntity(String name) throws org.xml.sax.SAXException {
  143. cat.debug("skippedEntity("+name+") called");
  144. chandler.skippedEntity(name);
  145. }
  146. public
  147. void startDocument() throws org.xml.sax.SAXException {
  148. cat.debug("startDocument called");
  149. chandler.startDocument();
  150. }
  151. public
  152. void startElement(String namespaceURI, String localName, String qName,
  153. Attributes atts) throws org.xml.sax.SAXException {
  154. cat.debug("startElement("+namespaceURI+", "+localName+", "+qName+")called");
  155. if("log4j:event".equals(qName)) {
  156. cat.debug("-------------");
  157. if(atts instanceof org.xml.sax.helpers.AttributesImpl) {
  158. AttributesImpl ai = (AttributesImpl) atts;
  159. int i = atts.getIndex("timestamp");
  160. ai.setValue(i, "hello");
  161. }
  162. String ts = atts.getValue("timestamp");
  163. cat.debug("New timestamp is " + ts);
  164. }
  165. chandler.startElement(namespaceURI, localName, qName, atts);
  166. }
  167. public
  168. void startPrefixMapping(String prefix, String uri)
  169. throws org.xml.sax.SAXException {
  170. cat.debug("startPrefixMapping("+prefix+", "+uri+") called");
  171. chandler.startPrefixMapping(prefix, uri);
  172. }
  173. }