Skip to content
This repository has been archived by the owner on Dec 30, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
ODFTOOLKIT-478 - Adding namespaces to manifest and digital signature …
…DOM by fix & refactoring of generator
  • Loading branch information
svanteschubert committed Oct 2, 2018
1 parent 971c54f commit 99975f3
Show file tree
Hide file tree
Showing 35 changed files with 456 additions and 309 deletions.
11 changes: 8 additions & 3 deletions generator/schema2template/pom.xml
Expand Up @@ -32,10 +32,15 @@
<version>0.9.0-incubating-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.msv</groupId>
<artifactId>msv-core</artifactId>
Expand Down
Expand Up @@ -110,8 +110,10 @@ public class OdfHelper {
private static final String PYTHON_OUTPUT_FILES = "target" + File.separator + "python-output-files.xml";
private static final String DOM_OUTPUT_FILES_TEMPLATE = "dom-output-files.vm";
private static final String DOM_OUTPUT_FILES = "target" + File.separator + "dom-output-files.xml";
private static final String PKG_OUTPUT_FILES_TEMPLATE = "pkg-output-files.vm";
private static final String PKG_OUTPUT_FILES = "target" + File.separator + "pkg-output-files.xml";
private static final String PKG_MANIFEST_OUTPUT_FILES_TEMPLATE = "pkg-manifest-output-files.vm";
private static final String PKG_MANIFEST_OUTPUT_FILES = "target" + File.separator + "pkg-manifest-output-files.xml";
private static final String PKG_DSIG_OUTPUT_FILES_TEMPLATE = "pkg-dsig-output-files.vm";
private static final String PKG_DSIG_OUTPUT_FILES = "target" + File.separator + "pkg-dsig-output-files.xml";
private static XMLModel mOdf12SignatureSchemaModel;
private static XMLModel mOdf12ManifestSchemaModel;
private static XMLModel mOdf12SchemaModel;
Expand Down Expand Up @@ -154,24 +156,24 @@ public void start() throws Exception {
initialize();

// ODF 1.2 Code Generation
fillTemplates(odfDomResourceDir, mOdf12Root, DOM_OUTPUT_FILES_TEMPLATE, DOM_OUTPUT_FILES);
fillTemplates(odfPkgResourceDir, mOdf12SignatureRoot, PKG_OUTPUT_FILES_TEMPLATE, PKG_OUTPUT_FILES);
fillTemplates(odfPkgResourceDir, mOdf12ManifestRoot, PKG_OUTPUT_FILES_TEMPLATE, PKG_OUTPUT_FILES);
fillTemplates(odfDomResourceDir, mOdf12Root, DOM_OUTPUT_FILES_TEMPLATE, DOM_OUTPUT_FILES, mOdf12SchemaModel);
fillTemplates(odfPkgResourceDir, mOdf12ManifestRoot, PKG_MANIFEST_OUTPUT_FILES_TEMPLATE, PKG_MANIFEST_OUTPUT_FILES, mOdf12ManifestSchemaModel);
fillTemplates(odfPkgResourceDir, mOdf12SignatureRoot, PKG_DSIG_OUTPUT_FILES_TEMPLATE, PKG_DSIG_OUTPUT_FILES, mOdf12SignatureSchemaModel);
}

public static void main(String[] args) throws Exception {
LOG.info("Starting code generation:");
initialize();

// ODF 1.2 HTML Reference (yet without BNF nor images)
fillTemplates(odfReferenceResourceDir, mOdf12Root, REFERENCE_OUTPUT_FILES_TEMPLATE, REFERENCE_OUTPUT_FILES);
fillTemplates(odfReferenceResourceDir, mOdf12Root, REFERENCE_OUTPUT_FILES_TEMPLATE, REFERENCE_OUTPUT_FILES, mOdf12SchemaModel);
// ODF 1.2 Python (The generated Python source is from a former colleague and might not work any longer..)
fillTemplates(odfPythonResourceDir, mOdf12Root, PYTHON_OUTPUT_FILES_TEMPLATE, PYTHON_OUTPUT_FILES);
fillTemplates(odfPythonResourceDir, mOdf12Root, PYTHON_OUTPUT_FILES_TEMPLATE, PYTHON_OUTPUT_FILES, mOdf12SchemaModel);

// ODF 1.2 Code Generation
fillTemplates(odfDomResourceDir, mOdf12Root, DOM_OUTPUT_FILES_TEMPLATE, DOM_OUTPUT_FILES);
fillTemplates(odfPkgResourceDir, mOdf12ManifestRoot, PKG_OUTPUT_FILES_TEMPLATE, PKG_OUTPUT_FILES);
fillTemplates(odfPkgResourceDir, mOdf12SignatureRoot, PKG_OUTPUT_FILES_TEMPLATE, PKG_OUTPUT_FILES);
fillTemplates(odfDomResourceDir, mOdf12Root, DOM_OUTPUT_FILES_TEMPLATE, DOM_OUTPUT_FILES, mOdf12SchemaModel);
fillTemplates(odfPkgResourceDir, mOdf12ManifestRoot, PKG_MANIFEST_OUTPUT_FILES_TEMPLATE, PKG_MANIFEST_OUTPUT_FILES, mOdf12ManifestSchemaModel);
fillTemplates(odfPkgResourceDir, mOdf12SignatureRoot, PKG_DSIG_OUTPUT_FILES_TEMPLATE, PKG_DSIG_OUTPUT_FILES, mOdf12SignatureSchemaModel);
}

private static void initialize() throws Exception {
Expand Down Expand Up @@ -205,20 +207,20 @@ private static void initialize() throws Exception {
LOG.info("Finished initilization..");
}

private static void fillTemplates(String sourceDir, Expression root, String outputRuleTemplate, String outputRuleFile) throws Exception {
private static void fillTemplates(String sourceDir, Expression root, String outputRuleTemplate, String outputRuleFile, XMLModel model) throws Exception {
// intialising template engine (ie. Velocity)
Properties props = new Properties();
props.setProperty("file.resource.loader.path", sourceDir);
VelocityEngine ve = new VelocityEngine(props);
ve.init();

// Create output-files.xml
createOutputFileList(ve, outputRuleTemplate, outputRuleFile);
createOutputFileList(ve, outputRuleTemplate, outputRuleFile, model);
LOG.info("output-files.xml created done.");

// Process output-files.xml, create output files
LOG.fine("Processing output files... ");
processFileList(ve, root, outputRuleFile);
processFileList(ve, root, outputRuleFile, model);
LOG.fine("DONE.\n");
}

Expand Down Expand Up @@ -264,12 +266,26 @@ public static Expression loadSchemaODF12() throws Exception {
* @throws Exception
*/
public static Expression loadSchema(File rngFile) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);

SAXParserFactory saxFactory = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
saxFactory.setNamespaceAware(true);
saxFactory.setValidating(false);
try {
saxFactory.setXIncludeAware(false);
saxFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
// removing potential vulnerability: see https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing
saxFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
saxFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (Exception ex) {
Logger.getLogger(OdfHelper.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException();
}

// Parsing the Schema with MSV
String absolutePath = rngFile.getAbsolutePath();
com.sun.msv.reader.util.IgnoreController ignoreController = new com.sun.msv.reader.util.IgnoreController();
Expression root = RELAXNGReader.parse(absolutePath, factory, ignoreController).getTopLevel();
Expression root = RELAXNGReader.parse(absolutePath, saxFactory, ignoreController).getTopLevel();


if (root == null) {
Expand All @@ -280,9 +296,6 @@ public static Expression loadSchema(File rngFile) throws Exception {

private static VelocityContext getContext(String contextStr, String param) {
VelocityContext context = new VelocityContext();
context.put("signaturemodel", mOdf12SignatureSchemaModel);
context.put("manifestmodel", mOdf12ManifestSchemaModel);
context.put("model", mOdf12SchemaModel);
context.put("oldmodel", mOdf11SchemaModel);
context.put("odfmodel", mOdfModel);
context.put("javamodel", mJavaModel);
Expand All @@ -291,8 +304,9 @@ private static VelocityContext getContext(String contextStr, String param) {
return context;
}

private static void createOutputFileList(VelocityEngine ve, String template, String output) throws Exception {
private static void createOutputFileList(VelocityEngine ve, String template, String output, XMLModel model) throws Exception {
VelocityContext context = getContext(null, null);
context.put("model", model);
File parentPatch = new File(output).getParentFile();
if (!parentPatch.exists()) {
parentPatch.mkdirs();
Expand Down Expand Up @@ -328,7 +342,7 @@ private static void ensureParentFolders(File newFile) {
}
}

public static void processFileList(VelocityEngine ve, Expression root, String outputRuleFile) throws Exception {
public static void processFileList(VelocityEngine ve, Expression root, String outputRuleFile, XMLModel model) throws Exception {
File outputFiles = new File(outputRuleFile);
List<OutputFileListEntry> fl = OutputFileListHandler.readFileListFile(outputFiles);

Expand All @@ -337,15 +351,18 @@ public static void processFileList(VelocityEngine ve, Expression root, String ou
case PATH:
break;
case FILE:
LOG.log(Level.INFO, "Processing line{0}: Generating file {1}\n", new Object[]{f.getLineNumber(), generateFilename(f.getAttribute("path"))});
LOG.log(Level.INFO, "Processing line {0}: Generating file {1}\n", new Object[]{f.getLineNumber(), generateFilename(f.getAttribute("path"))});
String odfContextStr = f.getAttribute("context");
String param = f.getAttribute("param");
VelocityContext context = getContext(odfContextStr, param);
if (context == null) {
throw new RuntimeException("Error in output-files.xml, line " + f.getLineNumber() + ": no or invalid odf-scope");
}
}else{
context.put("model", model);
}

File out = new File(outputRoot + File.separator + generateFilename(f.getAttribute("path"))).getCanonicalFile();
LOG.info("Absolute path of generated file: " + out.getAbsolutePath());
ensureParentFolders(out);
FileWriter fileout = new FileWriter(out);
String encoding = "utf-8";
Expand Down
Expand Up @@ -109,8 +109,10 @@ public static NamespaceDictionary getStandardDictionary() {
dict.put("grddl","http://www.w3.org/2003/g/data-view#");
dict.put("xhtml","http://www.w3.org/1999/xhtml");
dict.put("smil","urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0");
dict.put("xml", "http://www.w3.org/XML/1998/namespace");

dict.put("xml", "http://www.w3.org/XML/1998/namespace");
dict.put("manifest", "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0");
dict.put("dsig", "urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0");
dict.put("ds", "http://www.w3.org/2000/09/xmldsig#");
return dict;
}

Expand Down
Expand Up @@ -61,7 +61,7 @@ private void assertNotEmpty(String plannedAction) {
throw new RuntimeException("Attempt to " + plannedAction + " of empty DefinitionSet ");
}
}

private void assertMultiples(String plannedAction) {
assertNotEmpty(plannedAction);
PuzzlePiece first = first();
Expand All @@ -85,7 +85,7 @@ public boolean equals(Object o) {
public int hashCode() {
return mDefinitions.hashCode();
}

private PuzzlePiece first() {
return this.iterator().next();
}
Expand Down Expand Up @@ -139,7 +139,7 @@ Map<PuzzlePiece, PuzzlePiece> uniteDefinitionsWithEqualContent() {
}
return retval;
}

/**
* Make PuzzlePieceSet immutable. Cannot be undone.
*
Expand Down
Expand Up @@ -18,18 +18,17 @@
## under the License.
##
##################################################################
## Template to create the list of signature artefacts which are to be generated

## Template to create the list of artifacts which are to be generated
##
## Documentation of template development can be found in local file
## TemplateHelp.html.
##
## Returns PuzzleComponent covering a PuzzlePiece or PuzzlePieceSet dependent if attribute is multiple times defined
## Template to create the list of artefacts which are to be generated
#set($attribute = $signaturemodel.getAttribute($context))
#set($NS = "urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0")
#set ($PREFIX = "dsig")
#set($localname = ${signaturemodel.extractLocalname($attribute)})
#set($classname = "${signaturemodel.camelCase($attribute)}Attribute")
#set($attribute = $model.getAttribute($context))
#set($NS = $attribute.getNamespace())
#set($localname = ${model.extractLocalname($attribute)})
#set($classname = "${model.camelCase($attribute.getLocalName())}Attribute")
#set ($valueobject = "String")
##
#set($datatypes = ${attribute.getDatatypes().withoutMultiples()})
Expand Down Expand Up @@ -95,22 +94,30 @@ import org.odftoolkit.odfdom.pkg.OdfElement;
#end
import org.odftoolkit.odfdom.pkg.OdfFileDom;
import org.odftoolkit.odfdom.pkg.OdfName;
import org.odftoolkit.odfdom.pkg.OdfPackageNamespace;

#if ($hasDefaultValue and $defaultValueSet.size()>1)
#foreach ($parent in ${attribute.getParents().withoutMultiples()})
#if ($model.getDefaultAttributeValue($attribute, $parent))
import org.odftoolkit.odfdom.pkg.dsig.${model.camelCase($parent.getLocalName())}Element;
#end
#end
#end
/**
* DOM implementation of OpenDocument attribute {@odf.attribute ${attribute.getQName()}}.
*
*/
public class $classname extends OdfAttribute {

public static final OdfName ATTRIBUTE_NAME = OdfName.newName("${NS}", "${PREFIX}:${attribute}");
public static final OdfName ATTRIBUTE_NAME = OdfName.newName(OdfPackageNamespace.${NS.toUpperCase()}, "$localname");
#if ($hasDefaultValue)
#if ($defaultValueSet.size()>1)
#foreach ($defaultValue in $defaultValueSet)
#set ($constant = $signaturemodel.constantCase($defaultValue))
#set ($constant = $model.constantCase($defaultValue))
#if (($defaultValue=="true" or $defaultValue=="false") and $enum == false)
public static final String DEFAULT_VALUE_${signaturemodel.escapeKeyword($constant)}= "$defaultValue";
public static final String DEFAULT_VALUE_${model.escapeKeyword($constant)}= "$defaultValue";
#else
public static final String DEFAULT_VALUE_${signaturemodel.escapeKeyword($constant)}= Value.${signaturemodel.escapeKeyword($constant)}.toString();
public static final String DEFAULT_VALUE_${model.escapeKeyword($constant)}= Value.${model.escapeKeyword($constant)}.toString();
#end
#end
#else
Expand Down Expand Up @@ -160,8 +167,8 @@ public class $classname extends OdfAttribute {
#if (${value.toString().length()} == 1)
#set ($constant = $value)
#else
#set ($constant = $signaturemodel.constantCase($value))
#end${separator}${signaturemodel.escapeKeyword($constant)}("${signaturemodel.escapeLiteral($value)}")#set ($separator=", ")
#set ($constant = $model.constantCase($value))
#end${separator}${model.escapeKeyword($constant)}("${model.escapeLiteral($value)}")#set ($separator=", ")
#end ;

private String mValue;
Expand Down Expand Up @@ -217,7 +224,7 @@ public class $classname extends OdfAttribute {
/**
* @param value The <code>${simplevalue}</code> value of the attribute.
*/
public void set${signaturemodel.camelCase($simplevalue)}Value(${simplevalue} value) {
public void set${model.camelCase($simplevalue)}Value(${simplevalue} value) {
super.setValue(String.valueOf(value));
}

Expand All @@ -227,7 +234,7 @@ public class $classname extends OdfAttribute {
public ${simplevalue} ${simplevalue}Value() {
String val = super.getValue();
try {
return ${valueobject}.parse${signaturemodel.camelCase($simplevalue)}(val);
return ${valueobject}.parse${model.camelCase($simplevalue)}(val);
} catch (NumberFormatException e) {
// TODO: validation handling/logging
throw (e);
Expand Down Expand Up @@ -311,13 +318,13 @@ public class $classname extends OdfAttribute {
#if ($defaultValueSet.size()>1)
#foreach ($parent in ${attribute.getParents().withoutMultiples()})
#if ($odfmodel.getDefaultAttributeValue($attribute, $parent))
if (parentElement instanceof ${signaturemodel.camelCase($parent)}Element) {
defaultValue = "${signaturemodel.escapeLiteral($odfmodel.getDefaultAttributeValue($attribute, $parent))}";
if (parentElement instanceof ${model.camelCase($parent)}Element) {
defaultValue = "${model.escapeLiteral($odfmodel.getDefaultAttributeValue($attribute, $parent))}";
}
#end
#end
#else
defaultValue = "${signaturemodel.escapeLiteral($uniqueDefaultValue)}";
defaultValue = "${model.escapeLiteral($uniqueDefaultValue)}";
#end
}
return defaultValue;
Expand Down

0 comments on commit 99975f3

Please sign in to comment.