List of usage examples for org.apache.commons.digester3 Digester Digester
public Digester(XMLReader reader)
From source file:eu.scape_project.planning.xml.TreeLoader.java
private MindMap loadFreeMindMap(InputStream in) { try {/* w w w . ja v a 2 s . c o m*/ MindMap map = new MindMap(); SAXParserFactory f = SAXParserFactory.newInstance(); SAXParser parser = f.newSAXParser(); // SAXParser parser = validatingParserFactory.getValidatingParser(); // parser.setProperty(ValidatingParserFactory.JAXP_SCHEMA_SOURCE, // "http://freemind.sourceforge.net/freemind.xsd"); // load content into temporary structure Digester digester = new Digester(parser); // digester.setEntityResolver(new SchemaResolver().addSchemaLocation( // "http://freemind.sourceforge.net/freemind.xsd", "data/schemas/freemind.xsd")); // digester.setErrorHandler(new StrictErrorHandler()); digester.push(map); digester.addObjectCreate("*/node", "eu.scape_project.planning.xml.freemind.Node"); digester.addSetProperties("*/node"); digester.addCallMethod("*/node/hook/text", "setDESCRIPTION", 0); digester.addSetNext("*/node", "addChild"); digester.setUseContextClassLoader(true); digester.parse(in); return map; } catch (IOException e) { log.error("Error loading Freemind file.", e); } catch (SAXException e) { log.error("Document is not a valid Freemind file.", e); } catch (ParserConfigurationException e) { log.error("Parser not properly configured.", e); } return null; }
From source file:eu.scape_project.planning.xml.PlanMigrator.java
/** * Detect the version of the given XML representation of plans. If the * version of the XML representation is not up to date, necessary * transformations are applied.//from w w w. j a v a 2 s. c o m * * @param importData * @return null if the transformation fails, otherwise an up to date XML * representation * @throws IOException * if parsing the XML representation fails * @throws SAXException * if parsing the XML representation fails */ public String getCurrentVersionData(final InputStream in, final String tempPath, final List<String> appliedTransformations) throws PlatoException { String originalFile = tempPath + "_original.xml"; try { FileUtils.writeToFile(in, new FileOutputStream(originalFile)); /** check for the version of the file **/ // The version of the read xml file is unknown, so it is not possible to // validate it // moreover, in old plans the version attribute was on different // nodes(project, projects), // with a different name (fileVersion) // to be backwards compatible we create rules for all these attributes fileVersion = "xxx"; SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); Digester d = new Digester(factory.newSAXParser()); d.setValidating(false); // StrictErrorHandler errorHandler = new StrictErrorHandler(); // d.setErrorHandler(errorHandler); d.push(this); // to read the version we have to support all versions: d.addSetProperties("*/projects", "version", "fileVersion"); // manually migrated projects may have the file version in the node // projects/project d.addSetProperties("*/projects/project", "version", "fileVersion"); // pre V1.3 version info was stored in the project node d.addSetProperties("*/project", "version", "fileVersion"); // since V1.9 the root node is plans: d.addSetProperties("plans", "version", "fileVersion"); InputStream inV = new FileInputStream(originalFile); d.parse(inV); inV.close(); /** this could be more sophisticated, but for now this is enough **/ String version = "1.0"; if (fileVersion != null) { version = fileVersion; } String fileTo = originalFile; String fileFrom = originalFile; boolean success = true; if ("xxx".equals(version)) { fileFrom = fileTo; fileTo = fileFrom + "_V1.3.xml"; /** this is an old export file, transform it to the 1.3 schema **/ success = transformXmlData(fileFrom, fileTo, "data/xslt/Vxxx-to-V1.3.xsl"); appliedTransformations.add("Vxxx-to-V1.3.xsl"); version = "1.3"; } if (success && "1.3".equals(version)) { fileFrom = fileTo; fileTo = fileFrom + "_V1.9.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V1.3-to-V1.9.xsl"); appliedTransformations.add("V1.3-to-V1.9.xsl"); version = "1.9"; } // with release of Plato 2.0 and its schema ProjectExporter creates // documents with version 2.0 if (success && "1.9".equals(version)) { version = "2.0"; } if (success && "2.0".equals(version)) { // transform the document to version 2.1 fileFrom = fileTo; fileTo = fileFrom + "_V2.1.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.0-to-V2.1.xsl"); appliedTransformations.add("V2.0-to-V2.1.xsl"); version = "2.1"; } if (success && "2.1".equals(version)) { // transform the document to version 2.1.2 fileFrom = fileTo; fileTo = fileFrom + "_V2.1.2.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.1-to-V2.1.2.xsl"); appliedTransformations.add("V2.1-to-V2.1.2.xsl"); version = "2.1.2"; } if (success && "2.1.1".equals(version)) { // transform the document to version 2.1.2 fileFrom = fileTo; fileTo = fileFrom + "_V2.1.2.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.1.1-to-V2.1.2.xsl"); appliedTransformations.add("V2.1.1-to-V2.1.2.xsl"); version = "2.1.2"; } if (success && "2.1.2".equals(version)) { // transform the document to version 3.0.0 fileFrom = fileTo; fileTo = fileFrom + "_V3.0.0.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.1.2-to-V3.0.0.xsl"); appliedTransformations.add("V2.1.2-to-V3.0.0.xsl"); version = "3.0.0"; } if (success && "3.0.0".equals(version)) { // transform the document to version 3.0.1 fileFrom = fileTo; fileTo = fileFrom + "_V3.0.1.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.0.0-to-V3.0.1.xsl"); appliedTransformations.add("V3.0.0-to-V3.0.1.xsl"); version = "3.0.1"; } if (success && "3.0.1".equals(version)) { // transform the document to version 3.9.0 fileFrom = fileTo; fileTo = fileFrom + "_V3.9.0.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.0.1-to-V3.9.0.xsl"); appliedTransformations.add("V3.0.1-to-V3.9.0.xsl"); version = "3.9.0"; } if (success && "3.9.0".equals(version)) { // transform the document to version 3.9.9 fileFrom = fileTo; fileTo = fileFrom + "_V3.9.9.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.9.0-to-V3.9.9.xsl"); appliedTransformations.add("V3.9.0-to-V3.9.9.xsl"); version = "3.9.9"; } if (success && "3.9.9".equals(version)) { // transform the document to version 4.0.0 fileFrom = fileTo; fileTo = fileFrom + "_V4.0.1.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.9.9-to-V4.0.1.xsl"); appliedTransformations.add("V3.9.9-to-V4.0.1.xsl"); version = "4.0.1"; } if (success && "4.0.1".equals(version)) { // transform the document to version 4.0.0 fileFrom = fileTo; fileTo = fileFrom + "_V4.0.2.xml"; success = transformXmlData(fileFrom, fileTo, "data/xslt/V4.0.1-to-V4.0.2.xsl"); appliedTransformations.add("V4.0.1-to-V4.0.2.xsl"); version = "4.0.2"; } if (success) { return fileTo; } else { return null; } } catch (Exception e) { throw new PlatoException("Failed to update plan to current version.", e); } }
From source file:eu.scape_project.planning.xml.PlanParser.java
/** * Imports the XML representation of plans from the given input stream. * //w w w . ja va2 s . co m * @param in * the input stream to read from * @return list of read plans * @throws PlatoException * if the plan cannot be parsed */ public List<Plan> importProjects(final InputStream in) throws PlatoException { try { SAXParser parser = validatingParserFactory.getValidatingParser(); parser.setProperty(ValidatingParserFactory.JAXP_SCHEMA_SOURCE, PlanXMLConstants.PLAN_SCHEMAS); Digester digester = new Digester(parser); SchemaResolver schemaResolver = new SchemaResolver(); schemaResolver .addSchemaLocation(PlanXMLConstants.PLATO_SCHEMA_URI, PlanXMLConstants.PLATO_SCHEMA_LOCATION) .addSchemaLocation(PlanXMLConstants.PAP_SCHEMA_URI, PlanXMLConstants.PAP_SCHEMA_LOCATION) .addSchemaLocation(PlanXMLConstants.TAVERNA_SCHEMA_URI, PlanXMLConstants.TAVERNA_SCHEMA_LOCATION); digester.setEntityResolver(schemaResolver); digester.setErrorHandler(new StrictErrorHandler()); digester.setNamespaceAware(true); digester.push(this); PlanParser.addRules(digester); digester.setUseContextClassLoader(true); plans = new ArrayList<Plan>(); // finally parse the XML representation with all created rules digester.parse(in); for (Plan plan : plans) { String projectName = plan.getPlanProperties().getName(); if ((projectName != null) && (!"".equals(projectName))) { /* * establish links from values to scales. For all(!) * alternatives: An alternative could have be discarded * after some measurements have already been added. */ plan.getTree().initValues(plan.getAlternativesDefinition().getAlternatives(), plan.getSampleRecordsDefinition().getRecords().size(), true); /* * establish references of Experiment.uploads */ HashMap<String, SampleObject> records = new HashMap<String, SampleObject>(); for (SampleObject record : plan.getSampleRecordsDefinition().getRecords()) { records.put(record.getShortName(), record); } for (Alternative alt : plan.getAlternativesDefinition().getAlternatives()) { if ((alt.getExperiment() != null) && (alt.getExperiment() instanceof ExperimentWrapper)) { alt.setExperiment(((ExperimentWrapper) alt.getExperiment()).getExperiment(records)); } } // CHECK NUMERIC TRANSFORMER THRESHOLDS for (Leaf l : plan.getTree().getRoot().getAllLeaves()) { eu.scape_project.planning.model.transform.Transformer t = l.getTransformer(); if (t != null && t instanceof NumericTransformer) { NumericTransformer nt = (NumericTransformer) t; if (!nt.checkOrder()) { StringBuffer sb = new StringBuffer("NUMERICTRANSFORMER THRESHOLD ERROR "); sb.append(l.getName()).append("::NUMERICTRANSFORMER:: "); sb.append(nt.getThreshold1()).append(" ").append(nt.getThreshold2()).append(" ") .append(nt.getThreshold3()).append(" ").append(nt.getThreshold4()) .append(" ").append(nt.getThreshold5()); log.error(sb.toString()); } } } /* * establish references to selected alternative */ HashMap<String, Alternative> alternatives = new HashMap<String, Alternative>(); for (Alternative alt : plan.getAlternativesDefinition().getAlternatives()) { alternatives.put(alt.getName(), alt); } if ((plan.getRecommendation() != null) && (plan.getRecommendation() instanceof RecommendationWrapper)) { plan.setRecommendation( ((RecommendationWrapper) plan.getRecommendation()).getRecommendation(alternatives)); } if ((plan.getPlanProperties().getState() == PlanState.ANALYSED) && ((plan.getRecommendation() == null) || (plan.getRecommendation().getAlternative() == null))) { /* * This project is NOT completely analysed */ plan.getPlanProperties().setState(PlanState.valueOf(PlanState.ANALYSED.getValue() - 1)); } } else { throw new PlatoException("Could not find any project data."); } } } catch (Exception e) { throw new PlatoException("Failed to import plans.", e); } return plans; }