Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80c130f

Browse files
committed
[Java] Tidy up handling of reporting warnings on non-standard header and replace use of String.format.
1 parent 224bce3 commit 80c130f

File tree

5 files changed

+54
-55
lines changed

5 files changed

+54
-55
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/CompositeType.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ else if (!isUnsigned(numInGroupType.primitiveType()))
195195
}
196196
else if (numInGroupMinValue.longValue() < 0)
197197
{
198-
XmlSchemaParser.handleError(node, String.format(
199-
"\"numInGroup\" minValue=%s must be greater than zero " +
200-
"for signed \"numInGroup\" types", numInGroupMinValue));
198+
XmlSchemaParser.handleError(node,
199+
"\"numInGroup\" minValue=" + numInGroupMinValue + " must be greater than zero " +
200+
"for signed \"numInGroup\" types");
201201
}
202202
}
203203
else
@@ -218,8 +218,8 @@ else if (numInGroupMinValue.longValue() < 0)
218218

219219
if (numInGroupMinValue.longValue() > max)
220220
{
221-
XmlSchemaParser.handleError(node, String.format(
222-
"\"numInGroup\" minValue=%s greater than maxValue=%d", numInGroupMinValue, max));
221+
XmlSchemaParser.handleError(
222+
node, "\"numInGroup\" minValue=" + numInGroupMinValue + " greater than maxValue=" + max);
223223
}
224224
}
225225
}
@@ -275,22 +275,22 @@ private static void validateGroupMaxValue(
275275
final long allowedValue = primitiveType.maxValue().longValue();
276276
if (longValue > allowedValue)
277277
{
278-
XmlSchemaParser.handleError(node, String.format(
279-
"maxValue greater than allowed for type: maxValue=%d allowed=%d", longValue, allowedValue));
278+
XmlSchemaParser.handleError(
279+
node, "maxValue greater than allowed for type: maxValue=" + longValue + " allowed=" + allowedValue);
280280
}
281281

282282
final long maxInt = INT32.maxValue().longValue();
283283
if (primitiveType == UINT32 && longValue > maxInt)
284284
{
285-
XmlSchemaParser.handleError(node, String.format(
286-
"maxValue greater than allowed for type: maxValue=%d allowed=%d", longValue, maxInt));
285+
XmlSchemaParser.handleError(
286+
node, "maxValue greater than allowed for type: maxValue=" + longValue + " allowed=" + maxInt);
287287
}
288288
}
289289
else if (primitiveType == UINT32)
290290
{
291291
final long maxInt = INT32.maxValue().longValue();
292-
XmlSchemaParser.handleError(node, String.format(
293-
"maxValue must be set for varData UINT32 type: max value allowed=%d", maxInt));
292+
XmlSchemaParser.handleError(
293+
node, "maxValue must be set for varData UINT32 type: max value allowed=" + maxInt);
294294
}
295295
}
296296

