Search in sources :

Example 56 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project mdw-designer by CenturyLinkCloud.

the class NewTaskTemplateWizard method performFinish.

@Override
public boolean performFinish() {
    TaskTemplate taskTemplate = (TaskTemplate) getWorkflowAsset();
    try {
        TaskTemplateDocument doc;
        if (isImportFile()) {
            // load from selected file
            doc = TaskTemplateDocument.Factory.parse(new File(getImportFilePath()));
            com.centurylink.mdw.task.TaskTemplate template = doc.getTaskTemplate();
            // selection)
            if (template.getLogicalId() == null)
                throw new XmlException("Task template missing logicalId");
            else if (template.getCategory() == null)
                throw new XmlException("Task template missing category");
            else if (template.getName() == null || template.getName().isEmpty())
                throw new XmlException("Task template missing name");
        } else {
            doc = TaskTemplateDocument.Factory.newInstance();
            com.centurylink.mdw.task.TaskTemplate template = doc.addNewTaskTemplate();
            String taskName = taskTemplate.getName().substring(0, taskTemplate.getName().length() - 5);
            template.setLogicalId(taskName.replace(' ', '_'));
            template.setCategory("GEN");
            template.setName(taskName);
            if ("AUTOFORM".equals(taskTemplate.getLanguage())) {
                Attribute form = template.addNewAttribute();
                form.setName("FormName");
                form.setStringValue("Autoform");
            }
        }
        XmlOptions xmlOptions = new XmlOptions().setSaveAggressiveNamespaces();
        xmlOptions.setSavePrettyPrint().setSavePrettyPrintIndent(2);
        taskTemplate.setContent(doc.xmlText(xmlOptions));
        String templateName = taskTemplate.getName();
        taskTemplate.setTaskVO(new TaskVO(doc.getTaskTemplate()));
        taskTemplate.setName(templateName);
        taskTemplate.getTaskVO().setPackageName(taskTemplate.getPackage().getName());
        DesignerProxy designerProxy = taskTemplate.getProject().getDesignerProxy();
        designerProxy.createNewTaskTemplate(taskTemplate);
        if (designerProxy.getRunnerStatus().equals(RunnerStatus.SUCCESS)) {
            taskTemplate.openFile(new NullProgressMonitor());
            taskTemplate.addElementChangeListener(taskTemplate.getProject());
            taskTemplate.fireElementChangeEvent(ChangeType.ELEMENT_CREATE, taskTemplate);
            taskTemplate.setVersion(1);
            taskTemplate.fireElementChangeEvent(ChangeType.VERSION_CHANGE, taskTemplate);
            DesignerPerspective.promptForShowPerspective(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), taskTemplate);
            return true;
        } else {
            return false;
        }
    } catch (Exception ex) {
        PluginMessages.uiError(getShell(), ex, "Create " + taskTemplate.getTitle(), taskTemplate.getProject());
        return false;
    }
}
Also used : TaskTemplate(com.centurylink.mdw.plugin.designer.model.TaskTemplate) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TaskTemplateDocument(com.centurylink.mdw.task.TaskTemplateDocument) Attribute(com.centurylink.mdw.task.Attribute) XmlOptions(org.apache.xmlbeans.XmlOptions) DesignerProxy(com.centurylink.mdw.plugin.designer.DesignerProxy) XmlException(org.apache.xmlbeans.XmlException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TaskVO(com.centurylink.mdw.model.value.task.TaskVO)

Example 57 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class PMMLValidator method validatePMML.

/**
 * Validates a document against the schema and returns the number of
 * errors found.
 *
 * @param pmmlDocument the PMMML document
 * @return the error message or an em
 */
public static Map<String, String> validatePMML(final PMMLDocument pmmlDocument) {
    XmlOptions validateOptions = new XmlOptions();
    List<XmlError> errorList = new ArrayList<XmlError>();
    validateOptions.setErrorListener(errorList);
    pmmlDocument.validate(validateOptions);
    Map<String, String> errorMessages = new TreeMap<String, String>();
    for (XmlError error : errorList) {
        String location = error.getCursorLocation().xmlText();
        if (location.length() > 50) {
            location = location.substring(0, 50) + "[...]";
        }
        String errorMessage = error.getMessage().replace(PMML_NAMESPACE_URI, "");
        LOGGER.error(location + ": " + errorMessage);
        errorMessages.put(location, errorMessage);
    }
    return errorMessages;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) ArrayList(java.util.ArrayList) XmlError(org.apache.xmlbeans.XmlError) TreeMap(java.util.TreeMap)

Example 58 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class NodeDescriptionUtil method getPrettyXmlText.

/**
 * Obtain a String representation of the children of the given XML object. Intended for pretty-printing contents of
 * nodes that resemble HTML-formatted text. Strip comments, namespace prefixes and namespace attributes.
 * If the given String is {@code null}, the method returns {@code null}.
 *
 * @param obj the XML object whose child nodes should be printed to a String
 * @return a string representation of the nodes children, or null if {@code obj} is null.
 */
public static String getPrettyXmlText(final XmlObject obj) {
    if (obj == null) {
        return null;
    }
    var xmlOptions = // strip comments
    new XmlOptions().setLoadStripComments().setLoadStripProcinsts();
    var writer = new StringWriter();
    try {
        REMOVE_NAMESPACES_TRANSFORMER.get().transform(new StreamSource(obj.newReader(xmlOptions)), new StreamResult(writer));
    } catch (TransformerException | ConcurrentException e) {
        NodeLogger.getLogger(NodeDescriptionUtil.class).error("Could not format text, reason: " + e.getMessage(), e);
        e.printStackTrace();
    }
    var s = writer.toString();
    // On Windows, the transformer produces CRLF (\r\n) line endings, on Unix only LF (\n)
    if (Platform.OS_WIN32.equals(Platform.getOS())) {
        s = CRLF.matcher(s).replaceAll("\n");
    }
    return NodeDescriptionUtil.stripXmlFragment(s);
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XmlOptions(org.apache.xmlbeans.XmlOptions) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException) ConcurrentException(org.apache.commons.lang3.concurrent.ConcurrentException)

Example 59 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class NodeDescription13Proxy method validate.

/**
 * Validate against the XML Schema. If violations are found they are reported via the logger as coding problems.
 *
 * @return <code>true</code> if the document is valid, <code>false</code> otherwise
 */
final boolean validate() {
    // this method has default visibility so that we can use it in testcases
    XmlOptions options = new XmlOptions(OPTIONS);
    List<XmlError> errors = new ArrayList<XmlError>();
    options.setErrorListener(errors);
    boolean valid = m_document.validate(options);
    if (!valid) {
        logger.coding("Node description of '" + m_document.getKnimeNode().getName() + "' does not conform to the Schema. Violations follow.");
        for (XmlError err : errors) {
            logger.coding(err.toString());
        }
    }
    return valid;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) ArrayList(java.util.ArrayList) XmlError(org.apache.xmlbeans.XmlError)

Example 60 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class NodeDescription28Proxy method validate.

/**
 * Validate against the XML Schema. If violations are found they are reported via the logger as coding problems.
 *
 * @return <code>true</code> if the document is valid, <code>false</code> otherwise
 */
protected final boolean validate() {
    // this method has default visibility so that we can use it in testcases
    XmlOptions options = new XmlOptions(OPTIONS);
    List<XmlError> errors = new ArrayList<XmlError>();
    options.setErrorListener(errors);
    boolean valid = m_document.validate(options);
    if (!valid) {
        logger.coding("Node description of '" + m_document.getKnimeNode().getName() + "' does not conform to the Schema. Violations follow.");
        for (XmlError err : errors) {
            logger.coding(err.toString());
        }
    }
    return valid;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) ArrayList(java.util.ArrayList) XmlError(org.apache.xmlbeans.XmlError)

Aggregations

XmlOptions (org.apache.xmlbeans.XmlOptions)63 QName (javax.xml.namespace.QName)20 OutputStream (java.io.OutputStream)18 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)17 ArrayList (java.util.ArrayList)12 XmlError (org.apache.xmlbeans.XmlError)11 XmlException (org.apache.xmlbeans.XmlException)11 IOException (java.io.IOException)9 Beta (org.apache.poi.util.Beta)7 Before (org.junit.Before)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 XmlObject (org.apache.xmlbeans.XmlObject)4 PackageDocument (com.centurylink.mdw.bpm.PackageDocument)3 PackageVO (com.centurylink.mdw.model.value.process.PackageVO)3 TaskVO (com.centurylink.mdw.model.value.task.TaskVO)3 EncodingException (org.n52.svalbard.encode.exception.EncodingException)3 MDWProcessDefinition (com.centurylink.mdw.bpm.MDWProcessDefinition)2 ProcessDefinitionDocument (com.centurylink.mdw.bpm.ProcessDefinitionDocument)2 AttributeVO (com.centurylink.mdw.model.value.attribute.AttributeVO)2 Attribute (com.centurylink.mdw.task.Attribute)2