📄 xmlcontenthandler.java
字号:
package com.wiley.compBooks.EJwithUML.BillingSystemInterface;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import java.util.HashMap;
import java.util.ArrayList;
/**
* This is a ContentHandler class for SOAP/XML request messages. It ignores
* namespace information as well as attributes associated with elements.
*/
class XMLContentHandler implements ContentHandler
{
private String currentElement = "";
private HashMap xmlRequest;
private ArrayList users = new ArrayList();
/**
* Holds onto the locator for location information.
*/
private Locator locator;
/**
* Creates a ContentHandler that populates HashMap with request data
* from SOAP message. It uses the tag name as the key.
*/
public XMLContentHandler(HashMap request)
{
this.xmlRequest = request;
}
public void setDocumentLocator(Locator locator)
{
this.locator = locator;
}
/**
* This indicates the start of a SOAP message - <...Envelope...>
*/
public void startDocument() throws SAXException
{
System.out.println("XMLContentHandler.startDocument()- parsing begins.");
}
/**
* This indicates the end of a SOAP request message - </Envelope>
*/
public void endDocument() throws SAXException
{
System.out.println("XMLContentHandler.endDocument()- parsing ends.");
}
/**
* This indicates that a processing instruction (other than
* the XML declaration) has been encountered. This is not used.
*/
public void processingInstruction(String target, String data) throws SAXException
{
//ignore
}
/**
* This is not used - No OP!
*/
public void startPrefixMapping(String prefix, String xmlSource)
{
// ignore
}
/**
* This is not used - No OP!
*/
public void endPrefixMapping(String prefix)
{
// ignore
}
/**
* This captures the name of the request data as well as the request name.
* The namespace is ignored. Attributes are also ignored.
*/
public void startElement(String namespaceURI, String localName, String rawName,
Attributes atts) throws SAXException
{
if (currentElement.equals(XmlTimeEntryReportRequestTags.BODY))
{
xmlRequest.put(XmlTimeEntryReportRequestTags.REQUEST_NAME,
localName);
System.out.println("XMLContentHandler.startElement()- Request name:"
+ localName);
}
else
{
System.out.println("XMLContentHandler.startElement()-request data name: "
+ localName);
}
currentElement = localName;
if (currentElement.equals(XmlTimeEntryReportRequestTags.USERS))
{
xmlRequest.put(XmlTimeEntryReportRequestTags.USERS, users);
}
if (currentElement.equals(XmlTimeEntryReportRequestTags.ALL_USERS))
{
xmlRequest.put(XmlTimeEntryReportRequestTags.ALL_USERS,
new Boolean(true));
}
}
/**
* This indicates the end of a element of a SOAP request. It is ignored.
*/
public void endElement(String namespaceURI, String localName, String rawName)
throws SAXException
{
// ignore
}
/**
* This reports the value of as character data associated with the current
* request data.
*/
public void characters(char[] ch, int start, int end) throws SAXException
{
String value = new String(ch, start, end).trim();
if (!value.equals(""))
{
if (currentElement.equals(XmlTimeEntryReportRequestTags.USER_NAME))
{
users.add(value);
}
else
{
xmlRequest.put(currentElement, value);
}
}
System.out.println("XMLContentHandler.characters()-data(" +
currentElement + ", " + value + ").");
}
/**
* This reports whitespace that can be ignored in the originating document.
* This is typically invoked only when validation is ocurring in the parsing
* process. This is ignored.
*/
public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException
{
// ignored
}
/**
* This reports an entity that is skipped by the parser. This should only
* occur for non-validating parsers, and then is still
* implementation-dependent behavior. This is ignored.
*/
public void skippedEntity(String name) throws SAXException
{
// ignored
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -