KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > profiler > ProfilingXMLPipe


1 /*
2  * Copyright 1999-2004 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 package org.apache.cocoon.components.profiler;
17
18 import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
19 import org.apache.cocoon.components.sax.XMLByteStreamInterpreter;
20 import org.apache.cocoon.components.sax.XMLDeserializer;
21 import org.apache.cocoon.components.sax.XMLSerializer;
22 import org.apache.cocoon.xml.XMLConsumer;
23 import org.apache.cocoon.xml.XMLPipe;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.Locator JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 /**
29  * This SAX connector measures time taken by each Sitemap component. This
30  * class use the XMLSerializer/Interpreter to buffer the output, and to
31  * seperate the measurement of the time. The SAX fragments were also stored
32  * into the ProfilerData.
33  *
34  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
35  * @author <a HREF="mailto:bruno@outerthought.org">Bruno Dumon</a>
36  * @version CVS $Id: ProfilingXMLPipe.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38 public class ProfilingXMLPipe implements XMLPipe {
39
40     private XMLConsumer consumer;
41
42     // Data of the profile
43
private ProfilerData data;
44
45     // Index of the component
46
private int index;
47
48     // Start time
49
private long time;
50
51     // Time difference
52
private long total;
53
54     private XMLDeserializer deserializer;
55     private XMLSerializer serializer;
56
57     /**
58      * Setup this XMLPipe.
59      *
60      * @param index Index of the component.
61      * @param data Data of the profile.
62      */

63     public void setup(int index, ProfilerData data) {
64         this.index = index;
65         this.data = data;
66
67         // FIXME Retrieve components from the CM
68
this.deserializer = new XMLByteStreamInterpreter();
69         this.serializer = new XMLByteStreamCompiler();
70     }
71
72     /**
73      * Set the <code>XMLConsumer</code> that will receive XML data.
74      */

75     public void setConsumer(XMLConsumer consumer) {
76         this.consumer = consumer;
77     }
78
79     public void startDocument() throws SAXException JavaDoc {
80         this.time = System.currentTimeMillis(); // Startup time
81

82         this.serializer.startDocument();
83     }
84
85     public void endDocument() throws SAXException JavaDoc {
86         this.total = System.currentTimeMillis() - this.time;
87
88         this.serializer.endDocument();
89         if (this.index != -1)
90             this.data.setProcessingTime(this.index, this.total);
91
92         // push the content of the buffer through the next component
93
Object JavaDoc fragment = this.serializer.getSAXFragment();
94
95         if (this.index != -1)
96             this.data.setSAXFragment(this.index, fragment);
97
98         this.deserializer.setConsumer(this.consumer);
99
100         this.time = System.currentTimeMillis(); // Startup time
101
this.deserializer.deserialize(fragment);
102         this.total = System.currentTimeMillis() - this.time;
103
104         if ((this.index != -1) && (this.index==(this.data.getCount()-2)))
105             this.data.setProcessingTime(this.index+1, this.total);
106     }
107
108     public void setDocumentLocator(Locator JavaDoc locator) {
109         this.serializer.setDocumentLocator(locator);
110     }
111
112     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
113         this.serializer.startPrefixMapping(prefix, uri);
114     }
115
116     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
117         this.serializer.endPrefixMapping(prefix);
118     }
119
120     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) throws SAXException JavaDoc {
121         this.serializer.startElement(uri, loc, raw, a);
122     }
123
124     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
125         this.serializer.endElement(uri, loc, raw);
126     }
127
128     public void characters(char c[], int start, int len) throws SAXException JavaDoc {
129         this.serializer.characters(c, start, len);
130     }
131
132     public void ignorableWhitespace(char c[], int start, int len) throws SAXException JavaDoc {
133         this.serializer.ignorableWhitespace(c, start, len);
134     }
135
136     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
137         this.serializer.processingInstruction(target, data);
138     }
139
140     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
141         this.serializer.skippedEntity(name);
142     }
143
144     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
145         this.serializer.startDTD(name, publicId, systemId);
146     }
147
148     public void endDTD() throws SAXException JavaDoc {
149         this.serializer.endDTD();
150     }
151
152     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
153         this.serializer.startEntity(name);
154     }
155
156     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
157         this.serializer.endEntity(name);
158     }
159
160     public void startCDATA() throws SAXException JavaDoc {
161         this.serializer.startCDATA();
162     }
163
164     public void endCDATA() throws SAXException JavaDoc {
165         this.serializer.endCDATA();
166     }
167
168     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
169         this.serializer.comment(ch, start, len);
170     }
171 }
172
Popular Tags