Search in sources :

Example 36 with Namespace

use of org.jdom.Namespace in project maven-plugins by apache.

the class PomWriter method write.

public static void write(Writer w, Model newModel, boolean namespaceDeclaration) throws IOException {
    Element root = new Element("project");
    if (namespaceDeclaration) {
        String modelVersion = newModel.getModelVersion();
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/" + modelVersion);
        root.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.addNamespaceDeclaration(xsiNamespace);
        if (root.getAttribute("schemaLocation", xsiNamespace) == null) {
            root.setAttribute("schemaLocation", "http://maven.apache.org/POM/" + modelVersion + " http://maven.apache.org/maven-v" + modelVersion.replace('.', '_') + ".xsd", xsiNamespace);
        }
    }
    Document doc = new Document(root);
    MavenJDOMWriter writer = new MavenJDOMWriter();
    String encoding = newModel.getModelEncoding() != null ? newModel.getModelEncoding() : "UTF-8";
    Format format = Format.getPrettyFormat().setEncoding(encoding);
    writer.write(newModel, doc, w, format);
}
Also used : Format(org.jdom.output.Format) Element(org.jdom.Element) Document(org.jdom.Document) Namespace(org.jdom.Namespace)

Example 37 with Namespace

use of org.jdom.Namespace in project openolat by klemens.

the class SCORM12_DocumentHandler method canHandle.

protected static boolean canHandle(Document doc) throws DocumentHandlerException {
    // The first thing we do is to see if there is a root Namespace in the Document
    Namespace nameSpace = XMLUtils.getDocumentNamespace(doc);
    // No Namespace, sorry we don't know what it is!
    if (nameSpace == null || nameSpace.equals(Namespace.NO_NAMESPACE)) {
        throw new DocumentHandlerException("No Namespace in Document so cannot determine what it is!");
    }
    // Does it have the correct root Namespace?
    if (SUPPORTED_NAMESPACES.containsKey(nameSpace) == false)
        return false;
    // Now find out if it is a SCORM Document and if so whether we support it
    // We'll search all elements for the ADL Namespace
    Namespace nsSCORM = getSCORM_Namespace(doc);
    if (nsSCORM == null)
        return false;
    // Do we support this version of SCORM?
    return SUPPORTED_SCORM_NAMESPACES.containsKey(nsSCORM);
}
Also used : Namespace(org.jdom.Namespace)

Example 38 with Namespace

use of org.jdom.Namespace in project vcell by virtualcell.

the class ParameterEstimationTaskXMLPersistence method getParameterEstimationTask.

/**
 * Insert the method's description here.
 * Creation date: (5/5/2006 4:50:36 PM)
 * @return cbit.vcell.modelopt.ParameterEstimationTask
 * @param element org.jdom.Element
 * @param simContext cbit.vcell.mapping.SimulationContext
 */
