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
/*
* 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.controller;
import fr.gouv.vitamui.pastis.jaxb.model.*;
import fr.gouv.vitamui.pastis.model.ElementProperties;
import fr.gouv.vitamui.pastis.util.PastisCustomCharacterEscapeHandler;
import fr.gouv.vitamui.pastis.util.PastisGetPropertyValues;
import fr.gouv.vitamui.pastis.util.PastisGetXmlJsonTree;
import fr.gouv.vitamui.pastis.util.PastisSAX2Handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.*;
import java.net.URISyntaxException;
/**
* @author Audrey Realini <realini@cines.fr>
*/
@RestController
@CrossOrigin(origins = "https://dev.vitamui.com:4251", allowCredentials = "true")
public class PastisController {
private static final Logger LOGGER = LoggerFactory.getLogger(PastisController.class);
/** La constant APPLICATION_JSON_UTF8. */
private static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
@RequestMapping (value = "/updateprofile", method = RequestMethod.POST, consumes = APPLICATION_JSON_UTF8, produces = "application/xml")
public String updateprofile(@RequestBody final ElementProperties json) throws IOException {
// Recover a statically generated BaliseXML by buildBaliseXMLTree
json.initTree(json);
BaliseXML.buildBaliseXMLTree(json,0, null);
BaliseXML eparentRng = BaliseXML.baliseXMLStatic;
String response = null;
Writer writer = null;
try {
JAXBContext contextObj = JAXBContext.newInstance(AttributeXML.class, ElementXML.class, DataXML.class,
ValueXML.class, OptionalXML.class, OneOrMoreXML.class,
ZeroOrMoreXML.class, AnnotationXML.class, DocumentationXML.class,
StartXML.class, GrammarXML.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallerObj.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler",
new PastisCustomCharacterEscapeHandler());
ByteArrayOutputStream os = new ByteArrayOutputStream();
writer = new OutputStreamWriter(os, "UTF-8");
marshallerObj.marshal(eparentRng, writer);
response = new String (os.toByteArray(), "UTF-8");
} catch (IOException e1) {
e1.printStackTrace();
} catch (JAXBException e1) {
e1.printStackTrace();
}
finally {
writer.close();
}
System.out.println(response);
return response;
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "Pastis API is listening...";
}
@RequestMapping (value = "/getfile", method = RequestMethod.GET, produces = "text/plain")
public ResponseEntity<String> getFile() {
InputStream rngFile = getClass().getClassLoader().getResourceAsStream("profile3.rng");
if (rngFile != null) {
return new ResponseEntity<>(rngFile.toString(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping (value = "/createprofile", method = RequestMethod.GET)
public ResponseEntity<String> createprofile() throws URISyntaxException {
PastisSAX2Handler handler = new PastisSAX2Handler();
PastisGetXmlJsonTree getJson = new PastisGetXmlJsonTree();
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(handler);
String rngFile = new PastisGetPropertyValues().getPropValues().getProperty("rng.base.file");
System.out.println("rngFilePath : " + rngFile);
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(rngFile);
InputSource inputSource = new InputSource(inputStream);
xmlReader.parse(inputSource);
} catch (SAXException | IOException e ) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return ResponseEntity.ok(getJson.getJsonParsedTree(handler.elementRNGRoot));
}
@RequestMapping (value = "/createprofilefromfile", method = RequestMethod.POST, consumes = "multipart/form-data", produces = "application/json")
public ResponseEntity<String> createprofilefromfile(@RequestParam MultipartFile multipartFile ) {
PastisSAX2Handler handler = new PastisSAX2Handler();
PastisGetXmlJsonTree getJson = new PastisGetXmlJsonTree();
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(multipartFile.getInputStream()));
} catch (IOException | SAXException e) {
return new ResponseEntity<>("Error while processing file", HttpStatus.INTERNAL_SERVER_ERROR);
}
return ResponseEntity.ok(getJson.getJsonParsedTree(handler.elementRNGRoot));
}
}
/*
* 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.jaxb.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author rachid Sala <rachid@cines.fr>
*/
@XmlRootElement (name="xsd:annotation")
public class AnnotationXML extends BaliseXML{
DocumentationXML documentationXML;
@XmlElement (name="xsd:documentation")
public DocumentationXML getDocumentationXML() {
return documentationXML;
}
public void setDocumentationXML(DocumentationXML documentationXML) {
this.documentationXML = documentationXML;
}
}
/*
* 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.jaxb.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author rachid Sala <rachid@cines.fr>
*/
@XmlRootElement (name="rng:attribute")
public class AttributeXML extends BaliseXML{
ValueXML valueXML;
@XmlElement (name="rng:value")
public ValueXML getValueXML() {
return valueXML;
}
public void setValueXML(ValueXML valueXML) {
this.valueXML = valueXML;
}
}
/*
* 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.jaxb.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import fr.gouv.vitamui.pastis.model.ElementProperties;
import fr.gouv.vitamui.pastis.util.RNGConstants.Cardinality;
import fr.gouv.vitamui.pastis.util.RNGConstants.GroupOrChoice;
/**
* @author rachid Sala <rachid@cines.fr>
*/
@XmlRootElement
public class BaliseXML{
public BaliseXML() {
super();
}
String name;
String dataType;
String cardinality;
String groupOrChoice;
public static ElementProperties elementStatic = new ElementProperties();
public static BaliseXML baliseXMLStatic;
public static ElementProperties elementStaticRoot = new ElementProperties();
List<BaliseXML> children = new ArrayList<BaliseXML>();
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute (name="type")
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
BaliseXML parent;
//transient pour ne pas avoir une boucle infini pere/children
@XmlTransient
public BaliseXML getParent() {
return parent;
}
public void setParent(BaliseXML parent) {
this.parent = parent;
}
// XmlAnyElement pour etre le plus generique
@XmlAnyElement
public List<BaliseXML> getChildren() {
return children;
}
public void setChildren(List<BaliseXML> children) {
this.children = children;
}
public String getCardinality() {
return cardinality;
}
public String setCardinality(Cardinality cardinality) {
return this.cardinality = cardinality.getLabel();
}
public String getGroupOrChoice() {
return groupOrChoice;
}
public void setGroupOrChoice(GroupOrChoice groupOrChoice) {
this.groupOrChoice = groupOrChoice.getLabel();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.name;
}
/**
*
* @param node node représentant l'arbre ElementProperties correspondant au json reçu du front
* @param profondeur profondeur du noeud utile pour le front angular
* @param parentNode noeud parent utilisé dans la récursivité pour lié parent & children
* @return
*/
public static BaliseXML buildBaliseXMLTree( ElementProperties node, int profondeur, BaliseXML parentNode ){
ValueXML valueRNG = null;
DataXML dataRNG = null;
BaliseXML cardinalityRNG = null;
BaliseXML elementOrAttributeRNG = null;
AnnotationXML annotationXML = null;
DocumentationXML documentationXML = null;
BaliseXML groupOrChoice = null;
// si node a une value on crée une balise value
if(null != node.getValue() && !node.getValue().equals("undefined")) {
valueRNG = new ValueXML();
valueRNG.setValue(node.getValue());
}
//si node a une getValuleOrData=data on crée une balise data
if(null != node.getValueOrData() && !node.getValueOrData().equals("undefined")) {
if(node.getValueOrData().equals("data")) {
dataRNG = new DataXML();
}
}
//on set le type de donnée, pour celui créé que ce soit value ou data
if(null != node.getDataType() && !node.getDataType().equals("undefined")) {
//if(null != valueRNG) {
//valueRNG.setDataType(node.getDataType());
//}else
//if(null != dataRNG) {
dataRNG.setDataType(node.getDataType());
//}
}
if(null != node.getDocumentation() ) {
annotationXML = new AnnotationXML();
documentationXML = new DocumentationXML();
documentationXML.setDocumentation(node.getDocumentation());
annotationXML.setDocumentationXML(documentationXML);
}
if(null != node.getType() && !node.getType().equals("undefined")) {
if(node.getType().equals("element")) {
elementOrAttributeRNG = new ElementXML();
} else if(node.getType().equals("attribute")) {
elementOrAttributeRNG = new AttributeXML();
}
if(null != node.getName() && !node.getName().equals("undefined")) {
elementOrAttributeRNG.setName(node.getName());
}
}
if(null != documentationXML) {
elementOrAttributeRNG.getChildren().add(annotationXML);
annotationXML.setParent(elementOrAttributeRNG);
}
// Check node's and its children's cardinality
if(node.getCardinality() != null ) {
if(node.getCardinality().equals(Cardinality.zeroOrMore.getLabel())) {
cardinalityRNG = new ZeroOrMoreXML();
if(elementOrAttributeRNG != null) {
elementOrAttributeRNG.setCardinality(Cardinality.zeroOrMore);
}
} else if(node.getCardinality().equals(Cardinality.oneOrMore.getLabel())) {
cardinalityRNG = new OneOrMoreXML();
if(elementOrAttributeRNG != null) {
elementOrAttributeRNG.setCardinality(Cardinality.oneOrMore);
}
} else if(node.getCardinality().equals(Cardinality.optional.getLabel())) {
cardinalityRNG = new OptionalXML();
if(elementOrAttributeRNG != null) {
elementOrAttributeRNG.setCardinality(Cardinality.optional);
}
}
}
// if(node.getGroupOrChoice() != null ) {
// if(node.getGroupOrChoice().equals(GroupOrChoice.group.getLabel())) {
// groupOrChoice = new GroupXML();
// if(elementOrAttributeRNG != null) {
// elementOrAttributeRNG.setGroupOrChoice(GroupOrChoice.group);
// }
// } else if(node.getGroupOrChoice().equals(GroupOrChoice.choice.getLabel())) {
// groupOrChoice = new ChoiceXml();
// if(elementOrAttributeRNG != null) {
// elementOrAttributeRNG.setGroupOrChoice(GroupOrChoice.choice);
// }
// }
// }
BaliseXML currentXmlTag = null;
// 1. Check if it is an element
if(null != elementOrAttributeRNG) {
System.out.println(elementOrAttributeRNG.getName());
// 1.1 Check if the element has cardinality
if( null != cardinalityRNG ) {
cardinalityRNG.getChildren().add(elementOrAttributeRNG);
elementOrAttributeRNG.setParent(cardinalityRNG);
currentXmlTag = cardinalityRNG;
}
//
// if( null != groupOrChoice ) {
// groupOrChoice.getChildren().add(elementOrAttributeRNG);
// elementOrAttributeRNG.setParent(groupOrChoice);
// currentXmlTag = groupOrChoice;
// }
else {
currentXmlTag = elementOrAttributeRNG;
//1.2. Check if it's the first grammarnode (Archive transfer)
if(parentNode == null) {
GrammarXML grammar = new GrammarXML();
StartXML start = new StartXML();
start.setParent(grammar);
//start.setChildren(currentXmlTag.getChildren());
grammar.getChildren().add(start);
currentXmlTag = grammar;
//elementOrAttributeRNG = start;
System.out.println("Is the root node");
}
// if (parentNode != null && !parentNode.getChildren().isEmpty()
// && parentNode.getName() !=null
// && !parentNode.getName().equals("ArchiveTransfer")) {
// BaliseXML parentTagIsCardinality = parentNode.getChildren().stream()
// .filter(element -> element instanceof OptionalXML
// || element instanceof ZeroOrMoreXML
// || element instanceof OneOrMoreXML)
// .findFirst()
// .orElse(null);
// if (parentTagIsCardinality != null
// cardinalityRNG && !parentNode.getName().equals(node.getParent().getName())) {
// parentTagIsCardinality.getChildren().add(elementOrAttributeRNG);
// currentXmlTag.setParent(parentTagIsCardinality);
// }
// }
}
// 2. Check data tag
if(null != dataRNG) {
if (!currentXmlTag.getChildren().isEmpty() &&
(currentXmlTag.getChildren().get(0) instanceof ElementXML
||currentXmlTag.getChildren().get(0) instanceof AttributeXML)){
currentXmlTag.getChildren().get(0).getChildren().add(dataRNG);
dataRNG.setParent(currentXmlTag);
}else {
currentXmlTag.getChildren().add(dataRNG);
dataRNG.setParent(currentXmlTag);
}
//cardinalityRNG.setParent(currentXmlTag.getChildren().get(0));
// 3. Check value tag
} if(null != valueRNG) {
// If Children is empty
if(currentXmlTag.getChildren().isEmpty() ) {
if (currentXmlTag instanceof ElementXML) {
((ElementXML)currentXmlTag).setValueXML(valueRNG);
valueRNG.setParent(currentXmlTag);
} else if (currentXmlTag instanceof AttributeXML) {
((AttributeXML)currentXmlTag).setValueXML(valueRNG);
valueRNG.setParent(currentXmlTag);
//ccx
}
// If children is Element or Attribute, set accordingly
} else {
if (currentXmlTag instanceof ElementXML) {
((ElementXML)currentXmlTag).setValueXML(valueRNG);
valueRNG.setParent(currentXmlTag);
} else if (currentXmlTag instanceof AttributeXML) {
((AttributeXML)currentXmlTag).setValueXML(valueRNG);
valueRNG.setParent(currentXmlTag);
}
}
}
}
if(null != currentXmlTag) {
if(null != parentNode) {
BaliseXML optionalWithChidren = parentNode.getChildren()
.stream().filter(cardinality-> cardinality instanceof OptionalXML
|| cardinality instanceof ZeroOrMoreXML
|| cardinality instanceof OneOrMoreXML )
.findAny()
.orElse(null);
Boolean optionalHasAlreadyCurrentTag = optionalWithChidren == null
? false : optionalWithChidren.children.contains(currentXmlTag);
if (!optionalHasAlreadyCurrentTag) {
currentXmlTag.setParent(parentNode);
parentNode.getChildren().add(currentXmlTag);
}
}else {
baliseXMLStatic = currentXmlTag;
}
}
if (currentXmlTag instanceof GrammarXML) {
buildBaliseXMLTree( node, profondeur + 1, currentXmlTag.getChildren().get(0));
}
else {
for( ElementProperties next : node.getChildren() ) {
if (currentXmlTag instanceof OptionalXML || currentXmlTag instanceof OneOrMoreXML
|| currentXmlTag instanceof ZeroOrMoreXML) {
buildBaliseXMLTree( next, profondeur + 1, currentXmlTag.getChildren().get(0));
} else {
buildBaliseXMLTree( next, profondeur + 1, currentXmlTag );
}
}
}
return currentXmlTag;
}
}
package fr.gouv.vitamui.pastis.jaxb.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name="rng:choice")
public class ChoiceXml extends BaliseXML {
}
/*
* 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.jaxb.model;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author rachid Sala <rachid@cines.fr>
*/
@XmlRootElement (name="rng:data")
public class DataXML extends BaliseXML{
}
/*
* 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.jaxb.model;