Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar generator from XML #779

Merged
merged 1 commit into from
Jun 20, 2020

Conversation

angelozerr
Copy link
Contributor

Grammar generator from XML

Fixes #778

Signed-off-by: azerr azerr@redhat.com

@angelozerr
Copy link
Contributor Author

angelozerr commented Jun 11, 2020

@datho7561 @xorye @fbricon I created a draft PR with the generator (XML -> XSD, XML -> DTD).

The generation is not perfect because it misses some features (like managing maxOccurs, etc), but as it's a big PR, you can start to review the code, the API and comments.

To see what the generator can do it, I suggest you that you read tests for generating XSD and DTD). I will continue to improve the generation of those grammar, but any review feedback are welcome!

@angelozerr angelozerr force-pushed the grammar-generator2 branch 5 times, most recently from d47cab5 to 39ef689 Compare June 16, 2020 16:19
if (attributes != null) {
for (int j = 0; j < attributes.getLength(); j++) {
Attr attr = (Attr) attributes.item(i);
elementDecl.getAttribute(attr.getName());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could have a method ElementDeclaration#addAttribute instead? Just an idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to improve attributes cardinality, so please keep like this to get the attribute declaration.

@angelozerr angelozerr force-pushed the grammar-generator2 branch 4 times, most recently from 3c62883 to c575f16 Compare June 17, 2020 14:33
@angelozerr angelozerr marked this pull request as ready for review June 17, 2020 15:37
@angelozerr angelozerr force-pushed the grammar-generator2 branch 4 times, most recently from cb02740 to e56fc1e Compare June 18, 2020 14:37
@xorye
Copy link

xorye commented Jun 18, 2020

The PR looks good

@angelozerr angelozerr force-pushed the grammar-generator2 branch 2 times, most recently from 84022e1 to 73715a0 Compare June 19, 2020 13:14
@angelozerr
Copy link
Contributor Author

I have improved this PR to consume teh generation in code action for generating xsd and dtd.

Here a demo with noNamespaceSchemaLocation:

XML2XSDDemo

Here a demo with DTD

XML2DTDDemo

@angelozerr angelozerr force-pushed the grammar-generator2 branch 3 times, most recently from 161d3f0 to 3c0f4d4 Compare June 19, 2020 14:08
@xorye
Copy link

xorye commented Jun 19, 2020

For this example:

<!DOCTYPE root PUBLIC "public-id" "file.dtd">
<root>
    <a>
        <b>hello</b>
        text
    </a>
</root>

after the code action, I have validation errors on the dtd file and xml file:

dtd:
image

xml:
image

@datho7561
Copy link
Contributor

I opened issue #788 about multiplicity. @angelozerr, do you think that it makes sense to try to fix this now or in a future PR?

@angelozerr
Copy link
Contributor Author

For this example:

<!DOCTYPE root PUBLIC "public-id" "file.dtd">
<root>
    <a>
        <b>hello</b>
        text
    </a>
</root>

after the code action, I have validation errors on the dtd file and xml file:

Good catch, fixed!

@angelozerr
Copy link
Contributor Author

I opened issue #788 about multiplicity. @angelozerr, do you think that it makes sense to try to fix this now or in a future PR?

Fixed!

@datho7561
Copy link
Contributor

Should we also worry about something like this?

<root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="zero-one-schema.xsd">
    <a>
        <b />
    </a>
    <a></a>
</root>

Generates:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="a" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="b" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Which makes the second occurrence of <a> invalid.

@angelozerr angelozerr force-pushed the grammar-generator2 branch 2 times, most recently from 720d008 to 71845ed Compare June 19, 2020 17:41
Fixes eclipse-lemminx#778

Signed-off-by: azerr <azerr@redhat.com>
@angelozerr
Copy link
Contributor Author

Should we also worry about something like this?

Yes, it was a bug which should be fixed.

The

<root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="zero-one-schema.xsd">
    <a>
        <b />
    </a>
    <a></a>
</root>

generates now <xs:sequence minOccurs="0"> like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="a" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence minOccurs="0">
              <xs:element name="b" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grammar generator from XML
4 participants