Search in sources :

Example 6 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project as2-server by phax.

the class AddPartnershipCommand method execute.

@Override
public CommandResult execute(final IPartnershipFactoryWithPartners partFx, final Object[] params) throws OpenAS2Exception {
    if (params.length < 3)
        return new CommandResult(ECommandResultType.TYPE_INVALID_PARAM_COUNT, getUsage());
    final IMicroDocument doc = new MicroDocument();
    final IMicroElement root = doc.appendElement("partnership");
    for (int nIndex = 0; nIndex < params.length; nIndex++) {
        final String param = (String) params[nIndex];
        final int pos = param.indexOf('=');
        if (nIndex == 0) {
            root.setAttribute("name", param);
        } else if (nIndex == 1) {
            final IMicroElement elem = root.appendElement("sender");
            elem.setAttribute("name", param);
        } else if (nIndex == 2) {
            final IMicroElement elem = root.appendElement("receiver");
            elem.setAttribute("name", param);
        } else if (pos == 0) {
            return new CommandResult(ECommandResultType.TYPE_ERROR, "incoming parameter missing name");
        } else if (pos > 0) {
            final IMicroElement elem = root.appendElement("attribute");
            elem.setAttribute("name", param.substring(0, pos));
            elem.setAttribute("value", param.substring(pos + 1));
        } else
            return new CommandResult(ECommandResultType.TYPE_ERROR, "incoming parameter missing value");
    }
    final XMLPartnershipFactory aXMLPartnershipFactory = (XMLPartnershipFactory) partFx;
    final Partnership aPartnership = aXMLPartnershipFactory.loadPartnership(root, aXMLPartnershipFactory.getPartnerMap());
    if (aXMLPartnershipFactory.getPartnershipByName(aPartnership.getName()) != null)
        return new CommandResult(ECommandResultType.TYPE_ERROR, "A partnership with name '" + aPartnership.getName() + "' is already present!");
    // add the partnership to the list of available partnerships
    partFx.addPartnership(aPartnership);
    return new CommandResult(ECommandResultType.TYPE_OK);
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) Partnership(com.helger.as2lib.partner.Partnership) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) XMLPartnershipFactory(com.helger.as2lib.partner.xml.XMLPartnershipFactory) CommandResult(com.helger.as2.cmd.CommandResult)

Example 7 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project ph-schematron by phax.

the class PSPreprocessorTest method testWithTitle.

@Test
public void testWithTitle() throws SchematronException {
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance()).setKeepTitles(true).setKeepDiagnostics(true);
    final IReadableResource aRes = new ClassPathResource("test-sch/example-3-5.sch");
    final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aRes);
    final PSReader aReader = new PSReader(aRes);
    final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    assertNotNull(aPreprocessedSchema);
    assertTrue(aPreprocessedSchema.isValid(new DoNothingPSErrorHandler()));
    // Because titles are not in minimal mode
    assertFalse(aPreprocessedSchema.isMinimal());
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) PSReader(com.helger.schematron.pure.exchange.PSReader) PSSchema(com.helger.schematron.pure.model.PSSchema) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 8 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project ph-schematron by phax.

the class PSPreprocessorTest method testBasic.

@Test
public void testBasic() throws Exception {
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
    for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
        // Resolve all includes
        final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aRes);
        assertNotNull(aDoc);
        // Read to domain object
        final PSReader aReader = new PSReader(aRes);
        final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
        assertNotNull(aSchema);
        // Ensure the schema is valid
        final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler();
        assertTrue(aRes.getPath(), aSchema.isValid(aErrHdl));
        assertTrue(aErrHdl.isEmpty());
        // Convert to minified schema if not-yet minimal
        final PSSchema aPreprocessedSchema = aPreprocessor.getAsMinimalSchema(aSchema);
        assertNotNull(aPreprocessedSchema);
        if (false) {
            final String sXML = MicroWriter.getNodeAsString(aPreprocessedSchema.getAsMicroElement());
            SimpleFileIO.writeFile(new File("test-minified", FilenameHelper.getWithoutPath(aRes.getPath()) + ".min-pure.sch"), sXML, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
        }
        // Ensure it is still valid and minimal
        assertTrue(aRes.getPath(), aPreprocessedSchema.isValid(aErrHdl));
        assertTrue(aRes.getPath(), aPreprocessedSchema.isMinimal());
    }
}
Also used : IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) PSReader(com.helger.schematron.pure.exchange.PSReader) PSSchema(com.helger.schematron.pure.model.PSSchema) File(java.io.File) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Example 9 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project ph-schematron by phax.

