Skip to content

Commit

Permalink
Allow both attributes and element content to be (seq of) reals.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbattle committed May 9, 2024
1 parent 52ee116 commit 7a3f42a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 52 deletions.
2 changes: 1 addition & 1 deletion xsd2vdm/src/main/java/xsd2vdm/XSDContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public String toString()
protected String toVDM(String indent)
{
String quoted = content.replaceAll("\\\"", "\\\\\"").replaceAll("\\n", "\\\\\\n");
return indent + "\"" + quoted + "\"";
return indent + valueToVDM(quoted);
}
}
110 changes: 59 additions & 51 deletions xsd2vdm/src/main/java/xsd2vdm/XSDElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,57 +291,8 @@ protected String toVDM(String indent)
sb.append("mk_Attribute(\"");
sb.append(pair.getKey());
sb.append("\", ");

String value = pair.getValue();

try
{
if (value.matches("^([+-.0123456789eE]+\\s*)+$"))
{
List<String> nums = new Vector<String>();

Pattern p = Pattern.compile("[+-.0123456789eE]+");
Matcher m = p.matcher(value);

while (m.find())
{
nums.add(m.group());
}

if (nums.size() == 1)
{
sb.append(nums.get(0));
}
else
{
String comma = "";
sb.append("[");

for (String num: nums)
{
sb.append(comma);
sb.append(num);
comma = ", ";
}

sb.append("]");
}

sb.append(")");
}
else
{
double num = Double.parseDouble(value);
sb.append(num);
sb.append(")");
}
}
catch (NumberFormatException e)
{
sb.append("\"");
sb.append(value);
sb.append("\")");
}
sb.append(valueToVDM(pair.getValue()));
sb.append(")");

sep = ",\n";
}
Expand Down Expand Up @@ -381,6 +332,63 @@ protected String toVDM(String indent)
return sb.toString();
}

/**
* Most XML content string are just turned into VDM "quoted strings". But if the content
* parses as a number or sequence of numbers, it is turned into -1.23 or [1, 2, 3] etc.
*/
protected String valueToVDM(String value)
{
StringBuilder sb = new StringBuilder();

try
{
if (value.matches("^([+-.0123456789eE]+\\s*)+$"))
{
List<String> nums = new Vector<String>();

Pattern p = Pattern.compile("[+-.0123456789eE]+");
Matcher m = p.matcher(value);

while (m.find())
{
nums.add(m.group());
}

if (nums.size() == 1)
{
sb.append(nums.get(0));
}
else
{
String comma = "";
sb.append("[");

for (String num: nums)
{
sb.append(comma);
sb.append(num);
comma = ", ";
}

sb.append("]");
}
}
else
{
double num = Double.parseDouble(value);
sb.append(num);
}
}
catch (NumberFormatException e)
{
sb.append("\"");
sb.append(value);
sb.append("\"");
}

return sb.toString();
}

@Override
public boolean equals(Object other)
{
Expand Down

0 comments on commit 7a3f42a

Please sign in to comment.