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);
}
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());
}
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());
}
}
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;
}
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;
}
Aggregations