the class SchematronTestHelper method _readDI.

@Nonnull
private static ICommonsList<SchematronTestFile> _readDI(@Nonnull final IReadableResource aRes) {
    if (false)
        ClassPathHelper.getAllClassPathEntries().forEach(x -> {
            System.out.println(x);
            if (new File(x).isDirectory()) {
                final FileSystemRecursiveIterator it = new FileSystemRecursiveIterator(new File(x));
                it.forEach(y -> System.out.println(StringHelper.getRepeated("  ", it.getLevel()) + y));
            }
        });
    ValueEnforcer.notNull(aRes, "Resource");
    ValueEnforcer.isTrue(aRes.exists(), () -> "Resource " + aRes + " does not exist!");
    final ICommonsList<SchematronTestFile> ret = new CommonsArrayList<>();
    final IMicroDocument aDoc = MicroReader.readMicroXML(aRes);
    if (aDoc == null)
        throw new IllegalArgumentException("Failed to open/parse " + aRes + " as XML");
    String sLastParentDirBaseName = null;
    for (final IMicroElement eItem : aDoc.getDocumentElement().getAllChildElements()) if (eItem.getTagName().equals("directory"))
        sLastParentDirBaseName = eItem.getAttributeValue("basename");
    else if (eItem.getTagName().equals("file"))
        ret.add(new SchematronTestFile(sLastParentDirBaseName, new ClassPathResource(eItem.getAttributeValue("name")), eItem.getAttributeValue("basename")));
    else
        throw new IllegalArgumentException("Cannot handle " + eItem);
    return ret;
}
Also used : CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathHelper(com.helger.commons.lang.ClassPathHelper) StringHelper(com.helger.commons.string.StringHelper) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) File(java.io.File) ValueEnforcer(com.helger.commons.ValueEnforcer) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Nonempty(com.helger.commons.annotation.Nonempty) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroReader(com.helger.xml.microdom.serialize.MicroReader) Nonnull(javax.annotation.Nonnull) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) File(java.io.File) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Nonnull(javax.annotation.Nonnull)

Example 10 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project ph-schematron by phax.

the class SchematronHelper method getWithResolvedSchematronIncludes.

/**
 * Resolve all Schematron includes of the passed resource.
 *
 * @param aResource
 *        The Schematron resource to read. May not be <code>null</code>.
 * @param aSettings
 *        The SAX reader settings to be used. May be <code>null</code> to use
 *        the default settings.
 * @param aErrorHandler
 *        The error handler to be used. May not be <code>null</code>.
 * @return <code>null</code> if the passed resource could not be read as XML
 *         document
 */
@Nullable
public static IMicroDocument getWithResolvedSchematronIncludes(@Nonnull final IReadableResource aResource, @Nullable final ISAXReaderSettings aSettings, @Nonnull final IPSErrorHandler aErrorHandler) {
    final InputSource aIS = InputSourceFactory.create(aResource);
    final IMicroDocument aDoc = MicroReader.readMicroXML(aIS, aSettings);
    if (aDoc != null) {
        // Resolve all Schematron includes
        if (_recursiveResolveAllSchematronIncludes(aDoc.getDocumentElement(), aResource, aSettings, aErrorHandler).isFailure()) {
            // Error resolving includes
            return null;
        }
    }
    return aDoc;
}
Also used : InputSource(org.xml.sax.InputSource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Nullable(javax.annotation.Nullable)

Aggregations

IMicroDocument (com.helger.xml.microdom.IMicroDocument)14 IMicroElement (com.helger.xml.microdom.IMicroElement)7 IReadableResource (com.helger.commons.io.resource.IReadableResource)6 Nonnull (javax.annotation.Nonnull)4 Test (org.junit.Test)4 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)3 PSReader (com.helger.schematron.pure.exchange.PSReader)3 PSSchema (com.helger.schematron.pure.model.PSSchema)3 MicroDocument (com.helger.xml.microdom.MicroDocument)3 File (java.io.File)3 CommandResult (com.helger.as2.cmd.CommandResult)2 OpenAS2Exception (com.helger.as2lib.exception.OpenAS2Exception)2 XMLPartnershipFactory (com.helger.as2lib.partner.xml.XMLPartnershipFactory)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 IMicroNode (com.helger.xml.microdom.IMicroNode)2 Partnership (com.helger.as2lib.partner.Partnership)1 Partner (com.helger.as2lib.partner.xml.Partner)1 ValueEnforcer (com.helger.commons.ValueEnforcer)1 Nonempty (com.helger.commons.annotation.Nonempty)1 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1