Skip to content
Permalink
c3cef35b72
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
167 lines (144 sloc) 5.94 KB
package com.lmco.spectrum.systemnavigation3d.service;
import com.lmco.spectrum.systemnavigation3d.domain.catia.DocumentSMG;
import com.lmco.spectrum.systemnavigation3d.dto.TransformerOptions;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import javax.annotation.Nullable;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.*;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
@Service
public class TransformationService {
private static final String SERVER_TYPE_CLitList = "CLitList";
private static final String SERVER_TYPE_CLitIOManager = "CLitIOManager";
private static final String TAG_SERVER = "Server";
private static final String TAG_LAUNCH_ACTION = "LaunchAction";
private static final String ATTR_TYPE = "Type";
private static final String ATTR_NAME = "Name";
private static final String ATTR_VALUE = "Value";
// view tags
private static final String TAG_VIEW = "CLitView";
private static final String ATTR_IDENT = "Ident";
// TODO make functional
// TODO consider types of transformations we need to make
// accept list of transformation options?
// accept GEIA dtos and infer transformations?
// pending
public String transform(String smgXml, @Nullable TransformerOptions options){
DocumentSMG doc = unmarshal(smgXml);
if(doc == null) {
// TODO ??
return smgXml;
}
doc = processTransformations(doc, options);
return marshal(doc);
}
private DocumentSMG processTransformations(DocumentSMG doc, @Nullable TransformerOptions options){
if(options == null) {
return doc;
}
if(options.getSetFocusedView() != null) {
ensureViewMode(doc);
setFocusedView(doc, options.getSetFocusedView());
createView(doc, "test");
}
return doc;
}
private void createView(DocumentSMG doc, String viewName) {
for(Element el : doc.getNodes()){
if(el.getTagName().equals(TAG_SERVER)
&& el.hasAttribute(ATTR_TYPE)
&& el.getAttribute(ATTR_TYPE).equals(SERVER_TYPE_CLitList)
) {
Document smgDoc = el.getOwnerDocument();
Element newView = smgDoc.createElement(TAG_VIEW);
el.appendChild(newView);
newView.setAttribute(ATTR_IDENT, new SimpleDateFormat("HH.mm.ss").format(new java.util.Date()));
newView.setAttribute(ATTR_NAME, viewName);
newView.setAttribute(ATTR_TYPE, "1");
break;
}
}
}
private void ensureViewMode(DocumentSMG doc) {
for(Element el : doc.getNodes()) {
if(el.getTagName().equals(TAG_SERVER)
&& el.hasAttribute(ATTR_TYPE)
&& el.getAttribute(ATTR_TYPE).equals(SERVER_TYPE_CLitIOManager)
&& el.hasChildNodes()
) {
Element currentChild = (Element) el.getFirstChild();
while(currentChild != null) {
if(currentChild.getTagName().equals(TAG_LAUNCH_ACTION)) {
if(currentChild.hasAttribute(ATTR_VALUE)) {
currentChild.setAttribute(ATTR_VALUE, "1");
currentChild = null;
}
} else {
currentChild = (Element) currentChild.getNextSibling();
}
}
break;
}
}
}
// TODO make there is an additional CLitList, ensure we are looking at the correct one.
private void setFocusedView(DocumentSMG doc, String viewName) {
for(Element el : doc.getNodes()) {
if(el.getTagName().equals(TAG_SERVER)
&& el.hasAttribute(ATTR_TYPE)
&& el.getAttribute(ATTR_TYPE).equals(SERVER_TYPE_CLitList)
&& el.hasChildNodes()
) {
Element target = null;
Element currentChild = (Element) el.getFirstChild();
while(currentChild != null) {
if(currentChild.getAttribute(ATTR_NAME).equalsIgnoreCase(viewName)) {
target = currentChild;
currentChild = null;
} else {
currentChild = (Element) currentChild.getNextSibling();
}
}
if(target != null) {
el.removeChild(target);
el.insertBefore(target, el.getFirstChild());
break;
}
}
}
}
private String marshal(DocumentSMG doc) {
try(StringWriter stringWriter = new StringWriter()) {
JAXBContext jaxbContext = JAXBContext.newInstance(DocumentSMG.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(doc, stringWriter);
return stringWriter.toString();
} catch (JAXBException | IOException e) {
e.printStackTrace();
return null;
}
}
private DocumentSMG unmarshal(String smgXml) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DocumentSMG.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(smgXml));
try {
return unmarshaller.unmarshal(reader, DocumentSMG.class).getValue();
} finally {
reader.close();
}
} catch (JAXBException | XMLStreamException e) {
e.printStackTrace();
return null;
}
}
}