Skip to content

Commit

Permalink
#316 xsd works
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 6, 2021
1 parent 6b1576c commit ba29b68
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 108 deletions.
25 changes: 25 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/Syntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XSDDocument;
import java.io.IOException;
import java.util.Collection;
import javax.xml.transform.dom.DOMSource;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
Expand All @@ -36,11 +39,13 @@
import org.cactoos.Input;
import org.cactoos.Output;
import org.cactoos.io.InputOf;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.TeeInput;
import org.cactoos.io.UncheckedInput;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.TextOf;
import org.xml.sax.SAXParseException;

/**
* Syntax parser, from plain text to XML using ANTLR4.
Expand Down Expand Up @@ -118,6 +123,26 @@ public void syntaxError(final Recognizer<?, ?> recognizer,
final XeListener xel = new XeListener(this.name);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = xel.xml();
final Collection<SAXParseException> violations = new XSDDocument(
new TextOf(new ResourceOf("XMIR.xsd")).asString()
).validate(new DOMSource(dom.node()));
if (!violations.isEmpty()) {
Logger.error(this, "XML with XSD failures:%n%s", dom);
for (final SAXParseException violation : violations) {
Logger.error(
this, "XSD failure at #%d:%d %s",
violation.getLineNumber(),
violation.getColumnNumber(),
violation.getLocalizedMessage()
);
}
throw new IllegalStateException(
String.format(
"There are %d XSD violation(s), see the log",
violations.size()
)
);
}
Logger.debug(this, "Raw XML:\n%s", dom.toString());
new Unchecked<>(
new LengthOf(
Expand Down
94 changes: 94 additions & 0 deletions eo-parser/src/main/resources/XMIR.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2016-2021 Yegor Bugayenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="SNAPSHOT" elementFormDefault="qualified">
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="o" mixed="true">
<xs:sequence>
<xs:element name="o" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="line" type="xs:decimal"/>
<xs:attribute name="name"/>
<xs:attribute name="base"/>
<xs:attribute name="data"/>
<xs:attribute name="as"/>
<xs:attribute name="vararg" type="empty"/>
<xs:attribute name="method" type="empty"/>
<xs:attribute name="atom"/>
<xs:attribute name="const" type="empty"/>
<xs:attribute name="flags"/>
</xs:complexType>
<xs:complexType name="program">
<xs:sequence>
<xs:element name="listing" type="xs:string"/>
<xs:element name="errors">
<xs:complexType>
<xs:sequence>
<xs:element name="error" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sheets">
<xs:complexType>
<xs:sequence>
<xs:element name="sheet" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="license" type="xs:string" minOccurs="0"/>
<xs:element name="metas" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="meta" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="head" type="xs:string"/>
<xs:element name="tail" type="xs:string"/>
<xs:element name="part" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="line" type="xs:decimal"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects">
<xs:complexType>
<xs:sequence>
<xs:element name="o" type="o" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="ms" type="xs:string"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="time" type="xs:string"/>
<xs:attribute name="version" type="xs:string"/>
</xs:complexType>
<xs:element name="program" type="program"/>
</xs:schema>
108 changes: 0 additions & 108 deletions eo-parser/src/main/resources/eo-object.xsd

This file was deleted.

0 comments on commit ba29b68

Please sign in to comment.