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