diff --git a/src/main/java/tr/com/srdc/standardtransform/core/XmlTransform.java b/src/main/java/tr/com/srdc/standardtransform/core/XmlTransform.java index c040aa9..cc79151 100644 --- a/src/main/java/tr/com/srdc/standardtransform/core/XmlTransform.java +++ b/src/main/java/tr/com/srdc/standardtransform/core/XmlTransform.java @@ -52,7 +52,60 @@ public XmlTransform() throws ParserConfigurationException, IOException, SAXExcep logger.setLevel( Level.ALL ); } + + /** + * Asserts whether every mapped text context appears on target document. + */ + private boolean testMethod( Document sourceDocument, Document targetDocument, File csvFile ) throws IOException, XPathExpressionException { + boolean returnResult = true; + + BufferedReader bufferedReader = new BufferedReader( new FileReader( csvFile ) ); + String line; + + while ( ( line = bufferedReader.readLine() ) != null ) { + String[] mapping = line.split( ";" ); + + if ( mapping[ 2 ].equals( "." ) == false ) { + List sourceNodeList = XmlUtil.asList( (NodeList) xPath.compile( mapping[ 1 ] ).evaluate( sourceDocument, XPathConstants.NODESET ) ); + + if ( sourceNodeList.size() == 0 ) { + continue; + } + + // Target + List targetNodeList = XmlUtil.asList( (NodeList) xPath.compile( mapping[ 2 ] ).evaluate( targetDocument, XPathConstants.NODESET ) ); + + if ( targetNodeList.size() == 0 ) { + continue; + } + + if ( findUnboundedAncestorNode( targetNodeList.get( 0 ) ) == null ) { + continue; + } + + for ( Node sourceNode : sourceNodeList ) { + boolean miniResult = false; + + for ( Node targetNode : targetNodeList ) { + if ( sourceNode.getTextContent().trim().equals( targetNode.getTextContent().trim() ) ) { + miniResult = true; + break; + } + } + + if ( miniResult == false ) { + returnResult = false; + logger.log( Level.SEVERE, "Source value " + sourceNode.getTextContent().trim() + " doesn't exist!!!" ); + } + } + } + } + + return returnResult; + } + /** + * Deletes descendant nonmapped nodes and custom attributes(cardainality, required, haveNoTextContext). * @param node Base node to delete appropriate elements */ private void deleteNonmappedNodes( Node node ) { @@ -150,6 +203,8 @@ private void revertNodeToTemplateForm( Node node ) { * @return Returns first unbounded ancestor node if found, returns null if it doesn't have one. */ private Node findUnboundedAncestorNode( Node unboundedAncestor ) { + + while ( unboundedAncestor.getParentNode() != null ) { if ( XmlUtil.isUnboundedNode( unboundedAncestor ) ) { break; @@ -256,12 +311,12 @@ public String transformDocument( Document sourceDocumentParam, Document template continue; } - if ( mapping[ 0 ].equals( "resourceDesc" ) || mapping[ 0 ].equals( "event" ) || mapping[ 0 ].equals( "responseType" ) ) { + if ( mapping[ 0 ].equals( "resourceDesc" ) || mapping[ 0 ].equals( "Value" ) ) { System.out.print( "" ); } // Mapping have been previously done, so just create new nodes - if ( targetNodeList.get( 0 ).getTextContent().replaceAll( "££££", "" ).trim().equals( "" ) == false ) { + if ( XmlUtil.isNonmappedNode( targetNodeList.get( 0 ) ) == false ) { createBoundedNodes( sourceNodeList, targetNodeList.get( 0 ) ); } else { @@ -272,20 +327,28 @@ public String transformDocument( Document sourceDocumentParam, Document template if ( sourceNodeList.size() > 1 ) { if ( sourceNodeList.size() > nonmappedNodesCount ) { + int beforeCount = targetNodeList.size(); + createBoundedNodes( sourceNodeList.subList( 1, sourceNodeList.size() ), targetNodeList.get( 0 ) ); -/* targetNodeList = XmlUtil.asList( (NodeList) xPath.compile( mapping[ 2 ] ).evaluate( templateDocumentParam, XPathConstants.NODESET ) ); + //this piece of code is to move the reference node which new nodes inserted before it to the top of new created nodes - Node kayikNode = targetNodeList.get( targetNodeList.size() - 1 ); - Node babaKayikNode = findUnboundedAncestorNode( kayikNode ); - Node babaRefNode = findUnboundedAncestorNode( targetNodeList.get( 0 ) ); + targetNodeList = XmlUtil.asList( (NodeList) xPath.compile( mapping[ 2 ] ).evaluate( templateDocumentParam, XPathConstants.NODESET ) ); - if ( babaKayikNode == null ) { + //no new node have been created, so simply skip + if ( beforeCount == targetNodeList.size() ) { continue; } - babaRefNode.getParentNode().insertBefore( babaKayikNode, babaRefNode ); -*/ + Node referenceNode = targetNodeList.get( sourceNodeList.size() - 1 ); + Node unboundeAncestorOfReferenceNode = findUnboundedAncestorNode( referenceNode ); + Node babaNode = findUnboundedAncestorNode( targetNodeList.get( 0 ) ); + + if ( unboundeAncestorOfReferenceNode == null ) { + continue; + } + + babaNode.getParentNode().insertBefore( unboundeAncestorOfReferenceNode, babaNode ); } else { replaceNodes( sourceNodeList.subList( 1, sourceNodeList.size() ), targetNodeList.subList( 1, targetNodeList.size() ) ); @@ -298,6 +361,8 @@ public String transformDocument( Document sourceDocumentParam, Document template System.out.print( "" ); } + testMethod( sourceDocumentParam, templateDocumentParam, csvFile ); + deleteNonmappedNodes( templateDocumentParam.getDocumentElement() ); XmlUtil.writeResultToFile( templateDocumentParam, "output.xml" ); return XmlUtil.prettyPrint( templateDocumentParam ); @@ -315,9 +380,11 @@ public static void main( String[] args ) throws ParserConfigurationException, IO */ //output2 +/* Document sourceDocument = builder.parse( new File( XmlTransform.class.getClassLoader().getResource( "SampleXmlFiles/Fake/CAP/edxl-cap1.xml" ).getFile() ) ); Document targetDocument = builder.parse( new File( XmlTransform.class.getClassLoader().getResource( "SampleXmlFiles/Templates/SensorMlTemplate.xml" ).getFile() ) ); File csvFile = new File( XmlTransform.class.getClassLoader().getResource( "SampleXmlFiles/Mappings/mapping--cap--sensorml.csv" ).getFile() ); +*/ //output3 /* @@ -368,6 +435,11 @@ public static void main( String[] args ) throws ParserConfigurationException, IO File csvFile = new File( XmlTransform.class.getClassLoader().getResource( "TestFiles/CSV/test--overwrite--rm--cap.csv" ).getFile() ); */ + //output10 + Document sourceDocument = builder.parse( new File( XmlTransform.class.getClassLoader().getResource( "SampleXmlFiles/RealWorld/RM/RMRequestResource_OASIS_Example.xml" ).getFile() ) ); + Document targetDocument = builder.parse( new File( XmlTransform.class.getClassLoader().getResource( "SampleXmlFiles/Templates/CAPTemplate.xml" ).getFile() ) ); + File csvFile = new File( XmlTransform.class.getClassLoader().getResource( "TestFiles/CSV/test--kayik--rm--cap.csv" ).getFile() ); + //XmlUtil.traverse( targetDocument.getDocumentElement() ); XmlTransform xmlTransform = new XmlTransform(); diff --git a/src/main/resources/TestFiles/CSV/test--kayik--rm--cap.csv b/src/main/resources/TestFiles/CSV/test--kayik--rm--cap.csv new file mode 100644 index 0000000..a4e4fb5 --- /dev/null +++ b/src/main/resources/TestFiles/CSV/test--kayik--rm--cap.csv @@ -0,0 +1,7 @@ +IncidentDescription;/RequestResource/IncidentInformation/IncidentDescription;/alert/info/parameter/valueName +IncidentDescription;/RequestResource/IncidentInformation/IncidentDescription;/alert/info/parameter/value +ValueListURN;/RequestResource/ResourceInformation/Resource/TypeStructure/ValueListURN;/alert/info/parameter/valueName +Value;/RequestResource/ResourceInformation/Resource/TypeStructure/Value;/alert/info/parameter/value +MessageContentType;/RequestResource/MessageContentType;/alert/info/parameter/value +MessageID;/RequestResource/MessageID;/alert/info/parameter/value +IncidentDescription;/RequestResource/IncidentInformation/IncidentDescription;/alert/info/parameter/value \ No newline at end of file