public static ParameterEstimationTask getParameterEstimationTask(Element parameterEstimationTaskElement, SimulationContext simContext) throws ExpressionException, MappingException, MathException, java.beans.PropertyVetoException {
    Namespace ns = parameterEstimationTaskElement.getNamespace();
    ParameterEstimationTask parameterEstimationTask = new ParameterEstimationTask(simContext);
    String name = parameterEstimationTaskElement.getAttributeValue(NameAttribute);
    parameterEstimationTask.setName(name);
    Element annotationElement = parameterEstimationTaskElement.getChild(AnnotationTag, ns);
    if (annotationElement != null) {
        String annotationText = annotationElement.getText();
        parameterEstimationTask.setAnnotation(annotationText);
    }
    // 
    // read ParameterMappingSpecs
    // 
    Element parameterMappingSpecListElement = parameterEstimationTaskElement.getChild(ParameterMappingSpecListTag, ns);
    if (parameterMappingSpecListElement != null) {
        List<Element> parameterMappingSpecElementList = parameterMappingSpecListElement.getChildren(ParameterMappingSpecTag, ns);
        for (Element parameterMappingSpecElement : parameterMappingSpecElementList) {
            String parameterName = parameterMappingSpecElement.getAttributeValue(ParameterReferenceAttribute);
            SymbolTableEntry ste = getSymbolTableEntry(simContext, parameterName);
            if (ste instanceof Parameter) {
                Parameter parameter = (Parameter) ste;
                ParameterMappingSpec parameterMappingSpec = parameterEstimationTask.getModelOptimizationSpec().getParameterMappingSpec(parameter);
                if (parameterMappingSpec != null) {
                    String lowLimitString = parameterMappingSpecElement.getAttributeValue(LowLimitAttribute);
                    if (lowLimitString != null) {
                        parameterMappingSpec.setLow(parseDouble(lowLimitString));
                    }
                    String highLimitString = parameterMappingSpecElement.getAttributeValue(HighLimitAttribute);
                    if (highLimitString != null) {
                        parameterMappingSpec.setHigh(parseDouble(highLimitString));
                    }
                    String currentValueString = parameterMappingSpecElement.getAttributeValue(CurrentValueAttribute);
                    if (currentValueString != null) {
                        parameterMappingSpec.setCurrent(Double.parseDouble(currentValueString));
                    }
                    String selectedString = parameterMappingSpecElement.getAttributeValue(SelectedAttribute);
                    if (selectedString != null) {
                        parameterMappingSpec.setSelected(Boolean.valueOf(selectedString).booleanValue());
                    }
                }
            } else {
                System.out.println("couldn't read parameterMappingSpec '" + parameterName + "', ste=" + ste);
            }
        }
    }
    // 
    // read ReferenceData
    // 
    Element referenceDataElement = parameterEstimationTaskElement.getChild(ReferenceDataTag, ns);
    if (referenceDataElement != null) {
        String numRowsText = referenceDataElement.getAttributeValue(NumRowsAttribute);
        String numColsText = referenceDataElement.getAttributeValue(NumColumnsAttribute);
        // int numRows = Integer.parseInt(numRowsText);
        int numCols = Integer.parseInt(numColsText);
        // 
        // read columns
        // 
        String[] columnNames = new String[numCols];
        double[] columnWeights = new double[numCols];
        int columnCounter = 0;
        Element dataColumnListElement = referenceDataElement.getChild(DataColumnListTag, ns);
        List<Element> dataColumnList = dataColumnListElement.getChildren(DataColumnTag, ns);
        for (Element dataColumnElement : dataColumnList) {
            columnNames[columnCounter] = dataColumnElement.getAttributeValue(NameAttribute);
            columnWeights[columnCounter] = Double.parseDouble(dataColumnElement.getAttributeValue(WeightAttribute));
            columnCounter++;
        }
        // 
        // read rows
        // 
        Vector<double[]> rowDataVector = new Vector<double[]>();
        Element dataRowListElement = referenceDataElement.getChild(DataRowListTag, ns);
        List<Element> dataRowList = dataRowListElement.getChildren(DataRowTag, ns);
        for (Element dataRowElement : dataRowList) {
            String rowText = dataRowElement.getText();
            CommentStringTokenizer tokens = new CommentStringTokenizer(rowText);
            double[] rowData = new double[numCols];
            for (int j = 0; j < numCols; j++) {
                if (tokens.hasMoreTokens()) {
                    String token = tokens.nextToken();
                    rowData[j] = Double.parseDouble(token);
                } else {
                    throw new RuntimeException("failed to read row data for ReferenceData");
                }
            }
            rowDataVector.add(rowData);
        }
        ReferenceData referenceData = new SimpleReferenceData(columnNames, columnWeights, rowDataVector);
        parameterEstimationTask.getModelOptimizationSpec().setReferenceData(referenceData);
    }
    // 
    // read ReferenceDataMappingSpecs
    // 
    Element referenceDataMappingSpecListElement = parameterEstimationTaskElement.getChild(ReferenceDataMappingSpecListTag, ns);
    if (referenceDataMappingSpecListElement != null) {
        List<Element> referenceDataMappingSpecList = referenceDataMappingSpecListElement.getChildren(ReferenceDataMappingSpecTag, ns);
        for (Element referenceDataMappingSpecElement : referenceDataMappingSpecList) {
            String referenceDataColumnName = referenceDataMappingSpecElement.getAttributeValue(ReferenceDataColumnNameAttribute);
            String referenceDataModelSymbolName = referenceDataMappingSpecElement.getAttributeValue(ReferenceDataModelSymbolAttribute);
            ReferenceDataMappingSpec referenceDataMappingSpec = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(referenceDataColumnName);
            SymbolTableEntry modelSymbolTableEntry = null;
            if (referenceDataModelSymbolName != null) {
                modelSymbolTableEntry = getSymbolTableEntry(simContext, referenceDataModelSymbolName);
                if (referenceDataMappingSpec != null && modelSymbolTableEntry != null) {
                    referenceDataMappingSpec.setModelObject(modelSymbolTableEntry);
                }
            }
        }
    }
    // 
    // read OptimizationSolverSpec
    // 
    Element optimizationSolverSpecElement = parameterEstimationTaskElement.getChild(OptimizationSolverSpecTag, ns);
    if (optimizationSolverSpecElement != null) {
        OptimizationSolverSpec optSolverSpec = null;
        String optimizationSolverTypeName = optimizationSolverSpecElement.getAttributeValue(OptimizationSolverTypeAttribute);
        // getting parameters
        Element optimizationSolverParameterList = optimizationSolverSpecElement.getChild(OptimizationListOfParametersTag, ns);
        if (optimizationSolverParameterList != null) {
            List<Element> listOfSolverParams = optimizationSolverParameterList.getChildren(OptimizationParameterTag, ns);
            CopasiOptimizationMethod copasiOptMethod = null;
            if (listOfSolverParams != null && listOfSolverParams.size() > 0) {
                List<CopasiOptimizationParameter> copasiSolverParams = new ArrayList<CopasiOptimizationParameter>();
                for (Element solverParam : listOfSolverParams) {
                    String paramName = solverParam.getAttributeValue(OptimizationParameterNameAttribute);
                    double paramValue = Double.parseDouble(solverParam.getAttributeValue(OptimizationParameterValueAttribute));
                    CopasiOptimizationParameter copasiParam = new CopasiOptimizationParameter(getCopasiOptimizationParameterTypeByName(paramName), paramValue);
                    copasiSolverParams.add(copasiParam);
                }
                copasiOptMethod = new CopasiOptimizationMethod(getCopasiOptimizationMethodTypeByName(optimizationSolverTypeName), copasiSolverParams.toArray(new CopasiOptimizationParameter[copasiSolverParams.size()]));
            } else // no parameters
            {
                copasiOptMethod = new CopasiOptimizationMethod(getCopasiOptimizationMethodTypeByName(optimizationSolverTypeName), new CopasiOptimizationParameter[0]);
            }
            optSolverSpec = new OptimizationSolverSpec(copasiOptMethod);
            // add number of runs attribute
            String numOfRunsStr = optimizationSolverSpecElement.getAttributeValue(OptimizationSolverNumOfRunsAttribute);
            if (numOfRunsStr != null) {
                int numOfRuns = Integer.parseInt(numOfRunsStr);
                optSolverSpec.setNumOfRuns(numOfRuns);
            }
        }
        parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
    }
    if (// optimization solver spec is null create a default copasi evolutionary programming
    optimizationSolverSpecElement == null || parameterEstimationTask.getOptimizationSolverSpec() == null) {
        OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(new CopasiOptimizationMethod(CopasiOptimizationMethodType.EvolutionaryProgram));
        parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
    }
    // read optimization solver result set
    Element optimizationResultSetElement = parameterEstimationTaskElement.getChild(OptXmlTags.OptimizationResultSet_Tag, ns);
    if (optimizationResultSetElement != null) {
        OptimizationResultSet optResultSet = null;
        // read optsolverResultSet
        if (optimizationResultSetElement.getChild(OptXmlTags.bestOptRunResultSet_Tag, ns) != null) {
            Element optSolverResultSetElement = optimizationResultSetElement.getChild(OptXmlTags.bestOptRunResultSet_Tag, ns);
            OptSolverResultSet optSolverResultSet = null;
            // get best parameters, best func value, number of evaluations and construct an optRunResultSet
            Element paramListElement = optSolverResultSetElement.getChild(OptimizationListOfParametersTag, ns);
            OptRunResultSet optRunResultSet = null;
            List<String> paramNames = new ArrayList<String>();
            List<Double> paramValues = new ArrayList<Double>();
            if (paramListElement != null && !paramListElement.getChildren().isEmpty()) {
                List<Element> paramElements = paramListElement.getChildren(OptimizationParameterTag, ns);
                if (paramElements != null) {
                    for (Element paramElement : paramElements) {
                        String paramName = paramElement.getAttributeValue(OptimizationParameterNameAttribute);
                        double paramValue = Double.parseDouble(paramElement.getAttributeValue(OptimizationParameterValueAttribute));
                        paramNames.add(paramName);
                        paramValues.add(paramValue);
                    }
                }
            }
            Element bestFuncValueElement = optSolverResultSetElement.getChild(OptXmlTags.ObjectiveFunction_Tag, ns);
            double bestFuncValue = Double.parseDouble(bestFuncValueElement.getAttributeValue(OptimizationParameterValueAttribute));
            Element numEvaluationsElement = optSolverResultSetElement.getChild(OptXmlTags.OptSolverResultSetFunctionEvaluations_Tag, ns);
            long numEvaluations = Long.parseLong(numEvaluationsElement.getAttributeValue(OptimizationParameterValueAttribute));
            // change List<Double> to double[]
            double[] values = new double[paramValues.size()];
            int index = 0;
            for (Double value : paramValues) {
                values[index++] = value;
            }
            optRunResultSet = new OptRunResultSet(values, bestFuncValue, numEvaluations, null);
            // create optSolverResultSet
            optSolverResultSet = new OptSolverResultSet(paramNames.toArray(new String[paramNames.size()]), optRunResultSet);
            // create optimization result set
            optResultSet = new OptimizationResultSet(optSolverResultSet, null);
        }
        parameterEstimationTask.setOptimizationResultSet(optResultSet);
    }
    return parameterEstimationTask;
}
Also used : OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) Element(org.jdom.Element) ArrayList(java.util.ArrayList) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) CopasiOptimizationMethod(cbit.vcell.opt.CopasiOptimizationMethod) CopasiOptimizationParameter(cbit.vcell.opt.CopasiOptimizationParameter) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) Vector(java.util.Vector) Namespace(org.jdom.Namespace) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ReferenceData(cbit.vcell.opt.ReferenceData) Parameter(cbit.vcell.model.Parameter) CopasiOptimizationParameter(cbit.vcell.opt.CopasiOptimizationParameter) CommentStringTokenizer(org.vcell.util.CommentStringTokenizer) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)

