Author: chatellier Date: 2009-12-21 10:08:14 +0000 (Mon, 21 Dec 2009) New Revision: 2854 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/mexico/MexicoHelper.java isis-fish/trunk/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java Log: Format output xml (indentation). Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/mexico/MexicoHelper.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/mexico/MexicoHelper.java 2009-12-21 09:50:24 UTC (rev 2853) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/mexico/MexicoHelper.java 2009-12-21 10:08:14 UTC (rev 2854) @@ -20,13 +20,25 @@ import java.io.File; import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; -import org.nuiton.topia.TopiaContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; +import org.nuiton.topia.TopiaContext; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import fr.ifremer.isisfish.IsisFishRuntimeException; import fr.ifremer.isisfish.mexico.xml.DesignPlanXMLVisitor; import fr.ifremer.isisfish.mexico.xml.DomXMLParser; import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan; @@ -55,6 +67,10 @@ DesignPlanXMLVisitor visitor = new DesignPlanXMLVisitor(); designPlan.accept(visitor); String designPlanXml = visitor.getXML(); + + // apply beautiful xml indented format + designPlanXml = MexicoHelper.formatXML(designPlanXml); + return designPlanXml; } @@ -91,7 +107,7 @@ try { SAXReader reader = new SAXReader(); // don't use reader.read(String); - // don't work on windows beacause of : in path + // don't work on windows because of : in path // Document doc = reader.read(xmlFile); reader.setEncoding("utf-8"); Document doc = reader.read(xmlFile); @@ -125,4 +141,41 @@ } return scenarios; }*/ + + /** + * Format xml string. + * + * @param unformattedXml non formatted xml string (must be valid xml) + * @return xml, formatted and indented + * + * @throws IsisFishRuntimeException + * @throws IllegalArgumentException if input xml is not valid + */ + public static String formatXML(String unformattedXml) { + + try { + // parseXmlFile + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(unformattedXml)); + org.w3c.dom.Document document = db.parse(is); + + // format + OutputFormat format = new OutputFormat(document); + format.setLineWidth(65); + format.setIndenting(true); + format.setIndent(2); + Writer out = new StringWriter(); + XMLSerializer serializer = new XMLSerializer(out, format); + serializer.serialize(document); + + return out.toString(); + } catch (IOException e) { + throw new IsisFishRuntimeException(e); + } catch (ParserConfigurationException e) { + throw new IllegalArgumentException(e); + } catch (SAXException e) { + throw new IllegalArgumentException(e); + } + } } Modified: isis-fish/trunk/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java =================================================================== --- isis-fish/trunk/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java 2009-12-21 09:50:24 UTC (rev 2853) +++ isis-fish/trunk/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java 2009-12-21 10:08:14 UTC (rev 2854) @@ -21,15 +21,13 @@ import java.io.File; import java.io.IOException; -import junit.framework.Assert; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Test; import org.nuiton.math.matrix.MatrixFactory; import org.nuiton.math.matrix.MatrixND; import org.nuiton.topia.TopiaContext; -import org.nuiton.util.FileUtil; -import org.junit.Test; import fr.ifremer.isisfish.AbstractIsisFishTest; import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan; @@ -67,18 +65,15 @@ matrix1.setValue(new int[] { 2, 1 }, -1); // factor 1 - Factor<Double, Double> factorContinuous = new Factor<Double, Double>( - "factor 1 (double continuous)"); + Factor<Double, Double> factorContinuous = new Factor<Double, Double>("factor 1 (double continuous)"); ContinuousDomain<Double, Double> domain1 = new ContinuousDomain<Double, Double>(); domain1.setMinBound(0.0); domain1.setMaxBound(50.0); factorContinuous.setDomain(domain1); - factorContinuous - .setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength"); + factorContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength"); // factor 2 - Factor<Double, Double> factorDiscrete = new Factor<Double, Double>( - "factor 2 (double discrete)"); + Factor<Double, Double> factorDiscrete = new Factor<Double, Double>("factor 2 (double discrete)"); DiscreteDomain<Double, Double> domain2 = new DiscreteDomain<Double, Double>(); domain2.getValues().put(0.0, 12.3); domain2.getValues().put(1.0, 70.9); @@ -86,43 +81,36 @@ domain2.getValues().put(3.0, -12.1); domain2.getValues().put(4.0, -8.45); factorDiscrete.setDomain(domain2); - factorDiscrete - .setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521064#0.022976136053553198#minLength"); + factorDiscrete.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521064#0.022976136053553198#minLength"); factorDiscrete.setValue(3.0); // factor 3 - Factor<Integer, Double> factorContinuous2 = new Factor<Integer, Double>( - "factor 3 (integer discrete)"); + Factor<Integer, Double> factorContinuous2 = new Factor<Integer, Double>("factor 3 (integer discrete)"); DiscreteDomain<Integer, Double> domain3 = new DiscreteDomain<Integer, Double>(); domain3.getValues().put(0.0, 13); domain3.getValues().put(1.0, 14); domain3.getValues().put(2.0, 45); factorContinuous2.setDomain(domain3); - factorContinuous2 - .setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength"); + factorContinuous2.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength"); // factor 4 - Factor<MatrixND, Double> factorMatrixContinuous = new Factor<MatrixND, Double>( - "factor 4 (MatrixContinuous)"); + Factor<MatrixND, Double> factorMatrixContinuous = new Factor<MatrixND, Double>("factor 4 (MatrixContinuous)"); MatrixContinuousDomain domain4 = new MatrixContinuousDomain(); domain4.setCoefficient(0.1); domain4.setMatrix(matrix1); domain4.setOperator("-"); factorMatrixContinuous.setDomain(domain4); - factorMatrixContinuous - .setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength"); + factorMatrixContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength"); // factor 5 - Factor<Double, Double> factorEquationContinuous = new Factor<Double, Double>( - "factor 5 (EquationContinuous)"); + Factor<Double, Double> factorEquationContinuous = new Factor<Double, Double>("factor 5 (EquationContinuous)"); EquationContinuousDomain domain5 = new EquationContinuousDomain(); domain5.setCoefficient(0.1); domain5.setReferenceValue(45.0); domain5.setVariableName("L1"); domain5.setOperator("-"); factorEquationContinuous.setDomain(domain5); - factorEquationContinuous - .setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#maxLength"); + factorEquationContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#maxLength"); designPlan.getFactors().add(factorContinuous); designPlan.getFactors().add(factorDiscrete); @@ -146,15 +134,11 @@ // test xml export String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan); - Assert.assertTrue(xml - .indexOf("<factor name=\"factor 2 (double discrete)\"") != -1); + Assert.assertTrue(xml.indexOf("<factor name=\"factor 2 (double discrete)\"") != -1); //Assert.assertTrue(xml.indexOf("<range min=\"12.0\" max=\"99.0\" />") != -1); Assert.assertTrue(xml.indexOf("<value>70.9</value>") != -1); if (log.isDebugEnabled()) { - xml = xml.replaceAll(">", ">\n"); - FileUtil.writeString(new File(getTestDirectory(), - "mexicohelper_designplan.xml"), xml); log.info("testGetDesignPlanAsXML xml = " + xml); } } @@ -167,16 +151,16 @@ @Test public void testGetDesignPlanFromXML() throws IOException { - File testFile = new File( - "src/test/resources/mexico/mexicohelper_designplan.xml"); + File testFile = new File("src/test/resources/mexico/mexicohelper_designplan.xml"); // topia context can be null in tests DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, null); Assert.assertEquals(5, plan.getFactors().size()); String xml = MexicoHelper.getDesignPlanAsXML(plan); - xml = xml.replaceAll(">", ">\n"); - log.info("testGetDesignPlanFromXML xml = " + xml); + if (log.isDebugEnabled()) { + log.debug("testGetDesignPlanFromXML xml = " + xml); + } } /** @@ -190,16 +174,18 @@ // first export DesignPlan testDesignPlan = getTestDesignPlan(); String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan); - log.debug("xml 1 = " + xml1); + if (log.isDebugEnabled()) { + log.debug("xml 1 = " + xml1); + } // export - File testFile = new File( - "src/test/resources/mexico/mexicohelper_designplan.xml"); + File testFile = new File("src/test/resources/mexico/mexicohelper_designplan.xml"); // topia context can be null in tests DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, null); String xml2 = MexicoHelper.getDesignPlanAsXML(plan); - log.debug("xml 2 = " + xml2); - + if (log.isDebugEnabled()) { + log.debug("xml 2 = " + xml2); + } Assert.assertEquals(xml1, xml2); } }