cim1.parser.CIMParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powsybl-cim1-model Show documentation
Show all versions of powsybl-cim1-model Show documentation
CIM ENTSO-E V1 model generated by CIM Gateway tool
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package cim1.parser;
import cim1.exc.InterpretationException;
import cim1.exc.BadVersionException;
import cim1.exc.BadSubsetException;
import cim1.exc.LinkageException;
import cim1.model.CIMModel;
import cim1.CIMURI;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* This file has been automatically generated by CIMGateway
*
* This class is the interface between the data read in the XML document and
* the utilities to call in CIMModel and the associated instances.
*/
public class CIMParser {
private static final Logger LOGGER = LoggerFactory.getLogger(CIMParser.class);
private CIMModel model;
private String name;
/**
* Constructor of the CIMParser
*
* @param name only used to spot the input in log Messages
* This name is used later to determine
* the subset described
* @param model
*/
public CIMParser(final String name, CIMModel model) {
this.name = name;
this.model = model;
}
/**
* Utility to use when we meet a startElement in the CIM XML Document
* Concerning the CIMModel, it can interpret the startElement as the
* definition of a class (rdf:ID) or of an attribute referenced elsewhere
* (rdf:resource or rdf:about).
*
* @param xmlsr The XMLStreamReader that is reading the XMLDocument
*/
public void startElement(final XMLStreamReader xmlsr) throws InterpretationException, BadVersionException {
if (checkCIM(xmlsr.getNamespaceURI()) && !xmlsr.getLocalName().contains(".")) {
final String id_xml = xmlsr.getAttributeValue(CIMModel.rdfURI, "ID");
if (id_xml != null) {
LOGGER.debug("That's a class !");
if (id_xml.isEmpty()) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber()
+ ": The CIM element \"" + xmlsr.getLocalName()
+ "\" has an empty \"rdf:ID\" ! The tag is ignored.");
}
try {
model.readClass(xmlsr.getLocalName(), id_xml);
} catch (final InterpretationException toCatch) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber()
+ ": " + toCatch.getMessage() + " The tag is ignored.");
}
} else {
final String about_xml = xmlsr.getAttributeValue(CIMModel.rdfURI, "about");
if (about_xml != null) {
// That's a reference to a class described elsewhere !
if (about_xml.length() < 2) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber()
+ ": The CIM element \"" + xmlsr.getLocalName()
+ "\" has an invalid \"rdf:about\" ! The tag is ignored.");
}
try {
model.readAbout(xmlsr.getLocalName(),
about_xml.substring(1, about_xml.length()));
} catch (final InterpretationException toCatch) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber() +
": " + toCatch.getMessage() + " The tag is ignored.");
}
} else {
LOGGER.warn("Line " + xmlsr.getLocation().getLineNumber()
+ ": The CIM element \"" + xmlsr.getLocalName() + "\" has no \"rdf:ID\" "
+ "or \"rdf:about\" attribute ! The tag is ignored.");
}
}
} else if (checkCIM(xmlsr.getNamespaceURI()) && xmlsr.getLocalName().contains(".")) {
// That's an attribute of an UML class !
if (xmlsr.getAttributeCount() != 0) {
final String id_xml = xmlsr.getAttributeValue(CIMModel.rdfURI, "resource");
if (id_xml != null) {
if (id_xml.length() < 2) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber()
+ ": The CIM element \"" + xmlsr.getLocalName()
+ "\" has an invalid \"rdf:resource\" ! The tag is ignored.");
}
int pos = xmlsr.getLocalName().indexOf(".");
String mother = xmlsr.getLocalName().substring(0, pos++);
String attribute = xmlsr.getLocalName().substring(pos);
try {
model.readResource(mother, attribute, id_xml);
} catch (final InterpretationException toCatch) {
LOGGER.warn("Line " + xmlsr.getLocation().getLineNumber() + ": "
+ toCatch.getMessage() + " The tag is ignored.");
} catch (final LinkageException toCatch) {
LOGGER.warn("Line " + xmlsr.getLocation().getLineNumber() + ": "
+ toCatch.getMessage() + " The tag is ignored.");
}
} else {
// id_xml null, The "rdf:resource" cannot be found, and no
// other attribute should have been found
LOGGER.warn("Line " + xmlsr.getLocation().getLineNumber()
+ ": The existing tag attribute is not "
+ "valid: \"rdf:resource\" could not be found. The tag is ignored.");
}
} else {
// The XML element doesn't have XML attribute but is an UML
// attribute
// Then the next Event is XML.Characters
if (checkCIM(xmlsr.getNamespaceURI())) {
try {
int pos = xmlsr.getLocalName().indexOf(".");
if (pos != -1) {
String mother = xmlsr.getLocalName().substring(0, pos++);
String attribute = xmlsr.getLocalName().substring(pos);
try {
xmlsr.next();
} catch (XMLStreamException e) {
throw new InterpretationException("XMLException message is: "
+ e.getMessage());
}
if (xmlsr.isEndElement()) {
model.readAttribute(mother, attribute, "");
} else {
model.readAttribute(mother, attribute,
xmlsr.getText());
}
}
} catch (InterpretationException e) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber() +
": " + e.getMessage() + " The tag is ignored.");
}
} else {
// Wrong CIM namespace
if (xmlsr.getNamespaceContext().equals(xmlsr.getNamespaceURI("cim"))) {
throw new BadVersionException("Line " + xmlsr.getLocation().getLineNumber()
+ ": " + ": Wrong CIM namespace \"" + xmlsr.getNamespaceURI()
+ "\": it should have been " + CIMURI.CIMURI);
}
LOGGER.debug("Line " + xmlsr.getLocation().getLineNumber()
+ ": Wrong namespace \"" + xmlsr.getNamespaceURI()
+ "\". The tag is ignored.");
}
}
} else {
if (!checkCIM(xmlsr.getNamespaceURI())) {
// Wrong CIM namespace
if (xmlsr.getNamespaceContext().equals(xmlsr.getNamespaceURI("cim"))) {
throw new BadVersionException("Line " + xmlsr.getLocation().getLineNumber()
+ ": " + "Wrong CIM namespace \"" + xmlsr.getNamespaceURI()
+ "\": it should have been " + CIMURI.CIMURI);
} else if (xmlsr.getLocalName().equals("RDF")) {
LOGGER.error("Line " + xmlsr.getLocation().getLineNumber() + ": "
+ "The Local Name RDF" + "should not be found here ");
}
LOGGER.debug("Line " + xmlsr.getLocation().getLineNumber()
+ ": Wrong namespace \"" + xmlsr.getNamespaceURI()
+ "\". The tag is ignored.");
}
}
}
public void endElement(final XMLStreamReader xmlsr) throws InterpretationException, BadVersionException {
if (checkCIM(xmlsr.getNamespaceURI()) && !xmlsr.getLocalName().contains(".")) {
model.currentIO = null;
}
}
/**
* Utility only useful for the logger, to use when we meet the end of the
* CIM XML document
*/
public void endDocument() {
LOGGER.info("... end parsing \"" + name + "\"");
}
/**
* Utility meant to be used when we start reading a document
* It initializes the CIMModel. In particular, it calls an utility which
* will load the current subset described by the document.
*
* @throws BadSubsetException in the case where we try to load subsets in
* the wrong order.
*/
public void startDocument() throws BadSubsetException {
LOGGER.info("Starting to parse \"" + name + "\"...");
model.start(name);
}
/**
* Utility used to check if the right CIMURI is used in the CIM XML Document
*
* @param toCheck
* The CIMURI read in the document
* @return a boolean which indicates if the CIMURI read is right or not
* if true : the CIMURI read is the CIMURI expected
* if false : the CIMURI read is different from the CIMURI expected
*/
protected boolean checkCIM(final String toCheck) {
return (CIMURI.CIMURI.equals(toCheck));
}
}