Example 39 with Namespace

use of org.jdom.Namespace in project vcell by virtualcell.

the class XMLUtils method getXPathForAttributeIdentifiedByAttribute.

/**
 * @param el
 *            An {@link Element} that is attached to its hierarchy.
 * @param toIdentify
 *            the attribute for the XPath string
 * @param doc
 *            the Document to parse
 * @param identifier
 *            the Attribute identifier
 * @return the XPAth string
 * @throws IOException
 * @throws JDOMException
 */
public String getXPathForAttributeIdentifiedByAttribute(Element el, Attribute toIdentify, Document doc, Attribute identifier) throws JDOMException, IOException {
    Map<Namespace, String> ns2Prefix = getNamespacesWithPrefixes(doc);
    Stack<Element> s = buildElementStack(el);
    StringBuffer out = new StringBuffer();
    while (!s.isEmpty()) {
        Element el2 = s.pop();
        out.append("/").append(ns2Prefix.get(el2.getNamespace()) + ":" + el2.getName());
        if (s.size() == 0) {
            out.append("[@").append(getAttStrForAtt(identifier)).append("='").append(identifier.getValue()).append("']").append("/@").append(getAttStrForAtt(toIdentify));
        } else {
            addIdentifiersToXPath(out, el2, ns2Prefix);
        }
    }
    return out.toString();
}
Also used : Element(org.jdom.Element) Namespace(org.jdom.Namespace)

