This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 693b324 plc4j: fixed issues reported by sonar
693b324 is described below
commit 693b3249cb16f989476e00736bddd9aa222f8a08
Author: Sebastian Rühl <[email protected]>
AuthorDate: Sat Jun 19 12:20:39 2021 +0200
plc4j: fixed issues reported by sonar
---
.../mspec/parser/MessageFormatListener.java | 24 +++++----------
.../plc4x/java/knxnetip/ets5/Ets5Parser.java | 22 +++++---------
.../netty/config/PcapReplayChannelConfig.java | 8 ++---
.../internal/handlers/ApiResponseHandler.java | 11 +++++--
.../java/org/apache/plc4x/codegen/DFDLUtil.java | 34 +++++++++++++---------
5 files changed, 50 insertions(+), 49 deletions(-)
diff --git
a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
index d9d0447..15edba1 100644
---
a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
+++
b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
@@ -43,7 +43,6 @@ import java.util.*;
public class MessageFormatListener extends MSpecBaseListener {
-
private Deque<List<Field>> parserContexts;
private Deque<List<EnumValue>> enumContexts;
@@ -96,10 +95,7 @@ public class MessageFormatListener extends MSpecBaseListener
{
parserArguments, null);
types.put(typeName, enumType);
enumContexts.pop();
- }
-
- // Handle data-io types.
- else if (ctx.dataIoTypeSwitch != null) {
+ } else if (ctx.dataIoTypeSwitch != null) { // Handle data-io types.
SwitchField switchField = getSwitchField();
DefaultDataIoTypeDefinition type = new DefaultDataIoTypeDefinition(
typeName, parserArguments, null, switchField);
@@ -114,10 +110,7 @@ public class MessageFormatListener extends
MSpecBaseListener {
}
}
parserContexts.pop();
- }
-
- // Handle all other types.
- else {
+ } else { // Handle all other types.
// If the type has sub-types it's an abstract type.
SwitchField switchField = getSwitchField();
boolean abstractType = switchField != null;
@@ -356,11 +349,10 @@ public class MessageFormatListener extends
MSpecBaseListener {
String typeName = ctx.name.getText();
List<Argument> parserArguments = new LinkedList<>();
// For DataIO types, add all the arguments from the parent type.
- if (!(ctx.parent.parent.parent.parent instanceof
MSpecParser.ComplexTypeContext)) {
- if (((MSpecParser.ComplexTypeContext)
ctx.parent.parent.parent).params != null) {
- parserArguments.addAll(Arrays.asList(getParserArguments(
- ((MSpecParser.ComplexTypeContext)
ctx.parent.parent.parent).params.argument())));
- }
+ if (!(ctx.parent.parent.parent.parent instanceof
MSpecParser.ComplexTypeContext)
+ && ((MSpecParser.ComplexTypeContext)
ctx.parent.parent.parent).params != null) {
+ parserArguments.addAll(Arrays.asList(getParserArguments(
+ ((MSpecParser.ComplexTypeContext)
ctx.parent.parent.parent).params.argument())));
}
// Add all eventually existing local arguments.
if (ctx.argumentList() != null) {
@@ -416,7 +408,7 @@ public class MessageFormatListener extends
MSpecBaseListener {
MSpecParser.ExpressionContext expression =
ctx.constantValueExpressions.expression(i);
String constant = unquoteString(expression.getText());
// String expressions are double escaped
- if (constant.startsWith("\"")) {
+ if (constant != null && constant.startsWith("\"")) {
constant = unquoteString(constant);
}
constants.put(constantName, constant);
@@ -448,7 +440,7 @@ public class MessageFormatListener extends
MSpecBaseListener {
SimpleTypeReference.SimpleBaseType simpleBaseType =
SimpleTypeReference.SimpleBaseType.valueOf(ctx.base.getText().toUpperCase());
// String types need an additional "encoding" field and length
expression.
- if(simpleBaseType == SimpleTypeReference.SimpleBaseType.STRING) {
+ if (simpleBaseType == SimpleTypeReference.SimpleBaseType.STRING) {
String encoding = (ctx.encoding != null) ? ctx.encoding.getText()
: "UTF-8";
Term lengthExpression =
getExpressionTerm(ctx.length.getText().substring(1,
ctx.length.getText().length() - 1));
return new DefaultStringTypeReference(simpleBaseType,
lengthExpression, encoding);
diff --git
a/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/ets5/Ets5Parser.java
b/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/ets5/Ets5Parser.java
index 65d0756..588b25c 100644
---
a/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/ets5/Ets5Parser.java
+++
b/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/ets5/Ets5Parser.java
@@ -32,6 +32,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
+import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -46,6 +47,8 @@ public class Ets5Parser {
public Ets5Model parse(File knxprojFile) {
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
+ factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+ factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
DocumentBuilder builder = factory.newDocumentBuilder();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
@@ -57,7 +60,7 @@ public class Ets5Parser {
////////////////////////////////////////////////////////////////////////////////
ZipArchiveEntry projectHeaderFile =
zipFile.getEntry("P-05CD/project.xml");
if (projectHeaderFile == null) {
- throw new RuntimeException("Error accessing project header
file.");
+ throw new PlcRuntimeException("Error accessing project
header file.");
}
Document projectHeaderDoc =
builder.parse(zipFile.getInputStream(projectHeaderFile));
final XPathExpression xpathGroupAddressStyle =
xPath.compile("/KNX/Project/ProjectInformation/@GroupAddressStyle");
@@ -69,7 +72,7 @@ public class Ets5Parser {
////////////////////////////////////////////////////////////////////////////////
ZipArchiveEntry knxMasterDataFile =
zipFile.getEntry("knx_master.xml");
if (knxMasterDataFile == null) {
- throw new RuntimeException("Error accessing KNX master
file.");
+ throw new PlcRuntimeException("Error accessing KNX master
file.");
}
Document knxMasterDoc =
builder.parse(zipFile.getInputStream(knxMasterDataFile));
final XPathExpression xpathDatapointSubtype =
xPath.compile("//DatapointSubtype");
@@ -97,7 +100,7 @@ public class Ets5Parser {
////////////////////////////////////////////////////////////////////////////////
ZipArchiveEntry projectFile = zipFile.getEntry("P-05CD/0.xml");
if (projectFile == null) {
- throw new RuntimeException("Error accessing project
file.");
+ throw new PlcRuntimeException("Error accessing project
file.");
}
Document projectDoc =
builder.parse(zipFile.getInputStream(projectFile));
@@ -163,18 +166,9 @@ public class Ets5Parser {
}
return new Ets5Model(groupAddressStyleCode, groupAddresses,
topologyNames);
}
- } catch (IOException e) {
+ } catch (IOException | ParserConfigurationException | SAXException |
XPathExpressionException e) {
// Zip and Xml Stuff
throw new PlcRuntimeException(e);
- } catch (ParserConfigurationException e) {
- // XML Stuff
- throw new PlcRuntimeException(e);
- } catch (SAXException e) {
- // XML Stuff
- throw new PlcRuntimeException(e);
- } catch (XPathExpressionException e) {
- // XML Stuff
- throw new PlcRuntimeException(e);
}
}
@@ -186,7 +180,7 @@ public class Ets5Parser {
} else if ("Free".equals(knxprojValue)) {
return (byte) 1;
}
- throw new RuntimeException("Unsupported GroupAddressStyle=" +
knxprojValue);
+ throw new PlcRuntimeException("Unsupported GroupAddressStyle=" +
knxprojValue);
}
}
diff --git
a/plc4j/utils/pcap-replay/src/main/java/org/apache/plc4x/java/utils/pcapreplay/netty/config/PcapReplayChannelConfig.java
b/plc4j/utils/pcap-replay/src/main/java/org/apache/plc4x/java/utils/pcapreplay/netty/config/PcapReplayChannelConfig.java
index c9b70aa..6ec198c 100644
---
a/plc4j/utils/pcap-replay/src/main/java/org/apache/plc4x/java/utils/pcapreplay/netty/config/PcapReplayChannelConfig.java
+++
b/plc4j/utils/pcap-replay/src/main/java/org/apache/plc4x/java/utils/pcapreplay/netty/config/PcapReplayChannelConfig.java
@@ -26,10 +26,10 @@ import java.util.Map;
public class PcapReplayChannelConfig extends PcapChannelConfig {
- public static float SPEED_SLOW_HALF = 0.5f;
- public static float SPEED_REALTIME = 1f;
- public static float SPEED_FAST_DOUBLE = 2f;
- public static float SPEED_FAST_FULL = 0f;
+ public static final float SPEED_SLOW_HALF = 0.5f;
+ public static final float SPEED_REALTIME = 1f;
+ public static final float SPEED_FAST_DOUBLE = 2f;
+ public static final float SPEED_FAST_FULL = 0f;
private float speedFactor = SPEED_REALTIME;
private boolean loop = false;
diff --git
a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/internal/handlers/ApiResponseHandler.java
b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/internal/handlers/ApiResponseHandler.java
index 1631e50..c657150 100644
---
a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/internal/handlers/ApiResponseHandler.java
+++
b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/internal/handlers/ApiResponseHandler.java
@@ -18,6 +18,7 @@ under the License.
*/
package org.apache.plc4x.test.driver.internal.handlers;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.messages.PlcResponse;
import org.apache.plc4x.java.spi.utils.XmlSerializable;
import org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException;
@@ -25,6 +26,7 @@ import
org.apache.plc4x.test.driver.internal.utils.Synchronizer;
import org.apache.plc4x.test.driver.internal.validator.ApiValidator;
import org.dom4j.Element;
+import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -56,8 +58,11 @@ public class ApiResponseHandler {
PlcResponse plcResponse;
try {
plcResponse = synchronizer.responseFuture.get(5000,
TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new DriverTestsuiteException("Got no response within
5000ms.", e);
} catch (Exception e) {
- throw new DriverTestsuiteException("Got no response within
5000ms.");
+ throw new DriverTestsuiteException("Got no response within
5000ms.", e);
}
// Reset the future.
@@ -79,6 +84,8 @@ public class ApiResponseHandler {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
+ tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // Compliant
+ tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); //
Compliant
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
@@ -86,7 +93,7 @@ public class ApiResponseHandler {
transformer.transform(domSource, result);
return writer.toString();
} catch (ParserConfigurationException | TransformerException e) {
- throw new RuntimeException(e);
+ throw new PlcRuntimeException(e);
}
}
diff --git
a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/DFDLUtil.java
b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/DFDLUtil.java
index b6c67c4..3640728 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/DFDLUtil.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/DFDLUtil.java
@@ -22,8 +22,11 @@ import org.apache.plc4x.codegen.ast.*;
import org.apache.plc4x.codegen.util.PojoFactory;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
+import org.xml.sax.SAXException;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
@@ -32,14 +35,18 @@ import java.util.Iterator;
*/
public class DFDLUtil {
- private static final Namespace xsNamespace = new Namespace("xs",
"http://www.w3.org/2001/XMLSchema");
- private static final QName complexType = new QName("complexType",
xsNamespace);
- private static final QName sequence = new QName("sequence", xsNamespace);
- private static final QName ELEMENT = new QName("element", xsNamespace);
+ private static final Namespace XS_NAMESPACE = new Namespace("xs",
"http://www.w3.org/2001/XMLSchema");
+ private static final QName COMPLEX_TYPE = new QName("complexType",
XS_NAMESPACE);
+ private static final QName SEQUENCE = new QName("sequence", XS_NAMESPACE);
+ private static final QName ELEMENT = new QName("element", XS_NAMESPACE);
- public void transform(File dfdlSchemaFile, File outputDir) {
- assert outputDir.exists();
- assert outputDir.isDirectory();
+ public void transform(File dfdlSchemaFile, File outputDir) throws
IOException {
+ if (!outputDir.exists()) {
+ throw new FileNotFoundException(outputDir.getAbsolutePath() + "
does not exist");
+ }
+ if (!outputDir.isDirectory()) {
+ throw new FileNotFoundException(outputDir.getAbsolutePath() + " is
not a directory");
+ }
final Document schema = parseDFDLSchema(dfdlSchemaFile);
@@ -48,7 +55,7 @@ public class DFDLUtil {
final PojoFactory factory = new PojoFactory();
while (iterator.hasNext()) {
final Element element = iterator.next();
- final Iterator<Element> sequenceIterator =
element.elementIterator(sequence);
+ final Iterator<Element> sequenceIterator =
element.elementIterator(SEQUENCE);
final Element sequence = getSequence(sequenceIterator);
// Now make a POJO with all "elements" as fields
@@ -75,22 +82,23 @@ public class DFDLUtil {
private Element getSequence(Iterator<Element> sequenceIterator) {
assert sequenceIterator.hasNext();
final Element sequence = sequenceIterator.next();
- assert sequenceIterator.hasNext() == false;
+ assert !sequenceIterator.hasNext();
return sequence;
}
- private Document parseDFDLSchema(File schemaFile) {
+ private Document parseDFDLSchema(File schemaFile) throws IOException {
try {
SAXReader reader = new SAXReader();
+
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
return reader.read(schemaFile);
- } catch (DocumentException e) {
+ } catch (DocumentException | SAXException e) {
// Do something
- throw new RuntimeException("Unable to read DFDL Schema File", e);
+ throw new IOException("Unable to read DFDL Schema File", e);
}
}
private Iterator<Element> getMainTypes(Document dfdlSchema) {
Element rootElement = dfdlSchema.getRootElement();
- return rootElement.elementIterator(complexType);
+ return rootElement.elementIterator(COMPLEX_TYPE);
}
}