diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/cas/util/XmlUtils.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/cas/util/XmlUtils.java index f9e60b8f..4b17bfdf 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/cas/util/XmlUtils.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/cas/util/XmlUtils.java @@ -39,7 +39,7 @@ public final class XmlUtils { */ public static Document newDocument(final String xml) { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - final Map features = new HashMap(); + final Map features = new HashMap<>(); features.put(XMLConstants.FEATURE_SECURE_PROCESSING, true); features.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); for (final Map.Entry entry : features.entrySet()) { @@ -85,7 +85,7 @@ public final class XmlUtils { * @return the list of text from the elements. */ public static List getTextForElements(final String xmlAsString, final String element) { - final List elements = new ArrayList(2); + final List elements = new ArrayList<>(2); final XMLReader reader = getXmlReader(); final DefaultHandler handler = new DefaultHandler() { @@ -94,6 +94,7 @@ public final class XmlUtils { private StringBuilder buffer = new StringBuilder(); + @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { if (localName.equals(element)) { @@ -101,6 +102,7 @@ public final class XmlUtils { } } + @Override public void endElement(final String uri, final String localName, final String qName) throws SAXException { if (localName.equals(element)) { this.foundElement = false; @@ -109,6 +111,7 @@ public final class XmlUtils { } } + @Override public void characters(char[] ch, int start, int length) throws SAXException { if (this.foundElement) { this.buffer.append(ch, start, length); @@ -145,6 +148,7 @@ public final class XmlUtils { private boolean foundElement = false; + @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { if (localName.equals(element)) { @@ -152,12 +156,14 @@ public final class XmlUtils { } } + @Override public void endElement(final String uri, final String localName, final String qName) throws SAXException { if (localName.equals(element)) { this.foundElement = false; } } + @Override public void characters(char[] ch, int start, int length) throws SAXException { if (this.foundElement) { builder.append(ch, start, length); @@ -177,8 +183,8 @@ public final class XmlUtils { return builder.toString(); } - - + + public static Map extractCustomAttributes(final String xml) { final SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); @@ -195,7 +201,7 @@ public final class XmlUtils { return Collections.emptyMap(); } } - + private static class CustomAttributeHandler extends DefaultHandler { private Map attributes; @@ -208,7 +214,7 @@ public final class XmlUtils { @Override public void startDocument() throws SAXException { - this.attributes = new HashMap(); + this.attributes = new HashMap<>(); } @Override @@ -245,7 +251,7 @@ public final class XmlUtils { if (o instanceof List) { items = (List) o; } else { - items = new LinkedList(); + items = new LinkedList<>(); items.add(o); this.attributes.put(this.currentAttribute, items); } @@ -258,35 +264,35 @@ public final class XmlUtils { return this.attributes; } } - - + + public static void main(String[] args) { - String result = "\r\n" + - " \r\n" + - " admin\r\n" + - " \r\n" + - " UsernamePasswordCredential\r\n" + - " true\r\n" + - " 2019-08-01T19:33:21.527+08:00[Asia/Shanghai]\r\n" + - " RestAuthenticationHandler\r\n" + - " RestAuthenticationHandler\r\n" + - " false\r\n" + - " \r\n" + - " \r\n" + + String result = "\r\n" + + " \r\n" + + " admin\r\n" + + " \r\n" + + " UsernamePasswordCredential\r\n" + + " true\r\n" + + " 2019-08-01T19:33:21.527+08:00[Asia/Shanghai]\r\n" + + " RestAuthenticationHandler\r\n" + + " RestAuthenticationHandler\r\n" + + " false\r\n" + + " \r\n" + + " \r\n" + ""; - - String errorRes = "\r\n" + - " 未能够识别出目标 'ST-5-1g-9cNES6KXNRwq-GuRET103sm0-DESKTOP-VKLS8B3'票根\r\n" + + + String errorRes = "\r\n" + + " 未能够识别出目标 'ST-5-1g-9cNES6KXNRwq-GuRET103sm0-DESKTOP-VKLS8B3'票根\r\n" + ""; - + String error = XmlUtils.getTextForElement(errorRes, "authenticationFailure"); - System.out.println("------"+error); - + log.info("------"+error); + String error2 = XmlUtils.getTextForElement(result, "authenticationFailure"); - System.out.println("------"+error2); + log.info("------"+error2); String principal = XmlUtils.getTextForElement(result, "user"); - System.out.println("---principal---"+principal); + log.info("---principal---"+principal); Map attributes = XmlUtils.extractCustomAttributes(result); - System.out.println("---attributes---"+attributes); + log.info("---attributes---"+attributes); } }