Example 40 with Namespace

use of org.jdom.Namespace in project vcell by virtualcell.

the class XMLUtils method getXPathForElementIdentifiedByAttribute.

/**
 * @param el
 *            An {@link Element} that is attached to its hierarchy.
 * @param doc
 *            attribute
 * @param selected
 * @return The XPath String
 * @throws IOException
 * @throws JDOMException
 */
public String getXPathForElementIdentifiedByAttribute(Element el, Document doc, Attribute selected) {
    Map<Namespace, String> ns2Prefix = getNamespacesWithPrefixes(doc);
    Stack<Element> elementStack = buildElementStack(el);
    StringBuffer out = new StringBuffer();
    while (!elementStack.isEmpty()) {
        Element el2 = elementStack.pop();
        out.append("/").append(ns2Prefix.get(el2.getNamespace()) + ":" + el2.getName());
        if (elementStack.size() == 0) {
            out.append("[@").append(getAttStrForAtt(selected)).append("='").append(selected.getValue()).append("']");
        } else {
            addIdentifiersToXPath(out, el2, ns2Prefix);
        }
    }
    return out.toString();
}
Also used : Element(org.jdom.Element) Namespace(org.jdom.Namespace)

Aggregations

Namespace (org.jdom.Namespace)102 Element (org.jdom.Element)85 IOException (java.io.IOException)24 XConfiguration (org.apache.oozie.util.XConfiguration)19 StringReader (java.io.StringReader)15 Configuration (org.apache.hadoop.conf.Configuration)15 JDOMException (org.jdom.JDOMException)15 ActionExecutorException (org.apache.oozie.action.ActionExecutorException)13 ArrayList (java.util.ArrayList)12 Path (org.apache.hadoop.fs.Path)11 Document (org.jdom.Document)11 List (java.util.List)9 File (java.io.File)7 HashMap (java.util.HashMap)6 CoordinatorJobBean (org.apache.oozie.CoordinatorJobBean)6 SAXBuilder (org.jdom.input.SAXBuilder)6 XMLOutputter (org.jdom.output.XMLOutputter)6 Writer (java.io.Writer)5 Attribute (org.jdom.Attribute)5 Format (org.jdom.output.Format)5