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: */
018:
019: package org.apache.jmeter.protocol.http.modifier;
020:
021: import java.io.CharArrayWriter;
022: import java.util.HashMap;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.xml.sax.Attributes;
028: import org.xml.sax.ContentHandler;
029: import org.xml.sax.Locator;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * The handler used to read in XML parameter data.
034: *
035: * @author Mark Walsh
036: * @version $Revision: 493789 $
037: */
038: public class UserParameterXMLContentHandler implements ContentHandler {
039: // -------------------------------------------
040: // Constants and Data Members
041: // -------------------------------------------
042:
043: // Note UserParameterXML accesses this variable
044: // to obtain the Set data via method getParsedParameters()
045: private List userThreads = new LinkedList();
046:
047: private String paramname = "";
048:
049: private String paramvalue = "";
050:
051: private Map nameValuePair = new HashMap();
052:
053: /** Buffer for collecting data from the "characters" SAX event. */
054: private CharArrayWriter contents = new CharArrayWriter();
055:
056: // -------------------------------------------
057: // Methods
058: // -------------------------------------------
059:
060: /*-------------------------------------------------------------------------
061: * Methods implemented from org.xml.sax.ContentHandler
062: *----------------------------------------------------------------------- */
063: public void setDocumentLocator(Locator locator) {
064: }
065:
066: public void startDocument() throws SAXException {
067: }
068:
069: public void endDocument() throws SAXException {
070: }
071:
072: public void startPrefixMapping(String prefix, String uri)
073: throws SAXException {
074: }
075:
076: public void endPrefixMapping(String prefix) throws SAXException {
077: }
078:
079: public void startElement(String namespaceURL, String localName,
080: String qName, Attributes atts) throws SAXException {
081:
082: contents.reset();
083:
084: // haven't got to reset paramname & paramvalue
085: // but did it to keep the code looking correct
086: if (qName.equals("parameter")) {
087: paramname = "";
088: paramvalue = "";
089: }
090:
091: // must create a new object,
092: // or else end up with a set full of the same Map object
093: if (qName.equals("thread")) {
094: nameValuePair = new HashMap();
095: }
096:
097: }
098:
099: public void endElement(String namespaceURI, String localName,
100: String qName) throws SAXException {
101: if (qName.equals("paramname")) {
102: paramname = contents.toString();
103: }
104: if (qName.equals("paramvalue")) {
105: paramvalue = contents.toString();
106: }
107: if (qName.equals("parameter")) {
108: nameValuePair.put(paramname, paramvalue);
109: }
110: if (qName.equals("thread")) {
111: userThreads.add(nameValuePair);
112: }
113: }
114:
115: public void characters(char ch[], int start, int length)
116: throws SAXException {
117: contents.write(ch, start, length);
118: }
119:
120: public void ignorableWhitespace(char ch[], int start, int length)
121: throws SAXException {
122: }
123:
124: public void processingInstruction(String target, String date)
125: throws SAXException {
126: }
127:
128: public void skippedEntity(String name) throws SAXException {
129: }
130:
131: /*-------------------------------------------------------------------------
132: * Methods (used by UserParameterXML to get XML parameters from XML file)
133: *----------------------------------------------------------------------- */
134:
135: /**
136: * results of parsing all user parameter data defined in XML file.
137: *
138: * @return all users name value pairs obtained from XML file
139: */
140: public List getParsedParameters() {
141: return userThreads;
142: }
143: }
|