From d8dd4734dbc1e51e90e353ce5476ac0d5f7e96a3 Mon Sep 17 00:00:00 2001 From: valentin-gauthier-geosiris <88202743+valentin-gauthier-geosiris@users.noreply.github.com> Date: Fri, 22 Sep 2023 01:16:04 +0200 Subject: [PATCH] fixing epcfile reading (#21) --- pom.xml | 8 +- .../com/geosiris/energyml/pkg/EPCFile.java | 94 +++++++++++++++++-- .../com/geosiris/energyml/pkg/EPCPackage.java | 16 +++- .../energyml/pkg/EPCPackageManager.java | 2 +- .../com/geosiris/energyml/utils/Pair.java | 11 +++ .../utils/test/EPCGenericManagerTest.java | 33 ++----- .../energyml/utils/test/PairTest.java | 20 ++++ 7 files changed, 146 insertions(+), 38 deletions(-) create mode 100644 src/test/java/com/geosiris/energyml/utils/test/PairTest.java diff --git a/pom.xml b/pom.xml index 816da38..07566f6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.geosiris energyml-utils ${project.groupId}:${project.artifactId} - 1.0.7 + 1.0.8 Geosiris http://www.geosiris.com @@ -102,6 +102,12 @@ 1.0.4 test + + com.geosiris + energyml-resqml201 + 1.0.4 + test + diff --git a/src/main/java/com/geosiris/energyml/pkg/EPCFile.java b/src/main/java/com/geosiris/energyml/pkg/EPCFile.java index 90dcc14..fd68358 100644 --- a/src/main/java/com/geosiris/energyml/pkg/EPCFile.java +++ b/src/main/java/com/geosiris/energyml/pkg/EPCFile.java @@ -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; @@ -53,10 +50,15 @@ public class EPCFile { Map otherFiles; Map> additionalRels; + // Key is (Uuid;ObjectVersion) +// Map, List> readRels; + ExportVersion version; CoreProperties coreProperties; EPCPackageManager pkgManager; + String filePath; + public EPCFile(EPCPackageManager pkgManager, ExportVersion version, CoreProperties coreProperties, Map> energymlObjects, Map otherFiles, Map> additionalRels ) { this.energymlObjects = energymlObjects; this.otherFiles = otherFiles; @@ -64,9 +66,10 @@ public EPCFile(EPCPackageManager pkgManager, ExportVersion version, CoreProperti 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<>()); } @@ -74,6 +77,11 @@ 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"); @@ -222,7 +230,7 @@ public List getAllVersions(String uuid){ return version; } - private Map computeRelations(){ + public Map computeRelations(){ Map relations = new HashMap<>(); Map> sourceRels = new HashMap<>(); @@ -294,6 +302,12 @@ private Map 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]; @@ -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); @@ -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 rels: mapPathToRelationships.entrySet()) { if (mapPathToObject.containsKey(rels.getKey())){ Object target = mapPathToObject.get(rels.getKey()); + Pair 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){ @@ -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 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> getEnergymlObjects() { + return energymlObjects; + } + + public Map getOtherFiles() { + return otherFiles; + } + + public Map> getAdditionalRels() { + return additionalRels; + } + +// public Map, List> getReadRels() { +// return readRels; +// } + + public ExportVersion getVersion() { + return version; + } + + public CoreProperties getCoreProperties() { + return coreProperties; + } } diff --git a/src/main/java/com/geosiris/energyml/pkg/EPCPackage.java b/src/main/java/com/geosiris/energyml/pkg/EPCPackage.java index 6d66a8d..61b5b9d 100644 --- a/src/main/java/com/geosiris/energyml/pkg/EPCPackage.java +++ b/src/main/java/com/geosiris/energyml/pkg/EPCPackage.java @@ -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; @@ -102,7 +103,12 @@ public boolean isReleaseVersion(){ } public List> 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) { @@ -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){ diff --git a/src/main/java/com/geosiris/energyml/pkg/EPCPackageManager.java b/src/main/java/com/geosiris/energyml/pkg/EPCPackageManager.java index 0c6664c..7f2e048 100644 --- a/src/main/java/com/geosiris/energyml/pkg/EPCPackageManager.java +++ b/src/main/java/com/geosiris/energyml/pkg/EPCPackageManager.java @@ -600,7 +600,7 @@ public HashMap 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) { diff --git a/src/main/java/com/geosiris/energyml/utils/Pair.java b/src/main/java/com/geosiris/energyml/utils/Pair.java index fc41046..665e46e 100644 --- a/src/main/java/com/geosiris/energyml/utils/Pair.java +++ b/src/main/java/com/geosiris/energyml/utils/Pair.java @@ -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))) + ); + } } diff --git a/src/test/java/com/geosiris/energyml/utils/test/EPCGenericManagerTest.java b/src/test/java/com/geosiris/energyml/utils/test/EPCGenericManagerTest.java index 3cce8c9..da9a643 100644 --- a/src/test/java/com/geosiris/energyml/utils/test/EPCGenericManagerTest.java +++ b/src/test/java/com/geosiris/energyml/utils/test/EPCGenericManagerTest.java @@ -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; @@ -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 = "GRAPHICAL INFORMATION SET 1Geosiris user2023-05-24T09:15:46.016ZGeosiris WebStudio2023-05-25T20:27:34.990Zd1966850-6b1e-4dd7-abda-3c5b7b975712resqml22.PolylineSetRepresentationdepthVolve_F3_20118e329639-4097-4467-9e0e-a04e645c9035resqml22.PolylineSetRepresentationdepthVolve_F20_2013_update38bf3283-9514-43ab-81e3-17080dc5826fresqml22.PolylineSetRepresentationdepthVolve_F20_2011_New27908100a-68c1-4c07-a2f9-1c6f76af3a43resqml22.PolylineSetRepresentationdepthVolve_F52_2011052ec11a-e9f9-4faa-8512-ba0ef589952eresqml22.PolylineSetRepresentationdepthVolve_F28_20113229af74-8dcd-4df5-8148-9b4f54d0c381resqml22.PolylineSetRepresentationdepthVolve_F11_20114e23ee3e-54a7-427a-83f9-1473de6c56a4resqml22.PolylineSetRepresentationdepthVolve_F27_2011303bed39-6290-4ea7-b9f1-917438833fd5resqml22.PolylineSetRepresentationdepthVolve_F8_2013_update203ebec9-df29-4a6b-b621-ba1b8378c7faresqml22.PolylineSetRepresentationdepthVolve_F10_201127cf5a6d-6869-45e8-a061-555236f1546cresqml22.PolylineSetRepresentationdepthVolve_F17_2011162b0fd8-6dad-43ce-af6c-4b6370af767aresqml22.PolylineSetRepresentationdepthVolve_F7_2011falsefalse244920872-7f80-4fd9-992e-cd941f45c2e4resqml22.DiscreteColorMapDISCRETE COLOR MAP 0349ecd25-5db0-40c1-b179-b5316fbc754fresqml22.TriangulatedSetRepresentationdepthVolve_F27_2011_MBAc2f53ce1-1b2d-4819-b397-f174bc8c23e0resqml22.TriangulatedSetRepresentationdepthVolve_F11_2011_MBA5db39032-4998-4b75-9156-ea104a8649d2resqml22.TriangulatedSetRepresentationdepthVolve_F17_2011_MBAfa4e80d0-d30f-48d3-aa70-41d06b837c2bresqml22.TriangulatedSetRepresentationdepthVolve_F3_2011_MBAbc1fbf7f-1495-4f1f-9c30-8be4b19a5475resqml22.TriangulatedSetRepresentationdepthVolve_F7_2011_MBA79f43d6a-77e1-46b7-b161-127b395a64beresqml22.TriangulatedSetRepresentationdepthVolve_F28_2011_MBAcdd9c2cd-6673-44b0-8d6d-812319daacaaresqml22.TriangulatedSetRepresentationdepthVolve_F20_2013_update_MBA13f6b289-3b0a-4ec3-b060-347c1efd6e5eresqml22.TriangulatedSetRepresentationdepthVolve_F10_2011_MBA5755aa3a-87b3-4a5d-bdf1-b8a6c1ebdf36resqml22.TriangulatedSetRepresentationdepthVolve_F52_2011_MBAfalsefalse544920872-7f80-4fd9-992e-cd941f45c2e4resqml22.DiscreteColorMapDISCRETE COLOR MAP 0"; - - EPCPackageManager manager = new EPCPackageManager("energyml"); - System.out.println(manager.unmarshal(graphical).getValue()); - } } diff --git a/src/test/java/com/geosiris/energyml/utils/test/PairTest.java b/src/test/java/com/geosiris/energyml/utils/test/PairTest.java new file mode 100644 index 0000000..8d35bf9 --- /dev/null +++ b/src/test/java/com/geosiris/energyml/utils/test/PairTest.java @@ -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")); + } +}