Skip to content

Commit

Permalink
fixing epcfile reading (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-gauthier-geosiris committed Sep 21, 2023
1 parent 8e6e8d0 commit d8dd473
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 38 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.geosiris</groupId>
<artifactId>energyml-utils</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.0.7</version>
<version>1.0.8</version>
<organization>
<name>Geosiris</name>
<url>http://www.geosiris.com</url>
Expand Down Expand Up @@ -102,6 +102,12 @@
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.geosiris</groupId>
<artifactId>energyml-resqml201</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
94 changes: 86 additions & 8 deletions src/main/java/com/geosiris/energyml/pkg/EPCFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
package com.geosiris.energyml.pkg;

import com.geosiris.energyml.utils.EPCGenericManager;
import com.geosiris.energyml.utils.ExportVersion;
import com.geosiris.energyml.utils.ObjectController;
import com.geosiris.energyml.utils.Utils;
import com.geosiris.energyml.utils.*;
import energyml.content_types.Default;
import energyml.content_types.Override;
import energyml.content_types.Types;
Expand Down Expand Up @@ -53,27 +50,38 @@ public class EPCFile {
Map<String, InputStream> otherFiles;
Map<Object, List<Relationship>> additionalRels;

// Key is (Uuid;ObjectVersion)
// Map<Pair<String, String>, List<Relationship>> readRels;

ExportVersion version;
CoreProperties coreProperties;
EPCPackageManager pkgManager;

String filePath;

public EPCFile(EPCPackageManager pkgManager, ExportVersion version, CoreProperties coreProperties, Map<String, List<Object>> energymlObjects, Map<String, InputStream> otherFiles, Map<Object, List<Relationship>> additionalRels ) {
this.energymlObjects = energymlObjects;
this.otherFiles = otherFiles;
this.additionalRels = additionalRels;
this.version = version;
this.coreProperties = coreProperties;
this.pkgManager = pkgManager;
this.filePath = null;
}

public EPCFile(EPCPackageManager pkgManager, ExportVersion version, CoreProperties coreProperties){
public EPCFile(EPCPackageManager pkgManager, ExportVersion version, CoreProperties coreProperties){
this(pkgManager, version, coreProperties, new HashMap<>(), new HashMap<>(), new HashMap<>());
}

public EPCFile(EPCPackageManager pkgManager, ExportVersion version){
this(pkgManager, version, new CoreProperties());
}

public EPCFile(EPCPackageManager pkgManager, String filePath){
this(pkgManager);
this.filePath = filePath;
}

public EPCFile(EPCPackageManager pkgManager){
this(pkgManager, ExportVersion.EXPANDED);
coreProperties.setVersion("1.0");
Expand Down Expand Up @@ -222,7 +230,7 @@ public List<String> getAllVersions(String uuid){
return version;
}

private Map<Object, Relationships> computeRelations(){
public Map<Object, Relationships> computeRelations(){
Map<Object, Relationships> relations = new HashMap<>();

Map<Object, List<Object>> sourceRels = new HashMap<>();
Expand Down Expand Up @@ -294,6 +302,12 @@ private Map<Object, Relationships> computeRelations(){
return relations;
}

public static EPCFile read(String filePath, EPCPackageManager pkgManager) throws FileNotFoundException {
EPCFile file = read(new FileInputStream(filePath), pkgManager);
file.filePath = filePath;
return file;
}

public static EPCFile read(InputStream input, EPCPackageManager pkgManager){
EPCFile epc = new EPCFile(pkgManager);
byte[] buffer = new byte[2048];
Expand Down Expand Up @@ -335,7 +349,7 @@ public static EPCFile read(InputStream input, EPCPackageManager pkgManager){
}else if (entry.getName().endsWith("." + OPCRelsPackage.getRelsExtension())){
Relationships rels = (Relationships) OPCRelsPackage.unmarshal(new ByteArrayInputStream(entryBOS.toByteArray()));
String objPath = entry.getName()
.substring(0, entry.getName().length() - 4)
.substring(0, entry.getName().lastIndexOf(".")) // removing rels extension
.replace(OPCRelsPackage.genRelsFolderPath(epc.version) + "/", "")
.replace(OPCRelsPackage.genRelsFolderPath(epc.version) + "\\", "");
mapPathToRelationships.put(objPath, rels);
Expand All @@ -346,10 +360,23 @@ public static EPCFile read(InputStream input, EPCPackageManager pkgManager){
throw new RuntimeException(e);
}

// if(epc.readRels == null){
// epc.readRels = new HashMap<>();
// }else {
// epc.readRels.clear();
// }

for(Map.Entry<String, Relationships> rels: mapPathToRelationships.entrySet()) {
if (mapPathToObject.containsKey(rels.getKey())){
Object target = mapPathToObject.get(rels.getKey());

Pair<String, String> obj_pair = new Pair<>((String) ObjectController.getObjectAttributeValue(target, "uuid"),
(String) ObjectController.getObjectAttributeValue(target, "ObjectVersion"));
// if(!epc.readRels.containsKey(obj_pair)){
// epc.readRels.put(obj_pair, new ArrayList<>());
// }
// epc.readRels.get(obj_pair).addAll(rels.getValue().getRelationship());

for(Relationship r: rels.getValue().getRelationship()){
if(EPCRelsRelationshipType.DestinationObject.getType() .compareToIgnoreCase(r.getType()) != 0
&& EPCRelsRelationshipType.SourceObject.getType().compareToIgnoreCase(r.getType()) != 0){
Expand All @@ -361,11 +388,62 @@ public static EPCFile read(InputStream input, EPCPackageManager pkgManager){
}
}else{
logger.error("Object " + rels.getKey() + " not found for rels");
for(String k: mapPathToObject.keySet()){
logger.info("\t" + k + " ==> " + mapPathToObject.containsKey(rels.getKey()) + "--" + mapPathToObject.get(rels.getKey()));
}
}
}
logger.info("EPC " + epc.energymlObjects.size());
logger.debug("EPC " + epc.energymlObjects.size());

epc.version = foundNamespaceFolder ? ExportVersion.EXPANDED : ExportVersion.CLASSIC;
return epc;
}

// public String findNumericalDataLocation(String uuid, String objectVersion){
// Object related = getObject(uuid, objectVersion);
// if(related != null){
// Relationships rels = computeRelations().get(related);
// for(Relationship rel : rels.getRelationship()){
// if(rel.getType().compareToIgnoreCase(EPCRelsRelationshipType.ExternalResource.label) == 0){
// if(rel.getTarget().toLowerCase().endsWith(".h5") || rel.getTarget().toLowerCase().endsWith(".hdf5")){
// return rel.getTarget();
// }
// }
// }
// List<Object> references = new ArrayList<>();
// references.addAll(ObjectController.findAllAttributesFromName(related, "EpcExternalPartReference", true, false));
// references.addAll(ObjectController.findAllAttributesFromName(related, "HdfProxy", true, false));
//
// for(Object ref: references){
//
// }
// }
// return null;
// }

/* --------------------------------------------------- */

public Map<String, List<Object>> getEnergymlObjects() {
return energymlObjects;
}

public Map<String, InputStream> getOtherFiles() {
return otherFiles;
}

public Map<Object, List<Relationship>> getAdditionalRels() {
return additionalRels;
}

// public Map<Pair<String, String>, List<Relationship>> getReadRels() {
// return readRels;
// }

public ExportVersion getVersion() {
return version;
}

public CoreProperties getCoreProperties() {
return coreProperties;
}
}
16 changes: 12 additions & 4 deletions src/main/java/com/geosiris/energyml/pkg/EPCPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -102,7 +103,12 @@ public boolean isReleaseVersion(){
}

public List<Class<?>> getRootsElementsClasses() {
return pkgClasses.stream().filter(EPCGenericManager::isRootClass).collect(Collectors.toList());
// return pkgClasses.stream().filter(EPCGenericManager::isRootClass).collect(Collectors.toList()); OLD Way, but it includes types with no "createMY_TYPE_NAME(MY_TYPE value) -> JaxbElement" functions
Object objFactory = getObjectFactory();
return Arrays.stream(objFactory.getClass().getMethods())
.filter(m -> m.getReturnType() == JAXBElement.class && m.getParameterCount() == 1)
.map(m->m.getParameters()[0].getType())
.collect(Collectors.toList());
}

public Object createInstance(String className) {
Expand Down Expand Up @@ -295,9 +301,11 @@ public static JAXBElement<?> wrap(Object energymlObject, Object factory) throws
methodPotentialName.add("create" + typename);
methodPotentialName.add(typename.substring(0, 1).toLowerCase() + typename.substring(1));
for (String prefixToRemove : listOfPotentialRemovablePrefix) {
String noPrefix = typename.substring(prefixToRemove.length());
methodPotentialName.add("create" + noPrefix);
methodPotentialName.add(noPrefix.substring(0, 1).toLowerCase() + noPrefix.substring(1));
if(typename.startsWith(prefixToRemove)) {
String noPrefix = typename.substring(prefixToRemove.length());
methodPotentialName.add("create" + noPrefix);
methodPotentialName.add(noPrefix.substring(0, 1).toLowerCase() + noPrefix.substring(1));
}
}

for(String m_name : methodPotentialName){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public HashMap<String, String> getClassesComment() {

for (EPCPackage pkg : PKG_LIST) {
try {
logger.info("@getClassesComment " + pkg);
logger.debug("@getClassesComment " + pkg);
result.putAll(Utils.readJsonFileOrRessource(xsdCommentsFolderPath + pkg.getPackageName() + ".json",
HashMap.class));
} catch (Exception ignore) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/geosiris/energyml/utils/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ public TL l() {
public TR r() {
return _r;
}

@Override
public boolean equals(Object b){
return b instanceof Pair
&& ((this._l == null && ((Pair<?, ?>)b)._l == null)
|| ((this._l != null && ((Pair<?, ?>)b)._l != null && this._l.equals(((Pair<?, ?>)b)._l)))
)
&& ((this._r == null && ((Pair<?, ?>)b)._r == null)
|| ((this._r != null && ((Pair<?, ?>)b)._r != null && this._r.equals(((Pair<?, ?>)b)._r)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
*/
package com.geosiris.energyml.utils.test;

import com.geosiris.energyml.pkg.EPCFile;
import com.geosiris.energyml.pkg.EPCPackageManager;
import com.geosiris.energyml.utils.EPCGenericManager;
import com.geosiris.energyml.utils.ExportVersion;
import com.geosiris.energyml.utils.ObjectController;
import com.geosiris.energyml.utils.Utils;
import energyml.common2_2.DataObjectReference;
import com.geosiris.energyml.utils.*;
import energyml.common2_3.Citation;
import energyml.relationships.Relationship;
import energyml.relationships.Relationships;
import energyml.resqml2_2.TriangulatedSetRepresentation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -315,23 +319,4 @@ public static energyml.resqml_dev3x_2_2.TriangulatedSetRepresentation createTest

return tr;
}

public static void main(String[] argv){
// System.out.println(EPCGenericManager.getObjectQualifiedType(cName_tsr22dev3, true));
// Matcher m_tsr201 = EPCGenericManager.PATTERN_QUALIFIED_TYPE.matcher(EPCGenericManager.getObjectQualifiedType(cName_tsr201, true));
// m_tsr201.find();
// System.out.println(m_tsr201.group("domain"));
// System.out.println(m_tsr201.group("domainVersion"));
// System.out.println(m_tsr201.group("type"));
// EPCPackageManager manager = new EPCPackageManager("energyml", "", "", "");
// String tr0 = Utils.readFileOrRessource("C:/Users/Cryptaro/Downloads/TriangulatedSetRepresentation_616f910c-b3f2-4910-978b-146c21762d12.xml");
// System.out.println(tr0);
// manager.getClasses().stream().filter(cl -> cl != null && !Modifier.isAbstract(cl.getModifiers())).collect(Collectors.toList()).forEach(x -> System.out.println(x));


String graphical = "<?xml version=\"1.0\" encoding=\"utf-8\"?><GraphicalInformationSet xmlns=\"http://www.energistics.org/energyml/data/commonv2\" xmlns:ns2=\"http://www.energistics.org/energyml/data/resqmlv2\" uuid=\"5e3206c9-b3fa-4506-9ac0-1c76fdf72fbc\" schemaVersion=\"2.3\"><Citation><Title>GRAPHICAL INFORMATION SET 1</Title><Originator>Geosiris user</Originator><Creation>2023-05-24T09:15:46.016Z</Creation><Format>Geosiris WebStudio</Format><LastUpdate>2023-05-25T20:27:34.990Z</LastUpdate></Citation><GraphicalInformation xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ns2:ColorInformation\"><TargetObject><Uuid>d1966850-6b1e-4dd7-abda-3c5b7b975712</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F3_2011</Title></TargetObject><TargetObject><Uuid>8e329639-4097-4467-9e0e-a04e645c9035</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F20_2013_update</Title></TargetObject><TargetObject><Uuid>38bf3283-9514-43ab-81e3-17080dc5826f</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F20_2011_New2</Title></TargetObject><TargetObject><Uuid>7908100a-68c1-4c07-a2f9-1c6f76af3a43</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F52_2011</Title></TargetObject><TargetObject><Uuid>052ec11a-e9f9-4faa-8512-ba0ef589952e</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F28_2011</Title></TargetObject><TargetObject><Uuid>3229af74-8dcd-4df5-8148-9b4f54d0c381</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F11_2011</Title></TargetObject><TargetObject><Uuid>4e23ee3e-54a7-427a-83f9-1473de6c56a4</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F27_2011</Title></TargetObject><TargetObject><Uuid>303bed39-6290-4ea7-b9f1-917438833fd5</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F8_2013_update</Title></TargetObject><TargetObject><Uuid>203ebec9-df29-4a6b-b621-ba1b8378c7fa</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F10_2011</Title></TargetObject><TargetObject><Uuid>27cf5a6d-6869-45e8-a061-555236f1546c</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F17_2011</Title></TargetObject><TargetObject><Uuid>162b0fd8-6dad-43ce-af6c-4b6370af767a</Uuid><QualifiedType>resqml22.PolylineSetRepresentation</QualifiedType><Title>depthVolve_F7_2011</Title></TargetObject><ns2:UseLogarithmicMapping>false</ns2:UseLogarithmicMapping><ns2:UseReverseMapping>false</ns2:UseReverseMapping><ns2:ValueVectorIndex>2</ns2:ValueVectorIndex><ns2:ColorMap><Uuid>44920872-7f80-4fd9-992e-cd941f45c2e4</Uuid><QualifiedType>resqml22.DiscreteColorMap</QualifiedType><Title>DISCRETE COLOR MAP 0</Title></ns2:ColorMap></GraphicalInformation><GraphicalInformation xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ns2:ColorInformation\"><TargetObject><Uuid>349ecd25-5db0-40c1-b179-b5316fbc754f</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F27_2011_MBA</Title></TargetObject><TargetObject><Uuid>c2f53ce1-1b2d-4819-b397-f174bc8c23e0</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F11_2011_MBA</Title></TargetObject><TargetObject><Uuid>5db39032-4998-4b75-9156-ea104a8649d2</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F17_2011_MBA</Title></TargetObject><TargetObject><Uuid>fa4e80d0-d30f-48d3-aa70-41d06b837c2b</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F3_2011_MBA</Title></TargetObject><TargetObject><Uuid>bc1fbf7f-1495-4f1f-9c30-8be4b19a5475</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F7_2011_MBA</Title></TargetObject><TargetObject><Uuid>79f43d6a-77e1-46b7-b161-127b395a64be</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F28_2011_MBA</Title></TargetObject><TargetObject><Uuid>cdd9c2cd-6673-44b0-8d6d-812319daacaa</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F20_2013_update_MBA</Title></TargetObject><TargetObject><Uuid>13f6b289-3b0a-4ec3-b060-347c1efd6e5e</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F10_2011_MBA</Title></TargetObject><TargetObject><Uuid>5755aa3a-87b3-4a5d-bdf1-b8a6c1ebdf36</Uuid><QualifiedType>resqml22.TriangulatedSetRepresentation</QualifiedType><Title>depthVolve_F52_2011_MBA</Title></TargetObject><ns2:UseLogarithmicMapping>false</ns2:UseLogarithmicMapping><ns2:UseReverseMapping>false</ns2:UseReverseMapping><ns2:ValueVectorIndex>5</ns2:ValueVectorIndex><ns2:ColorMap><Uuid>44920872-7f80-4fd9-992e-cd941f45c2e4</Uuid><QualifiedType>resqml22.DiscreteColorMap</QualifiedType><Title>DISCRETE COLOR MAP 0</Title></ns2:ColorMap></GraphicalInformation></GraphicalInformationSet>";

EPCPackageManager manager = new EPCPackageManager("energyml");
System.out.println(manager.unmarshal(graphical).getValue());
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/geosiris/energyml/utils/test/PairTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.geosiris.energyml.utils.test;

import com.geosiris.energyml.utils.Pair;
import org.junit.jupiter.api.Test;

public class PairTest {

@Test
void test_equality(){
assert new Pair<>("abdc", "COUCOU").equals(new Pair<>("abdc", "COUCOU"));
assert new Pair<>("abdc", "COUCOU").equals(new Pair<>("abdc", "COUCOU"));
assert new Pair<>(1, "COUCOU").equals(new Pair<>(1, "COUCOU"));
assert new Pair<>(1, 2.3).equals(new Pair<>(1, 2.3));
assert ! new Pair<>("a", "COUCOU").equals(new Pair<>("abdc", "COUCOU"));
assert ! new Pair<>("abdc", "C").equals(new Pair<>("abdc", "COUCOU"));
assert new Pair<>("abdc", null).equals(new Pair<>("abdc", null));
assert ! new Pair<>("abdc", (Object) "a").equals(new Pair<>("abdc", null));
assert ! new Pair<>("abdc", null).equals(new Pair<>("abdc", (Object) "a"));
}
}

0 comments on commit d8dd473

Please sign in to comment.