1

I have generated an UML file from a model.xml with UML2 generator.

Now I want to replace the generated ids created in xmi:id="generated id".

Below is a snippet of the generated UML code.

<packagedElement xmi:type="uml:Package" xmi:id="_lAAK0A34Eeap1Y_jd5mZDA" name="java">
  <packagedElement xmi:type="uml:Package" xmi:id="_lAAK0Q34Eeap1Y_jd5mZDA" name="lang">
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK0g34Eeap1Y_jd5mZDA" name="String"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK0w34Eeap1Y_jd5mZDA" name="Boolean"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK1A34Eeap1Y_jd5mZDA" name="Byte"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK1Q34Eeap1Y_jd5mZDA" name="Character"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK1g34Eeap1Y_jd5mZDA" name="Double"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK1w34Eeap1Y_jd5mZDA" name="Float"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK2A34Eeap1Y_jd5mZDA" name="Integer"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK2Q34Eeap1Y_jd5mZDA" name="Long"/>
    <packagedElement xmi:type="uml:DataType" xmi:id="_lAAK2g34Eeap1Y_jd5mZDA" name="Short"/>
  </packagedElement>
</packagedElement>

I have tried to use regex to remove the IDs. First I read the file and convert it to a string then I find an pattern with Matcher and try to replace it. One problem is that the gernerated ID's are similar but not the same and i don't know how to tackle this. I guess this doesn't work because it only takes the first?

Path path = profileFile.toPath();
Charset charset = StandardCharsets.UTF_8;
String replaceString = "";
String content = new String(Files.readAllBytes(path), charset);
Pattern pattern = Pattern.compile("xmi:id=\"([A-Za-z0-9_]*)\"");
Matcher matcher = pattern.matcher(content);
if (matcher.find())
{
  replaceString = matcher.group(0);
  Pattern p = Pattern.compile("\"([^\"]*)\"");
  Matcher m = p.matcher(replaceString);
  if (m.find())
  {
    System.out.println("is this the real life:    " + m.group(1));
    replaceString = m.group(1);
  }
}
// content = content.replaceAll("xmi:id=\"([A-Za-z0-9_]*)\"", "xmi:id=\"\"");
content = content.replaceAll(replaceString, "");
Files.write(path, content.getBytes(charset));

Any idea is appreciated, you can just nudge me in the right direction. My brain is a bit blurry right now.

4
  • 2
    You should include your desired output in your question. Commented May 4, 2016 at 12:52
  • 1
    Change if (matcher.find()) to while (matcher.find()) Commented May 4, 2016 at 12:56
  • 1
    Also move content = content.replaceAll(replaceString, ""); inside if (m.find()) Commented May 4, 2016 at 12:57
  • I added your changes markbernard and it works fine. Quite obvious I guess. I stared myself blind and couldn't see it. Thanks for your help. Commented May 4, 2016 at 13:47

2 Answers 2

3

I believe you can get away with a single call to replaceAll():

String input = "<packagedElement xmi:type=\"uml:DataType\" xmi:id=\"_lAAK0g34Eeap1Y_jd5mZDA\" name=\"String\"/>";
String output = input.replaceAll("xmi:id=\".*?\"", "xmi:id=\"\"");
System.out.println("Input:\n" + input);
System.out.println("Output:\n" + output);

Output:

Input:
<packagedElement xmi:type="uml:DataType" xmi:id="_lAAK0g34Eeap1Y_jd5mZDA" name="String"/>
Output:
<packagedElement xmi:type="uml:DataType" xmi:id="" name="String"/>

This assumes that you were trying to empty the xmi:id property in your XML tags, which is what your question seems to be implying.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for not adding the wanted outcome. Thanks for your answer.
0

I made the changes markbernard mentioned and it works as i wanted.

Path path = profileFile.toPath();
Charset charset = StandardCharsets.UTF_8;
String replaceString = "";
String content = new String(Files.readAllBytes(path), charset);
Pattern pattern = Pattern.compile("xmi:id=\"([A-Za-z0-9_]*)\"");
Matcher matcher = pattern.matcher(content);
while (matcher.find())
{
  replaceString = matcher.group(0);
  Pattern p = Pattern.compile("\"([^\"]*)\"");
  Matcher m = p.matcher(replaceString);
  if (m.find())
  {
    System.out.println("is this the real life:    " + m.group(1));
    replaceString = m.group(1);
    content = content.replaceAll(replaceString, "");
  }
}

Files.write(path, content.getBytes(charset));

I just had to change the outer if matcher.find() to while and move the replaceAll() inside the second m.find().

I haven't tried Tim Biegeleisen solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.