KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > xml > Transform


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import org.xml.sax.Locator JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.XMLReader JavaDoc;
30 import org.xml.sax.ext.LexicalHandler JavaDoc;
31 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc;
49
50
51 import java.io.FileOutputStream JavaDoc;
52 import java.io.IOException JavaDoc;
53
54
55 public class Transform {
56
57   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
58     PropertyConfigurator.disableAll();
59     PropertyConfigurator.configure("x.lcf");
60
61     // I. Instantiate a stylesheet processor.
62
Processor processor = Processor.newInstance("xslt");
63
64     // II. Process the stylesheet. producing a Templates object.
65

66     // Get the XMLReader.
67
XMLReader JavaDoc reader = XMLReaderFactory.createXMLReader();
68
69     // Set the ContentHandler.
70
TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder();
71     reader.setContentHandler(templatesBuilder);
72
73     // Set the ContentHandler to also function as a LexicalHandler, which
74
// includes "lexical" (e.g., comments and CDATA) events. The Xalan
75
// TemplatesBuilder -- org.apache.xalan.processor.StylesheetHandler -- is
76
// also a LexicalHandler).
77
if(templatesBuilder instanceof LexicalHandler JavaDoc) {
78        reader.setProperty("http://xml.org/sax/properties/lexical-handler",
79                            templatesBuilder);
80     }
81
82     // Parse the stylesheet.
83
reader.parse(args[0]);
84
85     //Get the Templates object from the ContentHandler.
86
Templates templates = templatesBuilder.getTemplates();
87
88     // III. Use the Templates object to instantiate a Transformer.
89
Transformer transformer = templates.newTransformer();
90
91     // IV. Perform the transformation.
92

93     // Set up the ContentHandler for the output.
94
FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(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     // Set up the ContentHandler for the input.
102
org.xml.sax.ContentHandler JavaDoc chandler = transformer.getInputContentHandler();
103     DC dc = new DC(chandler);
104     reader.setContentHandler(dc);
105     if(chandler instanceof LexicalHandler JavaDoc) {
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     // Parse the XML input document. The input ContentHandler and
114
// output ContentHandler work in separate threads to optimize
115
// performance.
116
reader.parse(args[1]);
117   }
118 }
119
120  class DC implements ContentHandler JavaDoc {
121
122    static Category cat = Category.getInstance("DC");
123
124    ContentHandler JavaDoc chandler;
125
126    DC(ContentHandler JavaDoc 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 JavaDoc {
134     cat.debug("characters: ["+new String JavaDoc(ch, start, length)+ "] called");
135     chandler.characters(ch, start, length);
136
137   }
138
139   public
140   void endDocument() throws org.xml.sax.SAXException JavaDoc {
141     cat.debug("endDocument called.");
142     chandler.endDocument();
143
144   }
145
146   public
147   void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
148                                            throws org.xml.sax.SAXException JavaDoc {
149     cat.debug("endElement("+namespaceURI+", "+localName+", "+qName+") called");
150     chandler.endElement(namespaceURI, localName, qName);
151   }
152    
153    public
154    void endPrefixMapping(String JavaDoc prefix) throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc {
162     cat.debug("ignorableWhitespace called");
163     chandler.ignorableWhitespace(ch, start, length);
164   }
165   
166   public
167   void processingInstruction(java.lang.String JavaDoc target, java.lang.String JavaDoc data)
168                                               throws org.xml.sax.SAXException JavaDoc {
169     cat.debug("processingInstruction called");
170     chandler.processingInstruction(target, data);
171   }
172
173   public
174   void setDocumentLocator(Locator JavaDoc locator) {
175     cat.debug("setDocumentLocator called");
176     chandler.setDocumentLocator(locator);
177   }
178
179    public
180    void skippedEntity(String JavaDoc name) throws org.xml.sax.SAXException JavaDoc {
181      cat.debug("skippedEntity("+name+") called");
182      chandler.skippedEntity(name);
183    }
184   
185   public
186   void startDocument() throws org.xml.sax.SAXException JavaDoc {
187     cat.debug("startDocument called");
188     chandler.startDocument();
189   }
190   
191   public
192   void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName,
193             Attributes JavaDoc atts) throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc) {
199     AttributesImpl JavaDoc ai = (AttributesImpl JavaDoc) atts;
200     int i = atts.getIndex("timestamp");
201     ai.setValue(i, "hello");
202       }
203       String JavaDoc 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 JavaDoc prefix, String JavaDoc uri)
211                                           throws org.xml.sax.SAXException JavaDoc {
212      cat.debug("startPrefixMapping("+prefix+", "+uri+") called");
213      chandler.startPrefixMapping(prefix, uri);
214    }
215            
216    
217 }
218
219
Popular Tags