KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > monitor > parser > ParserImpl


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/ParserImpl.java,v 1.4 2004/03/20 20:36:38 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.jmeter.monitor.parser;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.InputSource JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.parsers.SAXParser JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28
29 import org.apache.jmeter.monitor.model.ObjectFactory;
30 import org.apache.jmeter.monitor.model.Status;
31 import org.apache.jmeter.samplers.SampleResult;
32
33 public abstract class ParserImpl implements Parser
34 {
35     private SAXParserFactory JavaDoc PARSERFACTORY = null;
36     private SAXParser JavaDoc PARSER = null;
37     private MonitorHandler DOCHANDLER = null;
38     private ObjectFactory FACTORY = null;
39     
40     /**
41      *
42      */

43     public ParserImpl(ObjectFactory factory)
44     {
45         super();
46         this.FACTORY = factory;
47         try {
48             PARSERFACTORY = SAXParserFactory.newInstance();
49             PARSER = PARSERFACTORY.newSAXParser();
50             DOCHANDLER = new MonitorHandler();
51             DOCHANDLER.setObjectFactory(this.FACTORY);
52         } catch (SAXException JavaDoc e) {
53             // e.printStackTrace();
54
// need to add logging later
55
} catch (ParserConfigurationException JavaDoc e){
56             // e.printStackTrace();
57
// need to add logging later
58
}
59     }
60
61     /**
62      * parse byte array and return Status object
63      * @param bytes
64      * @return Status
65      */

66     public Status parseBytes(byte[] bytes)
67     {
68         try {
69             InputSource JavaDoc is = new InputSource JavaDoc();
70             is.setByteStream(new ByteArrayInputStream JavaDoc(bytes));
71             PARSER.parse(is,DOCHANDLER);
72             return DOCHANDLER.getContents();
73         } catch (SAXException JavaDoc e){
74             e.printStackTrace();
75             // let bad input fail silently
76
return DOCHANDLER.getContents();
77         } catch (IOException JavaDoc e){
78             e.printStackTrace();
79             // let bad input fail silently
80
return DOCHANDLER.getContents();
81         }
82     }
83
84     /**
85      * @param content
86      * @return Status
87      */

88     public Status parseString(String JavaDoc content)
89     {
90         return parseBytes(content.getBytes());
91     }
92
93     /**
94      * @param result
95      * @return Status
96      */

97     public Status parseSampleResult(SampleResult result)
98     {
99         return parseBytes(result.getResponseData());
100     }
101
102 }
103
Popular Tags