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.jmeter.monitor.parser;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021:
022: import org.xml.sax.SAXException;
023: import org.xml.sax.InputSource;
024:
025: import javax.xml.parsers.ParserConfigurationException;
026: import javax.xml.parsers.SAXParser;
027: import javax.xml.parsers.SAXParserFactory;
028:
029: import org.apache.jmeter.monitor.model.ObjectFactory;
030: import org.apache.jmeter.monitor.model.Status;
031: import org.apache.jmeter.samplers.SampleResult;
032:
033: public abstract class ParserImpl implements Parser {
034: private SAXParserFactory PARSERFACTORY = null;
035:
036: private SAXParser PARSER = null;
037:
038: private MonitorHandler DOCHANDLER = null;
039:
040: private ObjectFactory FACTORY = null;
041:
042: /**
043: *
044: */
045: public ParserImpl(ObjectFactory factory) {
046: super ();
047: this .FACTORY = factory;
048: try {
049: PARSERFACTORY = SAXParserFactory.newInstance();
050: PARSER = PARSERFACTORY.newSAXParser();
051: DOCHANDLER = new MonitorHandler();
052: DOCHANDLER.setObjectFactory(this .FACTORY);
053: } catch (SAXException e) {
054: // e.printStackTrace();
055: // need to add logging later
056: } catch (ParserConfigurationException e) {
057: // e.printStackTrace();
058: // need to add logging later
059: }
060: }
061:
062: /**
063: * parse byte array and return Status object
064: *
065: * @param bytes
066: * @return Status
067: */
068: public Status parseBytes(byte[] bytes) {
069: try {
070: InputSource is = new InputSource();
071: is.setByteStream(new ByteArrayInputStream(bytes));
072: PARSER.parse(is, DOCHANDLER);
073: return DOCHANDLER.getContents();
074: } catch (SAXException e) {
075: e.printStackTrace();
076: // let bad input fail silently
077: return DOCHANDLER.getContents();
078: } catch (IOException e) {
079: e.printStackTrace();
080: // let bad input fail silently
081: return DOCHANDLER.getContents();
082: }
083: }
084:
085: /**
086: * @param content
087: * @return Status
088: */
089: public Status parseString(String content) {
090: return parseBytes(content.getBytes());
091: }
092:
093: /**
094: * @param result
095: * @return Status
096: */
097: public Status parseSampleResult(SampleResult result) {
098: return parseBytes(result.getResponseData());
099: }
100:
101: }
|