【fix sonar】 XmlUtils.java文件

This commit is contained in:
tianwenbo
2023-03-07 15:51:51 +08:00
parent 4d6e083dff
commit 69771d360d
@@ -39,7 +39,7 @@ public final class XmlUtils {
*/ */
public static Document newDocument(final String xml) { public static Document newDocument(final String xml) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final Map<String, Boolean> features = new HashMap<String, Boolean>(); final Map<String, Boolean> features = new HashMap<>();
features.put(XMLConstants.FEATURE_SECURE_PROCESSING, true); features.put(XMLConstants.FEATURE_SECURE_PROCESSING, true);
features.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); features.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
for (final Map.Entry<String, Boolean> entry : features.entrySet()) { for (final Map.Entry<String, Boolean> entry : features.entrySet()) {
@@ -85,7 +85,7 @@ public final class XmlUtils {
* @return the list of text from the elements. * @return the list of text from the elements.
*/ */
public static List<String> getTextForElements(final String xmlAsString, final String element) { public static List<String> getTextForElements(final String xmlAsString, final String element) {
final List<String> elements = new ArrayList<String>(2); final List<String> elements = new ArrayList<>(2);
final XMLReader reader = getXmlReader(); final XMLReader reader = getXmlReader();
final DefaultHandler handler = new DefaultHandler() { final DefaultHandler handler = new DefaultHandler() {
@@ -94,6 +94,7 @@ public final class XmlUtils {
private StringBuilder buffer = new StringBuilder(); private StringBuilder buffer = new StringBuilder();
@Override
public void startElement(final String uri, final String localName, final String qName, public void startElement(final String uri, final String localName, final String qName,
final Attributes attributes) throws SAXException { final Attributes attributes) throws SAXException {
if (localName.equals(element)) { 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 { public void endElement(final String uri, final String localName, final String qName) throws SAXException {
if (localName.equals(element)) { if (localName.equals(element)) {
this.foundElement = false; this.foundElement = false;
@@ -109,6 +111,7 @@ public final class XmlUtils {
} }
} }
@Override
public void characters(char[] ch, int start, int length) throws SAXException { public void characters(char[] ch, int start, int length) throws SAXException {
if (this.foundElement) { if (this.foundElement) {
this.buffer.append(ch, start, length); this.buffer.append(ch, start, length);
@@ -145,6 +148,7 @@ public final class XmlUtils {
private boolean foundElement = false; private boolean foundElement = false;
@Override
public void startElement(final String uri, final String localName, final String qName, public void startElement(final String uri, final String localName, final String qName,
final Attributes attributes) throws SAXException { final Attributes attributes) throws SAXException {
if (localName.equals(element)) { 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 { public void endElement(final String uri, final String localName, final String qName) throws SAXException {
if (localName.equals(element)) { if (localName.equals(element)) {
this.foundElement = false; this.foundElement = false;
} }
} }
@Override
public void characters(char[] ch, int start, int length) throws SAXException { public void characters(char[] ch, int start, int length) throws SAXException {
if (this.foundElement) { if (this.foundElement) {
builder.append(ch, start, length); builder.append(ch, start, length);
@@ -208,7 +214,7 @@ public final class XmlUtils {
@Override @Override
public void startDocument() throws SAXException { public void startDocument() throws SAXException {
this.attributes = new HashMap<String, Object>(); this.attributes = new HashMap<>();
} }
@Override @Override
@@ -245,7 +251,7 @@ public final class XmlUtils {
if (o instanceof List) { if (o instanceof List) {
items = (List<Object>) o; items = (List<Object>) o;
} else { } else {
items = new LinkedList<Object>(); items = new LinkedList<>();
items.add(o); items.add(o);
this.attributes.put(this.currentAttribute, items); this.attributes.put(this.currentAttribute, items);
} }
@@ -280,13 +286,13 @@ public final class XmlUtils {
"</cas:serviceResponse>"; "</cas:serviceResponse>";
String error = XmlUtils.getTextForElement(errorRes, "authenticationFailure"); String error = XmlUtils.getTextForElement(errorRes, "authenticationFailure");
System.out.println("------"+error); log.info("------"+error);
String error2 = XmlUtils.getTextForElement(result, "authenticationFailure"); String error2 = XmlUtils.getTextForElement(result, "authenticationFailure");
System.out.println("------"+error2); log.info("------"+error2);
String principal = XmlUtils.getTextForElement(result, "user"); String principal = XmlUtils.getTextForElement(result, "user");
System.out.println("---principal---"+principal); log.info("---principal---"+principal);
Map<String, Object> attributes = XmlUtils.extractCustomAttributes(result); Map<String, Object> attributes = XmlUtils.extractCustomAttributes(result);
System.out.println("---attributes---"+attributes); log.info("---attributes---"+attributes);
} }
} }