Mass conversion to static methods
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / rest / impl / XmlToPATCHBodyReader.java
index eb12029af965d7c11d362c19f24739c7fdab5e4f..fc8ad710e73ba7d2fb0d6dc5d62d26dc9dd8246a 100644 (file)
@@ -25,9 +25,6 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.Provider;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 import org.opendaylight.netconf.sal.rest.api.Draft02;
 import org.opendaylight.netconf.sal.rest.api.RestconfService;
 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
@@ -37,7 +34,7 @@ import org.opendaylight.netconf.sal.restconf.impl.PATCHEntity;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
-import org.opendaylight.restconf.utils.patch.Draft11XmlToPATCHBodyReader;
+import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -59,7 +56,8 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 /**
- * @deprecated This class will be replaced by {@link Draft11XmlToPATCHBodyReader}
+ * @deprecated This class will be replaced by
+ *             {@link org.opendaylight.restconf.jersey.providers.XmlToPATCHBodyReader}
  */
 @Deprecated
 @Provider
@@ -68,25 +66,6 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
         MessageBodyReader<PATCHContext> {
 
     private final static Logger LOG = LoggerFactory.getLogger(XmlToPATCHBodyReader.class);
-    private static final DocumentBuilderFactory BUILDERFACTORY;
-
-    static {
-        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        try {
-            factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
-            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
-            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
-            factory.setXIncludeAware(false);
-            factory.setExpandEntityReferences(false);
-        } catch (final ParserConfigurationException e) {
-            throw new ExceptionInInitializerError(e);
-        }
-        factory.setNamespaceAware(true);
-        factory.setCoalescing(true);
-        factory.setIgnoringElementContentWhitespace(true);
-        factory.setIgnoringComments(true);
-        BUILDERFACTORY = factory;
-    }
 
     @Override
     public boolean isReadable(final Class<?> type, final Type genericType,
@@ -108,14 +87,7 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
                 return new PATCHContext(path, null, null);
             }
 
-            final DocumentBuilder dBuilder;
-            try {
-                dBuilder = BUILDERFACTORY.newDocumentBuilder();
-            } catch (final ParserConfigurationException e) {
-                throw new IllegalStateException("Failed to parse XML document", e);
-            }
-            final Document doc = dBuilder.parse(entityStream);
-
+            final Document doc = UntrustedXML.newDocumentBuilder().parse(entityStream);
             return parse(path, doc);
         } catch (final RestconfDocumentedException e) {
             throw e;
@@ -127,7 +99,7 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
         }
     }
 
-    private PATCHContext parse(final InstanceIdentifierContext<?> pathContext, final Document doc) {
+    private static PATCHContext parse(final InstanceIdentifierContext<?> pathContext, final Document doc) {
         final List<PATCHEntity> resultCollection = new ArrayList<>();
         final String patchId = doc.getElementsByTagName("patch-id").item(0).getFirstChild().getNodeValue();
         final NodeList editNodes = doc.getElementsByTagName("edit");
@@ -210,15 +182,15 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
      * @param operation Name of current operation
      * @return List of value elements
      */
-    private List<Element> readValueNodes(@Nonnull final Element element, @Nonnull final String operation) {
+    private static List<Element> readValueNodes(@Nonnull final Element element, @Nonnull final String operation) {
         final Node valueNode = element.getElementsByTagName("value").item(0);
 
-        if (PATCHEditOperation.isPatchOperationWithValue(operation) && valueNode == null) {
+        if (PATCHEditOperation.isPatchOperationWithValue(operation) && (valueNode == null)) {
             throw new RestconfDocumentedException("Error parsing input",
                     ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
         }
 
-        if (!PATCHEditOperation.isPatchOperationWithValue(operation) && valueNode != null) {
+        if (!PATCHEditOperation.isPatchOperationWithValue(operation) && (valueNode != null)) {
             throw new RestconfDocumentedException("Error parsing input",
                     ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
         }
@@ -248,9 +220,8 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
      * @param revision Module revision
      * @return Non-conditional XPath
      */
-    private String prepareNonCondXpath(@Nonnull final DataSchemaNode schemaNode, @Nonnull final String target,
-                                       @Nonnull final Element value, @Nonnull final String namespace,
-                                       @Nonnull String revision) {
+    private static String prepareNonCondXpath(@Nonnull final DataSchemaNode schemaNode, @Nonnull final String target,
+            @Nonnull final Element value, @Nonnull final String namespace, @Nonnull final String revision) {
         final Iterator<String> args = Splitter.on("/").split(target.substring(target.indexOf(':') + 1)).iterator();
 
         final StringBuilder nonCondXpath = new StringBuilder();
@@ -262,12 +233,12 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
             nonCondXpath.append(s);
             childNode = ((DataNodeContainer) childNode).getDataChildByName(QName.create(namespace, revision, s));
 
-            if (childNode instanceof ListSchemaNode && args.hasNext()) {
+            if ((childNode instanceof ListSchemaNode) && args.hasNext()) {
                 appendKeys(nonCondXpath, ((ListSchemaNode) childNode).getKeyDefinition().iterator(), args);
             }
         }
 
-        if (childNode instanceof ListSchemaNode && value != null) {
+        if ((childNode instanceof ListSchemaNode) && (value != null)) {
             final Iterator<String> keyValues = readKeyValues(value,
                     ((ListSchemaNode) childNode).getKeyDefinition().iterator());
             appendKeys(nonCondXpath, ((ListSchemaNode) childNode).getKeyDefinition().iterator(), keyValues);
@@ -282,7 +253,7 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
      * @param keys Iterator of list keys names
      * @return Iterator of list keys values
      */
-    private Iterator<String> readKeyValues(@Nonnull final Element value, @Nonnull final Iterator<QName> keys) {
+    private static Iterator<String> readKeyValues(@Nonnull final Element value, @Nonnull final Iterator<QName> keys) {
         final List<String> result = new ArrayList<>();
 
         while (keys.hasNext()) {
@@ -298,7 +269,7 @@ public class XmlToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider i
      * @param keyNames Iterator of list keys names
      * @param keyValues Iterator of list keys values
      */
-    private void appendKeys(@Nonnull final StringBuilder nonCondXpath, @Nonnull final Iterator<QName> keyNames,
+    private static void appendKeys(@Nonnull final StringBuilder nonCondXpath, @Nonnull final Iterator<QName> keyNames,
                             @Nonnull final Iterator<String> keyValues) {
         while (keyNames.hasNext()) {
             nonCondXpath.append("[");