use of com.helger.xml.serialize.write.XMLWriterSettings in project ph-schematron by phax.
the class Schematron2XSLTMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
StaticLoggerBinder.getSingleton().setMavenLog(getLog());
if (m_aSchematronDirectory == null)
throw new MojoExecutionException("No Schematron directory specified!");
if (m_aSchematronDirectory.exists() && !m_aSchematronDirectory.isDirectory())
throw new MojoExecutionException("The specified Schematron directory " + m_aSchematronDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sSchematronPattern))
throw new MojoExecutionException("No Schematron pattern specified!");
if (m_aXsltDirectory == null)
throw new MojoExecutionException("No XSLT directory specified!");
if (m_aXsltDirectory.exists() && !m_aXsltDirectory.isDirectory())
throw new MojoExecutionException("The specified XSLT directory " + m_aXsltDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sXsltExtension) || !m_sXsltExtension.startsWith("."))
throw new MojoExecutionException("The XSLT extension '" + m_sXsltExtension + "' is invalid!");
if (!m_aXsltDirectory.exists() && !m_aXsltDirectory.mkdirs())
throw new MojoExecutionException("Failed to create the XSLT directory " + m_aXsltDirectory);
// for all Schematron files that match the pattern
final DirectoryScanner aScanner = new DirectoryScanner();
aScanner.setBasedir(m_aSchematronDirectory);
aScanner.setIncludes(new String[] { m_sSchematronPattern });
aScanner.setCaseSensitive(true);
aScanner.scan();
final String[] aFilenames = aScanner.getIncludedFiles();
if (aFilenames != null) {
for (final String sFilename : aFilenames) {
final File aFile = new File(m_aSchematronDirectory, sFilename);
// 1. build XSLT file name (outputdir + localpath with new extension)
final File aXSLTFile = new File(m_aXsltDirectory, FilenameHelper.getWithoutExtension(sFilename) + m_sXsltExtension);
getLog().info("Converting Schematron file '" + aFile.getPath() + "' to XSLT file '" + aXSLTFile.getPath() + "'");
// 2. The Schematron resource
final IReadableResource aSchematronResource = new FileSystemResource(aFile);
// 3. Check if the XSLT file already exists
if (aXSLTFile.exists() && !m_bOverwriteWithoutNotice) {
// 3.1 Not overwriting the existing file
getLog().debug("Skipping XSLT file '" + aXSLTFile.getPath() + "' because it already exists!");
} else {
// 3.2 Create the directory, if necessary
final File aXsltFileDirectory = aXSLTFile.getParentFile();
if (aXsltFileDirectory != null && !aXsltFileDirectory.exists()) {
getLog().debug("Creating directory '" + aXsltFileDirectory.getPath() + "'");
if (!aXsltFileDirectory.mkdirs()) {
final String message = "Failed to convert '" + aFile.getPath() + "' because directory '" + aXsltFileDirectory.getPath() + "' could not be created";
getLog().error(message);
throw new MojoFailureException(message);
}
}
// 3.3 Okay, write the XSLT file
try {
buildContext.removeMessages(aFile);
// Custom error listener to log to the Mojo logger
final ErrorListener aMojoErrorListener = new PluginErrorListener(aFile);
// Custom error listener
// No custom URI resolver
// Specified phase - default = null
// Specified language code - default = null
final SCHTransformerCustomizer aCustomizer = new SCHTransformerCustomizer().setErrorListener(aMojoErrorListener).setPhase(m_sPhaseName).setLanguageCode(m_sLanguageCode).setParameters(m_aCustomParameters);
final ISchematronXSLTBasedProvider aXsltProvider = SchematronResourceSCHCache.createSchematronXSLTProvider(aSchematronResource, aCustomizer);
if (aXsltProvider != null) {
// Write the resulting XSLT file to disk
final MapBasedNamespaceContext aNSContext = new MapBasedNamespaceContext().addMapping("svrl", CSVRL.SVRL_NAMESPACE_URI);
// Add all namespaces from XSLT document root
final String sNSPrefix = XMLConstants.XMLNS_ATTRIBUTE + ":";
XMLHelper.forAllAttributes(aXsltProvider.getXSLTDocument().getDocumentElement(), (sAttrName, sAttrValue) -> {
if (sAttrName.startsWith(sNSPrefix))
aNSContext.addMapping(sAttrName.substring(sNSPrefix.length()), sAttrValue);
});
final XMLWriterSettings aXWS = new XMLWriterSettings();
aXWS.setNamespaceContext(aNSContext).setPutNamespaceContextPrefixesInRoot(true);
final OutputStream aOS = FileHelper.getOutputStream(aXSLTFile);
if (aOS == null)
throw new IllegalStateException("Failed to open output stream for file " + aXSLTFile.getAbsolutePath());
XMLWriter.writeToStream(aXsltProvider.getXSLTDocument(), aOS, aXWS);
getLog().debug("Finished creating XSLT file '" + aXSLTFile.getPath() + "'");
buildContext.refresh(aXsltFileDirectory);
} else {
final String message = "Failed to convert '" + aFile.getPath() + "': the Schematron resource is invalid";
getLog().error(message);
throw new MojoFailureException(message);
}
} catch (final MojoFailureException up) {
throw up;
} catch (final Exception ex) {
final String sMessage = "Failed to convert '" + aFile.getPath() + "' to XSLT file '" + aXSLTFile.getPath() + "'";
getLog().error(sMessage, ex);
throw new MojoExecutionException(sMessage, ex);
}
}
}
}
}
use of com.helger.xml.serialize.write.XMLWriterSettings in project ph-schematron by phax.
the class Issue48Test method validateAndProduceSVRL.
public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematron)).readSchema();
final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
final String sSCH = new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).getXMLString(aPreprocessedSchema);
if (false)
System.out.println(sSCH);
final SchematronResourceSCH aSCH = new SchematronResourceSCH(new ReadableResourceString(sSCH, StandardCharsets.UTF_8));
// Perform validation
final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
assertNotNull(aSVRL);
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
use of com.helger.xml.serialize.write.XMLWriterSettings in project ph-css by phax.
the class MainCreateSupportedCSSPropertiesFile method main.
public static void main(final String[] args) {
final Locale aLocale = Locale.US;
final IMicroElement html = new MicroElement("html");
final IMicroElement head = html.appendElement("head");
head.appendElement("title").appendText("Supported CSS properties in ph-css");
head.appendElement("style").appendText("* {font-family:Arial,Helvetica;}" + " table{border-collapse:collapse;}" + " td,th {border:solid 1px black;padding:3px;vertical-align:top; }" + " .odd{background-color:#ddd;}" + " .center{text-align:center;}" + " .nowrap{white-space:nowrap;}" + " a, a:link, a:visited, a:hover, a:active{color:blue;}");
final IMicroElement body = html.appendElement("body");
body.appendElement("div").appendText("Automatically generated by " + ClassHelper.getClassLocalName(MainCreateSupportedCSSPropertiesFile.class) + " on " + new Date().toString());
body.appendElement("div").appendElement("a").setAttribute("href", "#generic").appendText("Generic properties");
body.appendElement("div").appendElement("a").setAttribute("href", "#vendor").appendText("Vendor specific properties");
body.appendElement("a").setAttribute("name", "generic").appendText("");
body.appendElement("h1").appendText("Generic properties");
IMicroElement table = body.appendElement("table");
IMicroElement thead = table.appendElement("thead");
IMicroElement tr = thead.appendElement("tr");
tr.appendElement("th").appendText("Name");
tr.appendElement("th").appendText("CSS 1.0");
tr.appendElement("th").appendText("CSS 2.1");
tr.appendElement("th").appendText("CSS 3.0");
tr.appendElement("th").appendText("Links");
IMicroElement tbody = table.appendElement("tbody");
int nIndex = 0;
for (final ECSSProperty eProperty : CollectionHelper.getSorted(ECSSProperty.values(), IHasName.getComparatorName())) if (!eProperty.isVendorSpecific()) {
final Version eMinVersion = eProperty.getMinimumCSSVersion().getVersion();
final boolean bCSS10 = eMinVersion.isLE(ECSSVersion.CSS10.getVersion());
final boolean bCSS21 = eMinVersion.isLE(ECSSVersion.CSS21.getVersion());
final boolean bCSS30 = eMinVersion.isLE(ECSSVersion.CSS30.getVersion());
tr = tbody.appendElement("tr");
if ((nIndex & 1) == 1)
tr.setAttribute("class", "odd");
tr.appendElement("td").setAttribute("class", "nowrap").appendText(eProperty.getName());
_boolean(tr.appendElement("td"), bCSS10, null);
_boolean(tr.appendElement("td"), bCSS21, null);
_boolean(tr.appendElement("td"), bCSS30, null);
final IMicroElement td = tr.appendElement("td");
for (final ECSSSpecification eSpecs : eProperty.getAllSpecifications()) if (eSpecs.hasHandledURL())
td.appendElement("div").appendElement("a").setAttribute("href", eSpecs.getHandledURL()).setAttribute("target", "_blank").appendText(eSpecs.getID());
else
td.appendElement("div").appendText(eSpecs.getID());
++nIndex;
}
// Determine all used vendor prefixes
final EnumSet<ECSSVendorPrefix> aRequiredPrefixes = EnumSet.noneOf(ECSSVendorPrefix.class);
for (final ECSSVendorPrefix eVendorPrefix : ECSSVendorPrefix.values()) {
for (final ECSSProperty eProperty : ECSSProperty.values()) if (eProperty.isVendorSpecific(eVendorPrefix)) {
aRequiredPrefixes.add(eVendorPrefix);
break;
}
}
body.appendElement("a").setAttribute("name", "vendor").appendText("");
body.appendElement("h1").appendText("Vendor specific properties");
table = body.appendElement("table");
thead = table.appendElement("thead");
tr = thead.appendElement("tr");
tr.appendElement("th").appendText("Name");
for (final ECSSVendorPrefix e : aRequiredPrefixes) {
final IMicroElement th = tr.appendElement("th");
th.appendText(e.getDisplayName());
th.appendElement("span").setAttribute("class", "nowrap").appendText(" (" + e.getPrefix() + ")");
}
tbody = table.appendElement("tbody");
nIndex = 0;
for (final ECSSProperty eProperty : CollectionHelper.getSorted(ECSSProperty.values(), IComparator.getComparatorCollating(ECSSProperty::getVendorIndependentName, aLocale))) if (eProperty.isVendorSpecific()) {
tr = tbody.appendElement("tr");
if ((nIndex & 1) == 1)
tr.setAttribute("class", "odd");
tr.appendElement("td").setAttribute("class", "nowrap").appendText(eProperty.getVendorIndependentName());
for (final ECSSVendorPrefix e : aRequiredPrefixes) _boolean(tr.appendElement("td"), eProperty.isVendorSpecific(e), eProperty.getName());
++nIndex;
}
body.appendElement("div").setAttribute("style", "margin:2em 0").appendText("That's it.");
String sHTML = "<!--\r\n" + "\r\n" + " Copyright (C) 2014 Philip Helger (www.helger.com)\r\n" + " philip[at]helger[dot]com\r\n" + "\r\n" + " Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + " you may not use this file except in compliance with the License.\r\n" + " You may obtain a copy of the License at\r\n" + "\r\n" + " http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + " Unless required by applicable law or agreed to in writing, software\r\n" + " distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + " See the License for the specific language governing permissions and\r\n" + " limitations under the License.\r\n" + "\r\n" + "-->\r\n";
sHTML += MicroWriter.getNodeAsString(html, new XMLWriterSettings().setIndent(EXMLSerializeIndent.ALIGN_ONLY).setSerializeVersion(EXMLSerializeVersion.HTML));
SimpleFileIO.writeFile(new File("src/main/resources/supported-css-properties.html"), sHTML, StandardCharsets.UTF_8);
System.out.println("Done");
}
use of com.helger.xml.serialize.write.XMLWriterSettings in project ph-schematron by phax.
the class PSWriterSettings method setXMLWriterSettings.
@Nonnull
public IPSWriterSettings setXMLWriterSettings(@Nonnull final IXMLWriterSettings aXMLWriterSettings) {
ValueEnforcer.notNull(aXMLWriterSettings, "XMLWriterSettings");
m_aXMLWriterSettings = new XMLWriterSettings(aXMLWriterSettings);
return this;
}
use of com.helger.xml.serialize.write.XMLWriterSettings in project ph-schematron by phax.
the class SchematronPreprocess method execute.
@Override
public void execute() throws BuildException {
boolean bCanRun = false;
if (m_aSrcFile == null)
_error("No source Schematron file specified!");
else if (m_aSrcFile.exists() && !m_aSrcFile.isFile())
_error("The specified source Schematron file " + m_aSrcFile + " is not a file!");
else if (m_aDstFile == null)
_error("No destination Schematron file specified!");
else if (m_aDstFile.exists() && !m_aDstFile.isFile())
_error("The specified destination Schematron file " + m_aDstFile + " is not a file!");
else
bCanRun = true;
if (bCanRun)
try {
// Read source
final PSSchema aSchema = new PSReader(new FileSystemResource(m_aSrcFile)).readSchema();
// Setup preprocessor
final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
aPreprocessor.setKeepTitles(m_bKeepTitles);
aPreprocessor.setKeepDiagnostics(m_bKeepDiagnostics);
aPreprocessor.setKeepReports(m_bKeepReports);
aPreprocessor.setKeepEmptyPatterns(m_bKeepEmptyPatterns);
aPreprocessor.setKeepEmptySchema(true);
// Main pre-processing
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
// Write the result file
new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).writeToFile(aPreprocessedSchema, m_aDstFile);
log("Successfully pre-processed Schematron " + m_aSrcFile + " to " + m_aDstFile);
} catch (final SchematronReadException | SchematronPreprocessException ex) {
_error("Error processing Schemtron " + m_aSrcFile.getAbsolutePath(), ex);
}
}
Aggregations