@@ -333,33 +333,25 @@ private void validateHeaderField(
333333
{
334334
if (actualType == null)
335335
{
336-
XmlSchemaParser.handleError(
337-
node,
338-
String.format("composite for message header must have \"%s\"", fieldName));
336+
XmlSchemaParser.handleError(node, "composite for message header must have \"" + fieldName + "\"");
339337
}
340338
else if (actualType.primitiveType() != expectedType)
341339
{
342-
XmlSchemaParser.handleWarning(node, String.format("\"%s\" should be %s", fieldName, expectedType.name()));
340+
XmlSchemaParser.handleWarning(node, "\"" + fieldName + "\" should be " + expectedType.name());
343341

344342
if (shouldGenerateInterfaces)
345343
{
346344
if (actualType.primitiveType().size() > expectedType.size())
347345
{
348-
XmlSchemaParser.handleError(
349-
node,
350-
String.format("\"%s\" must be less than %s bytes to use %s",
351-
fieldName,
352-
expectedType.size(),
353-
JAVA_GENERATE_INTERFACES));
346+
final String msg = "\"" + fieldName + "\" must be less than " + expectedType.size() +
347+
" bytes to use " + JAVA_GENERATE_INTERFACES;
348+
XmlSchemaParser.handleError(node, msg);
354349
}
355350
else
356351
{
357-
XmlSchemaParser.handleWarning(
358-
node,
359-
String.format("\"%s\" will be cast to %s to use %s",
360-
fieldName,
361-
expectedType.name(),
362-
JAVA_GENERATE_INTERFACES));
352+
final String msg = "\"" + fieldName + "\" will be cast to " + expectedType.name() +
353+
" to use " + JAVA_GENERATE_INTERFACES;
354+
XmlSchemaParser.handleWarning(node, msg);
363355
}
364356
}
365357
}
@@ -383,8 +375,7 @@ public void checkForValidOffsets(final Node node)
383375
if (offsetAttribute < offset)
384376
{
385377
XmlSchemaParser.handleError(
386-
node,
387-
String.format("composite element \"%s\" has incorrect offset specified", edt.name()));
378+
node, "composite element \"" + edt.name() + "\" has incorrect offset specified");
388379
}
389380

390381
offset = offsetAttribute;
@@ -434,8 +425,8 @@ private Type processType(
434425
{
435426
final XPath xPath = XPathFactory.newInstance().newXPath();
436427
final String refTypeName = XmlSchemaParser.getAttributeValue(subTypeNode, "type");
437-
final Node refTypeNode = (Node)xPath.compile(
438-
"/*[local-name() = 'messageSchema']/types/*[@name='" + refTypeName + "']")
428+
final String expression = "/*[local-name() = 'messageSchema']/types/*[@name='" + refTypeName + "']";
429+
final Node refTypeNode = (Node)xPath.compile(expression)
439430
.evaluate(subTypeNode.getOwnerDocument(), XPathConstants.NODE);
440431

441432
if (refTypeNode == null)

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/MessageSchema.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public class MessageSchema
5252
private final Map<String, Type> typeByNameMap;
5353
private final Map<Long, Message> messageByIdMap;
5454

55-
MessageSchema(
56-
final Node schemaNode, final Map<String, Type> typeByNameMap, final Map<Long, Message> messageByIdMap)
55+
MessageSchema(final Node schemaNode, final Map<String, Type> typeByNameMap, final Map<Long, Message> messageByIdMap)
5756
{
5857
this.packageName = getAttributeValue(schemaNode, "package");
5958
this.description = getAttributeValueOrNull(schemaNode, "description");
@@ -371,9 +370,9 @@ private static Node findNode(final Node contextNode, final String path)
371370
return (Node)XPathFactory.newInstance().newXPath()
372371
.evaluate(path, contextNode, XPathConstants.NODE);
373372
}
374-
catch (final XPathExpressionException e)
373+
catch (final XPathExpressionException ex)
375374
{
376-
throw new IllegalArgumentException("Unable to locate node with path=" + path, e);
375+
throw new IllegalArgumentException("Unable to locate node with path=" + path, ex);
377376
}
378377
}
379378
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/XmlSchemaParser.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ public static MessageSchema parse(final InputStream in, final ParserOptions opti
176176
* @param document for the XML parsing.
177177
* @param xPath for XPath expression reuse.
178178
* @return {@link java.util.Map} of name {@link java.lang.String} to {@link Type}.
179-
* @throws Exception on parsing error.
179+
* @throws XPathExpressionException on parsing error.
180180
*/
181-
public static Map<String, Type> findTypes(final Document document, final XPath xPath) throws Exception
181+
public static Map<String, Type> findTypes(final Document document, final XPath xPath)
182+
throws XPathExpressionException
182183
{
183184
final Map<String, Type> typeByNameMap = new HashMap<>();
184185

@@ -216,10 +217,11 @@ public static Map<String, Type> findTypes(final Document document, final XPath x
216217
* @param xPath for XPath expression reuse.
217218
* @param typeByNameMap to use for Type objects.
218219
* @return {@link java.util.Map} of schemaId to {@link Message}.
219-
* @throws Exception on parsing error.
220+
* @throws XPathExpressionException on parsing error.
220221
*/
221222
public static Map<Long, Message> findMessages(
222-
final Document document, final XPath xPath, final Map<String, Type> typeByNameMap) throws Exception
223+
final Document document, final XPath xPath, final Map<String, Type> typeByNameMap)
224+
throws XPathExpressionException
223225
{
224226
final Map<Long, Message> messageByIdMap = new HashMap<>();
225227
final ObjectHashSet<String> distinctNames = new ObjectHashSet<>();

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/WarningFormattingTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,31 @@
2424
import java.io.PrintStream;
2525
import java.nio.charset.StandardCharsets;
2626

27-
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.hamcrest.Matchers.containsString;
2829
import static uk.co.real_logic.sbe.Tests.getLocalResource;
2930
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;
3031

31-
public class WarningFormattingTest
32+
class WarningFormattingTest
3233
{
3334
@Test
3435
void shouldGenerateCorrectWarning() throws Exception
3536
{
3637
final ByteArrayOutputStream out = new ByteArrayOutputStream();
37-
final ParserOptions options = new ParserOptions.Builder().errorPrintStream(new PrintStream(out)).build();
38+
final ParserOptions options = new ParserOptions.Builder()
39+
.stopOnError(true)
40+
.errorPrintStream(new PrintStream(out))
41+
.build();
3842
final MessageSchema schema = parse(getLocalResource("npe-small-header.xml"), options);
3943
final IrGenerator irg = new IrGenerator();
4044
irg.generate(schema);
4145

4246
final String warnings = out.toString(StandardCharsets.US_ASCII.name());
43-
assertTrue(warnings.contains(
47+
assertThat(warnings, containsString(
4448
"WARNING: at <types><composite name=\"messageHeader\"> \"blockLength\" should be UINT16"));
45-
assertTrue(warnings.contains(
49+
assertThat(warnings, containsString(
4650
"WARNING: at <types><composite name=\"messageHeader\"> \"templateId\" should be UINT16"));
47-
assertTrue(warnings.contains(
51+
assertThat(warnings, containsString(
4852
"WARNING: at <types><composite name=\"messageHeader\"> \"version\" should be UINT16"));
4953
}
5054
}

sbe-tool/src/test/resources/npe-small-header.xml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
description="Message format for XXXXXX"
88
byteOrder="littleEndian">
99
<types>
10-
<!-- SBE standard-ish MessageHeader:
11-
We think we can use uint8 for most of these fields.
12-
Part of the argument is to acknowledge XXXXXXXX does use complex/large msgs, but they are/should-be config/protobuffs, not SBE -->
13-
<composite name="messageHeader" description="Header included at the start of every SBE message">
14-
<type name="schemaId" primitiveType="uint16" description="ID for the schema"/>
15-
<type name="version" primitiveType="uint8" description="Version number of the schema"/>
16-
<type name="templateId" primitiveType="uint8" description="ID of the msg type. 0-127 = outbound (inc Ping, etc), 128-255 = inbound(Pong, etc)"/>
17-
<type name="blockLength" primitiveType="uint8" description="Length of fixed-size root-block part, excludes this header, repeating-groups and VarData"/>
18-
<type name="numGroups" primitiveType="uint8" description="Number of repeating-groups"/>
19-
<type name="numVarDataFields" primitiveType="uint8" description="Number of variable-length fields"/>
10+
<composite name="messageHeader" description="Header included before every SBE message">
11+
<type name="schemaId" primitiveType="uint16"
12+
description="ID for the schema"/>
13+
<type name="version" primitiveType="uint8"
14+
description="Version number of the schema"/>
15+
<type name="templateId" primitiveType="uint8"
16+
description="ID of the msg type."/>
17+
<type name="blockLength" primitiveType="uint8"
18+
description="Length of fixed-size root-block, excludes this header, repeating-groups and var-data"/>
19+
<type name="numGroups" primitiveType="uint8"
20+
description="Number of repeating-groups"/>
21+
<type name="numVarDataFields" primitiveType="uint8"
22+
description="Number of variable-length fields"/>
2023
</composite>
2124
</types>
2225
<sbe:message name="Ping" id="0"> </sbe:message>

0 commit comments

Comments
 (0)