📄 readerutils.java
字号:
return value;
}
/**
* getBooleanAttribute purpose.
*
* <p>
* Used to help with XML manipulations. Returns the first child integer
* attribute of the specified name. An exception occurs when the node is
* required and not found.
* </p>
*
* @param elem The root element to look for children in.
* @param attName The name of the attribute to look for.
* @param mandatory true when an exception should be thrown if the
* attribute element does not exist.
* @param defaultValue what to return for a non-mandatory that is not
* found.
*
* @return The value if the attribute was found, the false otherwise.
*
* @throws Exception When a child attribute is required and
* not found.
*/
public static boolean getBooleanAttribute(Element elem, String attName, boolean mandatory,
boolean defaultValue) throws Exception {
String value = getAttribute(elem, attName, mandatory);
if ((value == null) || (value == "")) {
return defaultValue;
}
return Boolean.valueOf(value).booleanValue();
}
/**
* getChildText purpose.
*
* <p>
* Used to help with XML manipulations. Returns the first child text value
* of the specified element name.
* </p>
*
* @param root The root element to look for children in.
* @param childName The name of the attribute to look for.
*
* @return The value if the child was found, the null otherwise.
*/
public static String getChildText(Element root, String childName) {
try {
return getChildText(root, childName, false);
} catch (Exception ex) {
return null;
}
}
/**
* getChildText purpose.
*
* <p>
* Used to help with XML manipulations. Returns the first child text value
* of the specified element name. An exception occurs when the node is
* required and not found.
* </p>
*
* @param root The root element to look for children in.
* @param childName The name of the attribute to look for.
* @param mandatory true when an exception should be thrown if the text
* does not exist.
*
* @return The value if the child was found, the null otherwise.
*
* @throws Exception When a child attribute is required and
* not found.
*/
public static String getChildText(Element root, String childName, boolean mandatory)
throws Exception {
Element elem = getChildElement(root, childName, mandatory);
if (elem != null) {
return getElementText(elem, mandatory);
} else {
if (mandatory) {
String msg = "Mandatory child " + childName + "not found in " + " element: " + root;
throw new Exception(msg);
}
return null;
}
}
/**
* getChildText purpose.
*
* <p>
* Used to help with XML manipulations. Returns the text value of the
* specified element name.
* </p>
*
* @param elem The root element to look for children in.
*
* @return The value if the text was found, the null otherwise.
*/
public static String getElementText(Element elem) {
try {
return getElementText(elem, false);
} catch (Exception ex) {
return null;
}
}
/**
* getChildText purpose.
*
* <p>
* Used to help with XML manipulations. Returns the text value of the
* specified element name. An exception occurs when the node is required
* and not found.
* </p>
*
* @param elem The root element to look for children in.
* @param mandatory true when an exception should be thrown if the text
* does not exist.
*
* @return The value if the text was found, the null otherwise.
*
* @throws Exception When text is required and not found.
*/
public static String getElementText(Element elem, boolean mandatory)
throws Exception {
String value = null;
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer(new StringBuffer("getting element text for ").append(elem).toString());
}
if (elem != null) {
Node child;
NodeList childs = elem.getChildNodes();
int nChilds = childs.getLength();
for (int i = 0; i < nChilds; i++) {
child = childs.item(i);
if (child.getNodeType() == Node.TEXT_NODE) {
value = child.getNodeValue();
if (mandatory && "".equals(value.trim())) {
throw new Exception(elem.getNodeName() + " text is empty");
}
break;
}
}
if (mandatory && (value == null)) {
throw new Exception(elem.getNodeName() + " element does not contains text");
}
} else {
throw new Exception("Argument element can't be null");
}
return value;
}
/**
* getKeyWords purpose.
*
* <p>
* Used to help with XML manipulations. Returns a list of keywords that
* were found.
* </p>
*
* @param keywordsElem The root element to look for children in.
*
* @return The list of keywords that were found.
*/
public static List getKeyWords(Element keywordsElem) {
NodeList klist = keywordsElem.getElementsByTagName("keyword");
int kCount = klist.getLength();
List keywords = new ArrayList(kCount);
String kword;
Element kelem;
for (int i = 0; i < kCount; i++) {
kelem = (Element) klist.item(i);
kword = getElementText(kelem);
if (kword != null) {
keywords.add(kword);
}
}
Object[] s = (Object[]) keywords.toArray();
if (s == null) {
return new ArrayList();
}
ArrayList ss = new ArrayList(s.length);
for (int i = 0; i < s.length; i++)
ss.add(s[i]);
return ss;
}
/**
* getFirstChildElement purpose.
*
* <p>
* Used to help with XML manipulations. Returns the element which
* represents the first child.
* </p>
*
* @param root The root element to look for children in.
*
* @return The element if a child was found, the null otherwise.
*/
public static Element getFirstChildElement(Element root) {
Node child = root.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
return (Element) child;
}
child = child.getNextSibling();
}
return null;
}
/**
* getDoubleAttribute purpose.
*
* <p>
* Used to help with XML manipulations. Returns the first child integer
* attribute of the specified name. An exception occurs when the node is
* required and not found.
* </p>
*
* @param elem The root element to look for children in.
* @param attName The name of the attribute to look for.
* @param mandatory true when an exception should be thrown if the
* attribute element does not exist.
*
* @return The double value if the attribute was found, the NaN otherwise.
*
* @throws Exception When a attribute element is required and
* not found.
*/
public static double getDoubleAttribute(Element elem, String attName, boolean mandatory)
throws Exception {
String value = getAttribute(elem, attName, mandatory);
if ((value == null) || (value == "")) {
return 0.0;
}
double d = Double.NaN;
if (value != null) {
try {
d = Double.parseDouble(value);
} catch (NumberFormatException ex) {
throw new ConfigurationException("Illegal attribute value for " + attName
+ " in element " + elem.getNodeName() + ". Expected double, but was " + value);
}
}
return d;
}
/**
* Validates an xml document against a specified schema.
*
* @param xml The document.
* @param errorHandler The validation error handler.
* @param targetNamespace The target namespace of the schema, may be <code>null</code>
* @param schemaLocation The location of the schema to validate against, may be <code>null</code>
*
* @throws RuntimeException If reader failed to parse properly.
*/
public static void validate( Document xml, DefaultHandler errorHandler, String targetNamespace, String schemaLocation ) {
try {
Transformer tx = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream output = new ByteArrayOutputStream();
tx.transform( new DOMSource( xml ), new StreamResult( output ) );
InputStreamReader reader =
new InputStreamReader( new ByteArrayInputStream( output.toByteArray() ) );
validate( reader, errorHandler, targetNamespace, schemaLocation );
}
catch( Exception e ) {
throw new RuntimeException( e );
}
}
/**
* Validates an xml document against a specified schema.
*
* @param xml Reader representing xml stream to parse.
* @param errorHandler The validation error handler.
* @param targetNamespace The target namespace of the schema, may be <code>null</code>
* @param schemaLocation The location of the schema to validate against, may be <code>null</code>
*
* @throws RuntimeException If reader failed to parse properly.
*/
public static void validate ( Reader xml, DefaultHandler errorHandler, String targetNamespace, String schemaLocation ) {
InputSource in = new InputSource( xml );
try {
//TODO: pretty sure this doesn't actually do validation
// ahhh... xml in java....
SAXParserFactory sf = SAXParserFactory.newInstance();
sf.setNamespaceAware(true);
sf.setValidating(true);
SAXParser parser = sf.newSAXParser();
parser.setProperty(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
// SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
// parser.setProperty("http://xml.org/sax/features/validation", Boolean.TRUE);
//
// parser.setProperty("http://apache.org/xml/features/validation/schema",
// Boolean.TRUE);
// parser.setProperty("http://apache.org/xml/features/validation/schema-full-checking",
// Boolean.TRUE);
if ( schemaLocation != null ) {
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaLocation);
// if ( targetNamespace != null ) {
// parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
// targetNamespace + " " + schemaLocation );
// }
}
parser.parse( in, errorHandler);
}
catch( Exception e ) {
String msg = "Error reading : " + xml;
throw new RuntimeException ( msg, e );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -