markobean commented on a change in pull request #5324:
URL: https://github.com/apache/nifi/pull/5324#discussion_r786327291



##########
File path: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ValidateXml.java
##########
@@ -134,32 +157,73 @@ public void onTrigger(final ProcessContext context, final 
ProcessSession session
         }
 
         final Schema schema = schemaRef.get();
-        final Validator validator = schema.newValidator();
+        final Validator validator = schema == null ? null : 
schema.newValidator();
         final ComponentLog logger = getLogger();
+        final boolean attributeContainsXML = 
context.getProperty(XML_SOURCE_ATTRIBUTE).isSet();
 
         for (FlowFile flowFile : flowFiles) {
             final AtomicBoolean valid = new AtomicBoolean(true);
-            final AtomicReference<Exception> exception = new 
AtomicReference<Exception>(null);
-
-            session.read(flowFile, new InputStreamCallback() {
-                @Override
-                public void process(final InputStream in) throws IOException {
-                    try {
-                        validator.validate(new StreamSource(in));
-                    } catch (final IllegalArgumentException | SAXException e) {
-                        valid.set(false);
-                        exception.set(e);
+            final AtomicReference<Exception> exception = new 
AtomicReference<>(null);
+            SafeXMLConfiguration safeXMLConfiguration = new 
SafeXMLConfiguration();
+            safeXMLConfiguration.setValidating(false);
+
+            try {
+                DocumentBuilder docBuilder = 
safeXMLConfiguration.createDocumentBuilder();
+
+                if (attributeContainsXML) {
+                    // If XML source attribute is set, validate attribute value
+                    String xml = 
flowFile.getAttribute(context.getProperty(XML_SOURCE_ATTRIBUTE).evaluateAttributeExpressions().getValue());
+                    ByteArrayInputStream bais = new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
+
+                    if (validator != null) {
+                        // If schema is provided, validator will be non-null
+                        validator.validate(new StreamSource(bais));
+                    } else {
+                        // Only verify that the XML is well-formed; no schema 
check
+                        docBuilder.parse(bais);
                     }

Review comment:
       Implemented single method for shared functionality.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to