001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.profiler;
018:
019: import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
020: import org.apache.cocoon.components.sax.XMLByteStreamInterpreter;
021: import org.apache.cocoon.components.sax.XMLDeserializer;
022: import org.apache.cocoon.components.sax.XMLSerializer;
023: import org.apache.cocoon.xml.XMLConsumer;
024: import org.apache.cocoon.xml.XMLPipe;
025: import org.xml.sax.Attributes;
026: import org.xml.sax.Locator;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * This SAX connector measures time taken by each Sitemap component. This
031: * class use the XMLSerializer/Interpreter to buffer the output, and to
032: * seperate the measurement of the time. The SAX fragments were also stored
033: * into the ProfilerData.
034: *
035: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
036: * @author <a href="mailto:bruno@outerthought.org">Bruno Dumon</a>
037: * @version CVS $Id: ProfilingXMLPipe.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class ProfilingXMLPipe implements XMLPipe {
040:
041: private XMLConsumer consumer;
042:
043: // Data of the profile
044: private ProfilerData data;
045:
046: // Index of the component
047: private int index;
048:
049: // Start time
050: private long time;
051:
052: // Time difference
053: private long total;
054:
055: private XMLDeserializer deserializer;
056: private XMLSerializer serializer;
057:
058: /**
059: * Setup this XMLPipe.
060: *
061: * @param index Index of the component.
062: * @param data Data of the profile.
063: */
064: public void setup(int index, ProfilerData data) {
065: this .index = index;
066: this .data = data;
067:
068: // FIXME Retrieve components from the CM
069: this .deserializer = new XMLByteStreamInterpreter();
070: this .serializer = new XMLByteStreamCompiler();
071: }
072:
073: /**
074: * Set the <code>XMLConsumer</code> that will receive XML data.
075: */
076: public void setConsumer(XMLConsumer consumer) {
077: this .consumer = consumer;
078: }
079:
080: public void startDocument() throws SAXException {
081: this .time = System.currentTimeMillis(); // Startup time
082:
083: this .serializer.startDocument();
084: }
085:
086: public void endDocument() throws SAXException {
087: this .total = System.currentTimeMillis() - this .time;
088:
089: this .serializer.endDocument();
090: if (this .index != -1)
091: this .data.setProcessingTime(this .index, this .total);
092:
093: // push the content of the buffer through the next component
094: Object fragment = this .serializer.getSAXFragment();
095:
096: if (this .index != -1)
097: this .data.setSAXFragment(this .index, fragment);
098:
099: this .deserializer.setConsumer(this .consumer);
100:
101: this .time = System.currentTimeMillis(); // Startup time
102: this .deserializer.deserialize(fragment);
103: this .total = System.currentTimeMillis() - this .time;
104:
105: if ((this .index != -1)
106: && (this .index == (this .data.getCount() - 2)))
107: this .data.setProcessingTime(this .index + 1, this .total);
108: }
109:
110: public void setDocumentLocator(Locator locator) {
111: this .serializer.setDocumentLocator(locator);
112: }
113:
114: public void startPrefixMapping(String prefix, String uri)
115: throws SAXException {
116: this .serializer.startPrefixMapping(prefix, uri);
117: }
118:
119: public void endPrefixMapping(String prefix) throws SAXException {
120: this .serializer.endPrefixMapping(prefix);
121: }
122:
123: public void startElement(String uri, String loc, String raw,
124: Attributes a) throws SAXException {
125: this .serializer.startElement(uri, loc, raw, a);
126: }
127:
128: public void endElement(String uri, String loc, String raw)
129: throws SAXException {
130: this .serializer.endElement(uri, loc, raw);
131: }
132:
133: public void characters(char c[], int start, int len)
134: throws SAXException {
135: this .serializer.characters(c, start, len);
136: }
137:
138: public void ignorableWhitespace(char c[], int start, int len)
139: throws SAXException {
140: this .serializer.ignorableWhitespace(c, start, len);
141: }
142:
143: public void processingInstruction(String target, String data)
144: throws SAXException {
145: this .serializer.processingInstruction(target, data);
146: }
147:
148: public void skippedEntity(String name) throws SAXException {
149: this .serializer.skippedEntity(name);
150: }
151:
152: public void startDTD(String name, String publicId, String systemId)
153: throws SAXException {
154: this .serializer.startDTD(name, publicId, systemId);
155: }
156:
157: public void endDTD() throws SAXException {
158: this .serializer.endDTD();
159: }
160:
161: public void startEntity(String name) throws SAXException {
162: this .serializer.startEntity(name);
163: }
164:
165: public void endEntity(String name) throws SAXException {
166: this .serializer.endEntity(name);
167: }
168:
169: public void startCDATA() throws SAXException {
170: this .serializer.startCDATA();
171: }
172:
173: public void endCDATA() throws SAXException {
174: this .serializer.endCDATA();
175: }
176:
177: public void comment(char ch[], int start, int len)
178: throws SAXException {
179: this.serializer.comment(ch, start, len);
180: }
181: }
|