Commit c09efac0 authored by cazenave's avatar cazenave
Browse files

Merge branch 'feature/nicoback' into 'master'

Feature/nicoback

See merge request !1
parents d4296394 56606d5f
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
\ No newline at end of file
package fr.gouv.vitamui.pastis.util;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import java.io.IOException;
import java.io.Writer;
public class PastisCustomCharacterEscapeHandler implements CharacterEscapeHandler{
public PastisCustomCharacterEscapeHandler() {
super();
}
/**
* @param ch The array of characters.
* @param start The starting position.
* @param length The number of characters to use.
* @param isAttVal true if this is an attribute value literal.
*/
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
int limit = start + length;
for (int i = start; i < limit; i++) {
char c = ch[i];
if (c == '&' || c == '<' || c == '>' || (c == '\"' && isAttVal)
|| (c == '\'' && isAttVal)) {
if (i != start) {
out.write(ch, start, i - start);
}
start = i + 1;
switch (ch[i]) {
case '&':
out.write("&");
break;
case '<':
out.write("<");
break;
case '>':
out.write(">");
break;
case '\"':
out.write("\"");
break;
case '\'':
out.write("'");
break;
}
}
}
if (start != limit) {
out.write(ch, start, limit - start);
}
}
}
package fr.gouv.vitamui.pastis.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PastisGetPropertyValues {
public Properties getPropValues() {
InputStream otherpath = getClass().getClassLoader().getResourceAsStream("config.properties");
Properties pastisProperties = new Properties();
try {
pastisProperties.load(otherpath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pastisProperties;
}
public Properties getTestPropValues() {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("../classes/").getPath();
String pastisConfigFile = rootPath + "config.properties";
Properties pastisProperties = new Properties();
try {
pastisProperties.load(new FileInputStream(pastisConfigFile));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pastisProperties;
}
}
package fr.gouv.vitamui.pastis.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.fasterxml.jackson.xml.XmlMapper;
import fr.gouv.vitamui.pastis.jaxb.model.AnnotationXML;
import fr.gouv.vitamui.pastis.jaxb.model.AttributeXML;
import fr.gouv.vitamui.pastis.jaxb.model.BaliseXML;
import fr.gouv.vitamui.pastis.jaxb.model.DataXML;
import fr.gouv.vitamui.pastis.jaxb.model.DocumentationXML;
import fr.gouv.vitamui.pastis.jaxb.model.ElementXML;
import fr.gouv.vitamui.pastis.jaxb.model.OneOrMoreXML;
import fr.gouv.vitamui.pastis.jaxb.model.OptionalXML;
import fr.gouv.vitamui.pastis.jaxb.model.ValueXML;
import fr.gouv.vitamui.pastis.jaxb.model.ZeroOrMoreXML;
import fr.gouv.vitamui.pastis.model.ElementProperties;
import fr.gouv.vitamui.pastis.model.ElementRNG;
import fr.gouv.vitamui.pastis.util.PastisCustomCharacterEscapeHandler;
public class PastisGetXmlJsonTree {
//ElementRNG elementRNGRoot;
public String jsonParsed = "";
public String getJsonParsedTree(ElementRNG elementRNGRoot) {
ElementRNG.buildElementPropertiesTree(elementRNGRoot,0, null);
ElementProperties eparent = ElementRNG.elementStaticRoot;
ObjectMapper mapper = new ObjectMapper();
String jsonString = "";
try {
jsonString = mapper.writeValueAsString(eparent);
} catch (JsonGenerationException e1) {
e1.printStackTrace();
} catch (JsonMappingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return "["+jsonString+"]";
}
// Test an xml to json and a json to xml.
public String getJsonParsedTreeTest(ElementRNG elementRNGRoot) throws JAXBException, FileNotFoundException {
//vWhen the handler is called, the proprities tree (json) is build
// using its ElementRNG(elementRngRoot) object.
// The elementRngRoot is filled when the xml file is read, by passing
// it to the contentHanler of the Xml reader.
// The methods used are the 5 main methods of a DefaultHandler type
// See methods bellow
ElementRNG.buildElementPropertiesTree(elementRNGRoot,0, null);
ElementProperties eparent = ElementRNG.elementStaticRoot;
// The eparentRng is an object of type BalizeXML. It is built using the
// object eparent (of type ElementProperties) that, in fact, represent the json
// prouced during the parser's first call.
BaliseXML.buildBaliseXMLTree(eparent,0, null);
BaliseXML eparentRng = BaliseXML.baliseXMLStatic;
// Transforms java objects to Xml file (Marshalling)
JAXBContext contextObj = JAXBContext.newInstance(AttributeXML.class, ElementXML.class, DataXML.class, ValueXML.class, OptionalXML.class, OneOrMoreXML.class,
ZeroOrMoreXML.class, AnnotationXML.class, DocumentationXML.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallerObj.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler",
new PastisCustomCharacterEscapeHandler());
marshallerObj.marshal(eparentRng, new FileOutputStream("generated_test.xml"));
ObjectMapper mapper = new ObjectMapper();
String jsonString = "";
try {
jsonString = mapper.writeValueAsString(eparent);
} catch (JsonGenerationException e1) {
e1.printStackTrace();
} catch (JsonMappingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return "["+jsonString+"]";
}
public String getXmlParsedTree(String jsonString) throws JsonProcessingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectMapper xmlMapper = new XmlMapper();
JsonNode tree = objectMapper.readTree(jsonString);
String jsonAsXml = xmlMapper.writeValueAsString(tree);
return jsonAsXml;
}
public void setJsonParsed(String jsonParsed) {
this.jsonParsed = jsonParsed;
}
}
/*
* Copyright CINES Centre Informatique National de l'Enseignement Supérieur, 2017
* Tout droit d'utilisation strictement soumis à l'approbation du CINES
*/
package fr.gouv.vitamui.pastis.util;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import fr.gouv.vitamui.pastis.model.ElementRNG;
/**
* @author rachid Sala <rachid@cines.fr>
*/
public class PastisSAX2Handler extends DefaultHandler{
private static Logger LOGGER = LoggerFactory.getLogger(PastisSAX2Handler.class);
public String jsonParsed = "";
public int oldSize = 0;
boolean isValue = false;
private boolean isInDocumentationTag = false;
public ElementRNG elementRNGRoot;
Stack<ElementRNG> stackRNG = new Stack<ElementRNG>();
private StringBuilder documentationContent;
/* BEGIN OF OVERRIDE OF SAX 5 METHODS : startElement, endElement, startDocument.
* endDocument and characters **/
/**
*
*
* Actions à réaliser lors de la détection d'un nouvel él System.out.println(elementRNG.getType() + ".." + localName);
ément.
* This method is called everytime the parser gets an open tag
* Identifies which tag has being opened at time by assiging a new flag
*/
public void startElement(String nameSpace, String localName, String qName, Attributes attr) throws SAXException {
System.out.println("---------------------------------------------");
//cette variable contient le nom du nœud qui a créé l'événement
System.out.println("qname = " + qName + " " + attr.getValue("name"));
// If node not a grammar tag or start tag
if(!("grammar".equals(localName) || "start".equals(localName))){
// If node is ArchiveTransfer
if(null !=attr.getValue("name") && attr.getValue("name").equals("ArchiveTransfer")){
return ;
}
//If node has documentation
if("value".equals(localName) || "documentation".equals(localName)) {
this.isValue = true;
}
// Create a new rng tag element and add it to the stack
ElementRNG elementRNG = new ElementRNG();
elementRNG.setName(attr.getValue("name"));
elementRNG.setType(localName);
elementRNG.setDataType(attr.getValue("type"));
if(!stackRNG.empty()) {
ElementRNG e = stackRNG.lastElement();
elementRNG.setParent(e);
e.getChildren().add(elementRNG);
}
//LOGGER.warn(" START elementRNG " + elementRNG.getType() + elementRNG.getName());
stackRNG.push(elementRNG);
}
documentationContent = new StringBuilder();
//System.out.println("Start element :" + stringBuilder);
if (qName.equalsIgnoreCase("xsd:documentation")) {
isInDocumentationTag = true;
}
}
/**
* Actions à réaliser lors de la détection de la fin d'un élément.
*/
public void endElement(String nameSpace, String localName, String qName) throws SAXException {
//System.out.println("End element :" + qName);
if (qName.equalsIgnoreCase("xsd:documentation")) {
isInDocumentationTag = false;
}
if(!stackRNG.empty()) {
ElementRNG e = stackRNG.pop();
// LOGGER.warn("END localName " + localName + " e.getType() " + e.getType());
}
}
/**
* Actions à réaliser au début du document.
*/
public void startDocument() {
elementRNGRoot = new ElementRNG();
elementRNGRoot.setName("ArchiveTransfer");
elementRNGRoot.setType("element");
stackRNG.push(elementRNGRoot);
// LOGGER.warn("startDocument");
}
/**
* Actions à réaliser lors de la fin du document XML.
*/
public void endDocument() {
// LOGGER.warn("endDocument");
}
/**
* Actions to perform when tag content is reached (Data between '< />' )
*/
@Override
public void characters(char[] caracteres, int start, int length) throws SAXException {
if (isInDocumentationTag) {
documentationContent.append(new String(caracteres, start, length));
stackRNG.lastElement().setValue(documentationContent.toString());
}
if(isValue) {
String valueContent = new String(caracteres, start, length);
stackRNG.lastElement().setValue(valueContent);
this.isValue = false;
}
}
}
/*
* Copyright CINES Centre Informatique National de l'Enseignement Supérieur, 2017
* Tout droit d'utilisation strictement soumis à l'approbation du CINES
*/
package fr.gouv.vitamui.pastis.util;
import java.util.HashMap;
import java.util.Map;
/**
* @author rachid Sala <rachid@cines.fr>
*/
public class RNGConstants {
public static enum DataType
{
anyURI("anyURI"),
token("token"),
string("string"),
dateTime("dateTime"),
ID("ID");
private String label;
private DataType(final String value) {
setLabel(value);
}
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
}
public static enum Type
{
element ("element"),
attribute("attribute"),
data("data"),
except("except"),
nsName("nsName"),
value("value"),
text("text"),
ID("ID");
private String label;
private Type(final String value) {
setLabel(value);
}
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
}
public static final Map<String , String> CardinalityMap = new HashMap<String , String>() {
public static final long serialVersionUID = 1L;
{
put("optional", "0-1");
put("zeroOrMore", "0-N");
put("obligatoire", "1");
put("oneOrMore", "1-N");
}};
public static enum Cardinality
{
optional("0-1"),
zeroOrMore("0-N"),
obligatoire("1"),
oneOrMore("1-N");
private String label;
private Cardinality(final String value) {
setLabel(value);
}
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
}
public static final Map<String , String> GroupOrChoiceMap = new HashMap<String , String>() {
public static final long serialVersionUID = 1L;
{
put("group","group");
put("choice","choice");
}};
public static enum GroupOrChoice
{
group("group"),
choice("choice");
private String label;
private GroupOrChoice(final String value) {
setLabel(value);
}
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
}
public static boolean isElement (String type) {
for(Type typeElement : RNGConstants.Type.values()){
if (typeElement.toString().equals(type)) {
return true;
}
}
return false;
}
public static boolean isCardinality (String type) {
for(Cardinality typeElement : RNGConstants.Cardinality.values()){
if (typeElement.toString().equals(type)) {
return true;
}
}
return false;
}
public static boolean hasGroupOrChoice(String type) {
for(GroupOrChoice typeElement : RNGConstants.GroupOrChoice.values()){
if (typeElement.toString().equals(type)) {
return true;
}
}
return false;
}
public static boolean isDataType (String type) {
for(DataType typeElement : RNGConstants.DataType.values()){
if (typeElement.toString().equals(type)) {
return true;
}
}
return false;
}
public static boolean isValueOrData (String type) {
if (null != type && (RNGConstants.Type.data.toString().equals(type)
|| RNGConstants.Type.nsName.toString().equals(type)
|| RNGConstants.Type.value.toString().equals(type))){
return true;
}
return false;
}
}
//package fr.gouv.vitamui.pastis.util;
//
//import java.io.IOException;
//import java.util.List;
//
//import com.fasterxml.jackson.core.JsonGenerationException;
//import com.fasterxml.jackson.core.JsonGenerator;
//import com.fasterxml.jackson.core.JsonParser;
//import com.fasterxml.jackson.core.JsonStreamContext;
//import com.fasterxml.jackson.databind.SerializerProvider;
//import com.fasterxml.jackson.databind.ser.std.StdSerializer;
//
//import fr.cines.pastis.rng.model.Attribute;
//import fr.cines.pastis.rng.model.Element;
//import fr.cines.pastis.rng.model.Grammar;
//import fr.cines.pastis.rng.model.NsName;
//import fr.cines.pastis.rng.model.Start;
//
//public class RngSerialize extends StdSerializer<Grammar> {
//
// private Element rootElement;
// private List<Element> children;
// private Start startElement;
// private RNGConstants rngConstants;
// private JsonParser jsonParser;
//
//
//
// public RngSerialize(Class<Grammar> t) {
// super(t);
// }
//
// @Override
// public void serialize(Grammar grammar, JsonGenerator jsonGen, SerializerProvider provider)
// throws IOException, JsonGenerationException {
// startElement = grammar.getStart();
// rootElement = startElement.getElement();
// buildJsonTree(rootElement, jsonGen);
//
//
// }
//