Java tutorial
/* Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.c4om.autoconf.ulysses.configanalyzer.objectivesextractor; import static com.c4om.utils.xmlutils.JDOMUtils.*; import java.io.File; import java.io.IOException; import org.jdom2.Document; import org.jdom2.JDOMException; import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.ConfigurationAnalysisContext; import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.Objective; import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.impl.ModifyXMLFromSVRLResultObjective; import com.c4om.autoconf.ulysses.configanalyzer.core.exceptions.ObjectivesExtractionException; import com.c4om.autoconf.ulysses.configanalyzer.objectivesextractor.exceptions.SchematronObjectivesExtractionException; import com.c4om.autoconf.ulysses.interfaces.Target; import com.c4om.utils.xmlutils.constants.CommonXMLConstants; /** * Abstract class, base for any {@link ObjectivesExtractor} which extracts objectives from the * SVRL results of a schematron validation. Here, we implement all the validator-independent code * to avoid repetition. Subclasses of this one must implement abstract methods to perform the actual * validation via the corresponding validator. * @author Pablo Alonso Rodrguez (Center for Open Middleware - UPM) * */ public abstract class SchematronBasedExtractor implements ObjectivesExtractor { /** * Solve hint suggested by the controller for the objective extracted by the current objectives extractor. */ protected String suggestedSolveHint; /** * Flag that indicates whether extracted objective must be required or not. */ protected boolean suggestedRequired; /** * Suggested id for the extracted objective */ protected String suggestedId; /** * The source target for objectives extracted by this extractor. */ protected Target sourceTarget; /** * The schematron schema to be used, as a JDOM2 {@link Document} */ protected Document schematronSchema; /** * The validated input file, as a JDOM2 {@link Document} */ protected Document inputCandidateDocument; /** * The validator working directory. It is important, as it may affect relative paths used, for instance, by 'document' XSLT function (allowed at schematron schemas by most implementations). */ protected File workingDirectory; /** * Method to fill the fields at constructors. * @param suggestedSolveHint the solve hint suggested by the controller for objectives extracted by the current objectives extractor. * @param sourceTarget the source target for objectives extracted by this extractor. * @param suggestedRequired flag that indicates whether extracted objective must be required or not. * @param suggestedId suggested id for the extracted objective * @param schematronSchema the schematron schema to be used, as a JDOM2 {@link Document} * @param inputCandidateDocument the candidate document to validate, as a JDOM2 {@link Document} * @param workingDirectory the validator working directory */ private void fillFields(String suggestedSolveHint, Target sourceTarget, boolean suggestedRequired, String suggestedId, Document schematronSchema, Document inputCandidateDocument, File workingDirectory) { this.suggestedSolveHint = suggestedSolveHint; this.suggestedRequired = suggestedRequired; this.schematronSchema = schematronSchema; this.inputCandidateDocument = inputCandidateDocument; this.workingDirectory = workingDirectory; this.suggestedId = suggestedId; this.sourceTarget = sourceTarget; } /** * Constructor. * @param suggestedSolveHint the solve hint suggested by the controller for objectives extracted by the current objectives extractor. * @param sourceTarget the source target for objectives extracted by this extractor. * @param suggestedRequired whether extracted objectives should be required or not * @param suggestedId suggested id for the extracted objective * @param schematronSchema the schematron schema to be used, as a JDOM2 {@link Document} * @param inputCandidateDocument the candidate document to validate, as a JDOM2 {@link Document} * @param workingDirectory the validator working directory */ public SchematronBasedExtractor(String suggestedSolveHint, Target sourceTarget, boolean suggestedRequired, String suggestedId, Document schematronSchema, Document inputCandidateDocument, File workingDirectory) { fillFields(suggestedSolveHint, sourceTarget, suggestedRequired, suggestedId, schematronSchema, inputCandidateDocument, workingDirectory); } /** * Constructor for no input candidate (a dummy temporary file is created). * @param suggestedSolveHint the solve hint suggested by the controller for objectives extracted by the current objectives extractor. * @param sourceTarget the source target for objectives extracted by this extractor. * @param suggestedRequired whether extracted objectives should be required or not * @param suggestedId suggested id for the extracted objective * @param schematronSchema the schematron schema to be used, as a JDOM2 {@link Document} * @param workingDirectory the validator working directory * @throws IOException if there are problems at temp file creation or at schematron file reading. * @throws JDOMException if there are problems at XML parsing */ public SchematronBasedExtractor(String suggestedSolveHint, Target sourceTarget, boolean suggestedRequired, String suggestedId, Document schematronSchema, File workingDirectory) throws IOException, JDOMException { Document inputCandidateDocument = loadJDOMDocumentFromString("<dummy/>"); fillFields(suggestedSolveHint, sourceTarget, suggestedRequired, suggestedId, schematronSchema, inputCandidateDocument, workingDirectory); } /** * Constructor for no input candidate (a dummy temporary file is created). * @param suggestedSolveHint the solve hint suggested by the controller for objectives extracted by the current objectives extractor. * @param sourceTarget the source target for objectives extracted by this extractor. * @param suggestedRequired whether extracted objectives should be required or not * @param suggestedId suggested id for the extracted objective * @param schematronSchemaFile a {@link File} object pointing to the schematron schema to be used * @param workingDirectory the validator working directory * @throws IOException if there are problems at temp file creation or at schematron file reading. * @throws JDOMException if there are problems at XML parsing */ public SchematronBasedExtractor(String suggestedSolveHint, Target sourceTarget, boolean suggestedRequired, String suggestedId, File schematronSchemaFile, File workingDirectory) throws IOException, JDOMException { Document schematronSchema = loadJDOMDocumentFromFile(schematronSchemaFile); Document inputCandidateDocument = loadJDOMDocumentFromString("<dummy/>"); fillFields(suggestedSolveHint, sourceTarget, suggestedRequired, suggestedId, schematronSchema, inputCandidateDocument, workingDirectory); } /** * Constructor * @param suggestedSolveHint the solve hint suggested by the controller for objectives extracted by the current objectives extractor. * @param sourceTarget the source target for objectives extracted by this extractor. * @param suggestedRequired whether extracted objectives should be required or not * @param suggestedId suggested id for the extracted objective * @param schematronSchemaFile a {@link File} object pointing to the schematron schema to be used * @param inputCandidateDocumentFile a {@link File} object pointing to the candidate document to validate * @param workingDirectory the validator working directory * @throws JDOMException if there are problems at XML parsing * @throws IOException if there are I/O problems with the input files */ public SchematronBasedExtractor(String suggestedSolveHint, Target sourceTarget, boolean suggestedRequired, String suggestedId, File schematronSchemaFile, File inputCandidateDocumentFile, File workingDirectory) throws JDOMException, IOException { Document schematronSchema = loadJDOMDocumentFromFile(schematronSchemaFile); Document inputCandidate = loadJDOMDocumentFromFile(inputCandidateDocumentFile); fillFields(suggestedSolveHint, sourceTarget, suggestedRequired, suggestedId, schematronSchema, inputCandidate, workingDirectory); } /** * Abstract method that must be implemented by subclasses. It must return a JDOM2 {@link Document} with * the SVRL result of a schematron validation. * @param schematronSchema the schematron schema * @param inputCandidateDocument the candidate document * @param workingDirectory the working directory of the validator. * @return the SVRL results, as a JDOM2 document * @throws SchematronObjectivesExtractionException if there is any problem at schematron extraction */ protected abstract Document performSchematronValidation(Document schematronSchema, Document inputCandidateDocument, File workingDirectory) throws SchematronObjectivesExtractionException; /** * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.objectivesextractor.ObjectivesExtractor#extractObjectives(com.c4om.autoconf.ulysses.interfaces.configanalyzer.core.datastructures.ConfigurationAnalysisContext) */ @Override public void extractObjectives(ConfigurationAnalysisContext context) throws ObjectivesExtractionException { Document svrlResult = performSchematronValidation(schematronSchema, inputCandidateDocument, workingDirectory); if (svrlResult.getRootElement().getChildren("failed-assert", CommonXMLConstants.NAMESPACE_SVRL) .size() > 0) { Objective<Document> extractedObjective = new ModifyXMLFromSVRLResultObjective(suggestedId, sourceTarget, svrlResult, suggestedSolveHint, suggestedRequired); context.getObjectivesToSatisfy().add(extractedObjective); } } }