📄 xmlutils.java
字号:
* Parses the string representation,validates with the given schema file
* and returns the XML Document.
*
* @param xmlStr string representaion of the XML Document
* @param url url of schema file
* @return XML Document constructed from the string
* @since 1.0
*/
public static Document stringToXML(String xmlStr, String url) {
// Initialize DOM Parser
DOMParser dp = new DOMParser();
Document xmldoc = null;
// Set Schema Object for Validation
dp.setXMLSchema(getXMLSchema(url));
dp.setValidationMode(XMLParser.SCHEMA_VALIDATION);
dp.setPreserveWhitespace(true);
try {
// Parse the file through the Reader instance and validate with
// schema object
StringReader input = new StringReader(xmlStr);
dp.parse(input);
// Get the Root element of the parsed document
xmldoc = dp.getDocument();
} catch (XMLParseException pe) {
System.out.println("Parser Exception: " + pe.toString());
} catch (Exception e) {
System.out.println("NonParserException: " + e.toString());
}
return xmldoc;
}
} // End of class - XMLUtils
/**
* This class extends the DefaultHandler class and overrides
* the standard callback methods. This class parses the given XML file
* and populates the URLMappings in a Hashmap.
*
* @version 1.0
* @since 1.0
*/
class URLMappingHandler extends DefaultHandler {
// Hashmap to hold URLMappings
private HashMap hmap = new HashMap();
// Instance of URLMap, currently being populated.
private URLMapping urlMap = null;
// Name of the last tag encountered by startElement(..) method
private String lastTag = null;
// List of roles for the current URLMap
private ArrayList roles = new ArrayList();
/**
* Returns the mappings that have been parsed and loaded in Hashmap.
*
* @return HashMap containing URLMappings.
* @since 1.0
*/
public HashMap getMappings() {
return hmap;
}
/**
* Clears the Hashmap.
*
* @since 1.0
*/
public void clearMappings() {
hmap.clear();
}
/**
* This method is notified when a start tag is encountered.
*
* @param namespaceURI namespace URI
* @param sName local name of the tag
* @param qName quantified name of the tag
* @param attrs Attribute list of the tag
* @since 1.0
*/
public void startElement(String namespaceURI, String sName, String qName,
Attributes attrs) {
String tagName = sName;
// If Namespace aware
if ("".equals(sName) || (sName == null)) {
tagName = qName;
}
// A new event was encountered, initialize a new URLMapping
if (tagName.equals("Event")) {
// Start a new URL Mapping
urlMap = new URLMapping();
} else {
lastTag = tagName;
}
}
/**
* This method is notified when a end tag is encountered.
*
* @param namespaceURI namespace URI
* @param sName local name of the tag
* @param qName quantified name of the tag
* @since 1.0
*/
public void endElement(String namespaceURI, String sName, String qName) {
String tagName = sName;
// If namespace aware
if ("".equals(sName) || (sName == null)) {
tagName = qName;
}
// If end of a URL mapping
if (tagName.equals("Event")) {
// Put the URL Mapping into the Hashmap
hmap.put(urlMap.getEventName(), urlMap);
} else if (tagName.equals("Roles")) {
// Load the roles into the URLMap
String[] role = (String[]) roles.toArray(new String[roles.size()]);
urlMap.setRoles(role);
roles.clear();
}
// Reset last tag
lastTag = null;
}
/**
* This method is notified when the characters in-between tags is encountered.
*
* @param ch Character array of the text
* @param offset start position in the char array
* @param len length of the characters to be taken from the char array
* @since 1.0
*/
public void characters(char[] ch, int offset, int len) {
if (len == 0 || lastTag==null) return;
String tagValue = new String(ch, offset, len);
if (lastTag.equals("Name"))
urlMap.setEventName(tagValue);
else if (lastTag.equals("Class"))
urlMap.setClassName(tagValue);
else if (lastTag.equals("Method"))
urlMap.setMethodName(tagValue);
else if (lastTag.equals("Screen"))
urlMap.setNextScreen(tagValue);
else if (lastTag.equals("Role"))
roles.add(tagValue);
}
} // End of class URLMappingHandler
/**
* This class extends the DefaultHandler class and overrides
* the standard callback methods. This class parses the given XML file
* and populates the ExceptionMappings in a Hashmap.
*
* @version 1.0
* @since 1.0
*/
class ExceptionMappingHandler extends DefaultHandler {
// Hashmap to hold ExceptionMappings
private HashMap hmap = new HashMap();
// ExceptionMap that is currently being constructed
private ExceptionMapping excpMap = null;
// Name of the last tag encountered by startElement(..)
private String lastTag = null;
/**
* Returns the mappings that have been parsed and loaded in Hashmap.
*
* @return HashMap containing ExceptionMappings.
* @since 1.0
*/
public HashMap getMappings() {
return hmap;
}
/**
* Clears the Hashmap.
*
* @since 1.0
*/
public void clearMappings() {
hmap.clear();
}
/**
* This method is notified when a start tag is encountered.
*
* @param namespaceURI namespace URI
* @param sName local name of the tag
* @param qName quantified name of the tag
* @param attrs Attribute list of the tag
* @since 1.0
*/
public void startElement(String namespaceURI, String sName, String qName,
Attributes attrs) {
String tagName = sName;
if ("".equals(sName) || (sName == null)) {
tagName = qName;
}
if (tagName.equals("Exception")) {
// Start a new Exception Mapping
excpMap = new ExceptionMapping();
}
else {
lastTag = tagName;
}
}
/**
* This method is notified when a end tag is encountered.
*
* @param namespaceURI namespace URI
* @param sName local name of the tag
* @param qName quantified name of the tag
* @since 1.0
*/
public void endElement(String namespaceURI, String sName, String qName) {
String tagName = sName;
if ("".equals(sName) || (sName == null)) {
tagName = qName;
}
if (tagName.equals("Exception")) {
// Put the URL Mapping into the Hashmap
hmap.put(excpMap.getClassName(), excpMap);
}
lastTag = null;
}
/**
* This method is notified when the characters in-between tags is encountered.
*
* @param ch Character array of the text
* @param offset start position in the char array
* @param len length of the characters to be taken from the char array
* @since 1.0
*/
public void characters(char[] ch, int offset, int len) {
if (len == 0 || lastTag==null) return;
String tagValue = new String(ch, offset, len);
if (lastTag.equals("Class"))
excpMap.setClassName(tagValue);
else if (lastTag.equals("WebErrorPage"))
excpMap.setNextWebScreen(tagValue);
else if (lastTag.equals("MobileErrorPage"))
excpMap.setNextMobileScreen(tagValue);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -