Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ODF XML Reference HTML: Using some short notation for relationship of node children #184

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private static Graph buildGraph(
v = createVertex(g, exp);
}

// stop building the graph after first element and attribtue children
addGraphProperties(g, v, parentV, exp, parentExp);
// stop building the graph after first element and attribute children
if (!(exp instanceof NameClassAndExpression) || parentExp == null) {
List<Expression> children = (List<Expression>) exp.visit(CHILD_VISITOR);
Integer newChildNo = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@
*/
package schema2template.grammar;

import com.sun.msv.grammar.ElementExp;
import com.sun.msv.grammar.Expression;
import com.sun.msv.grammar.Grammar;
import com.sun.msv.grammar.ReferenceExp;
import com.sun.msv.grammar.util.ExpressionWalker;
import com.sun.msv.reader.trex.ng.RELAXNGReader;
import com.sun.msv.reader.xmlschema.XMLSchemaReader;
import com.sun.msv.writer.relaxng.RELAXNGWriter;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.SAXParserFactory;
Expand Down Expand Up @@ -66,6 +63,8 @@ public class XMLModel {
private String mGrammarID;
private String mGrammarVersion;

private Set<Expression> mIslandHeads;

/**
* Constructs new model by the grammar and a label
*
Expand Down Expand Up @@ -565,4 +564,85 @@ public static String extractLocalName(String name) {
public static String extractLocalName(PuzzleComponent def) {
return extractLocalName(def.getQName());
}

/**
* Allows to express the relationship of descendants as HTML with a pseudocode aligned to RegEx.
* Used for the OdfReference generated by this project.
*
* @param expr the expression which children should be viewed
* @param elementDepth the amount of elements that should be shown. 1 will show only the child
* elements not their content (the default and used by ODF Reference).
* @return the HTML as string. Usually to be inserted within documentation.
*/
public String getDescendantsAsHTMLString(Expression expr, int elementDepth) {
StringBuilder builder = new StringBuilder();
if (this.mGrammar == null) {
System.err.println("xx");
}
if (expr == null) {
System.err.println("yy");
}
// ElementExps and ReferenceExps who are referenced more than once.
if (mIslandHeads == null) {
mIslandHeads = getHeadsOfIslands(this.mGrammar);
}

expr.visit(
new MSVExpressionVisitorDescendantsAsHTMLString(builder, mIslandHeads, elementDepth));
return builder.toString();
}

/**
* Allows to express the relationship of descendants as HTML with a pseudocode aligned to RegEx.
* Used for OdfReference (2DO: Add Reference)
*
* @param expr the expression which children should be viewed (must be of the grammar of this XML
* model)
* @return the HTML as string. Usually to be inserted within documentation.
*/
public String getChildrenAsHTMLString(Expression expr) {
return getDescendantsAsHTMLString(expr, 1);
}

/**
* Identifies heads of islands of grammars, which are multiple used and must be identified to
* avoid infinite loops when traversing the grammar
*/
public static Set<Expression> getHeadsOfIslands(Grammar grammar) {
// collect all reachable ElementExps and ReferenceExps.
final Set<Expression> nodes = new HashSet<Expression>();
// ElementExps and ReferenceExps who are referenced more than once.
final Set<Expression> heads = new HashSet<Expression>();

grammar
.getTopLevel()
.visit(
new ExpressionWalker() {
// ExpressionWalker class traverses expressions in depth-first order.
// So this invokation traverses the all reachable expressions from
// the top level expression.

// Whenever visiting elements and RefExps, they are memorized
// to identify head of islands.
public void onElement(ElementExp exp) {
if (nodes.contains(exp)) {
heads.add(exp);
return; // prevent infinite recursion.
}
nodes.add(exp);
super.onElement(exp);
}

public void onRef(ReferenceExp exp) {
if (nodes.contains(exp)) {
heads.add(exp);
return; // prevent infinite recursion.
}
nodes.add(exp);
super.onRef(exp);
}
});

return heads;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GenerationOdfReferenceTest {

/** Test: It should be able to generate all examples without a failure. */
@Test
public void testAllExampleGenerations() {
public void testAllExampleGenerations() throws Exception {
ArrayList<GenerationParameters> generations = new ArrayList<>();

for (OdfSpecificationPart specPart : OdfSpecificationPart.values()) {
Expand Down Expand Up @@ -76,7 +76,7 @@ public void testAllExampleGenerations() {
SchemaToTemplate.run(generations);
} catch (Exception e) {
Assert.fail("Exception during test run: " + e.toString());
throw new RuntimeException(e);
throw new Exception(e);
}

// Changing order of multiple puzzlepieces makes file comparison unuseable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* **********************************************************************
*
* <p>DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
*
* <p>Copyright 2009, 2010 Oracle and/or its affiliates. All rights reserved.
*
* <p>Use is subject to license terms.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at
* http://odftoolkit.org/docs/license.txt
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
*
* <p>See the License for the specific language governing permissions and limitations under the
* License.
*
* <p>*********************************************************************
*/
package schema2template.grammar;

import com.sun.msv.grammar.*;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* MSVDescendantsToHTMLTest is able to dump for each XML element and XML attribute its content as a
* string expressing choice, sequence, etc. with regular expression syntax.
*/
public class MSVDescendantsToHTMLTest {

private static final Logger LOG = Logger.getLogger(MSVDescendantsToHTMLTest.class.getName());

private static final String CHILD_DESC_DIRECTORY = "childDesc";

@Before
public void intialize() {
new File(ConstantsBuildEnv.TARGET_BASE_DIR + CHILD_DESC_DIRECTORY).mkdirs();
}

/**
* This test iterates over all ODF grammars loads them into the MSV Validator and dumps the
* run-time model (ExpressionTree) into a file.
*/
@Test
public void testMSVExpressionTree() {
try {
for (ConstantsOdf.OdfSpecificationPart specPart :
ConstantsOdf.OdfSpecificationPart.values()) {
if (specPart.grammarID.equals(ConstantsOdf.GrammarID.ODF_SCHEMA.ID)
&& specPart.grammarVersion.equals(
ConstantsOdf.OdfSpecificationPart.ODF_1_3_SCHEMA.grammarVersion)) {
LOG.info(
"\n\nNew ODF grammar runtime serialization (MSV dump) for regression test:"
+ "\n\tgrammarVersion "
+ specPart.grammarVersion
+ "\n\tgrammarID: "
+ specPart.grammarID
+ "\n\tgrammarPath: "
+ specPart.grammarPath
+ "\n\ttargetDirPath: "
+ ConstantsBuildEnv.TARGET_BASE_DIR
+ CHILD_DESC_DIRECTORY);
Grammar xmlGrammar = XMLModel.loadSchema(specPart.grammarPath);
String odfDump = dumpChildRelation(xmlGrammar);

String grammarLabel = specPart.grammarID + "-" + specPart.grammarVersion;

String targetChildDescFile =
ConstantsBuildEnv.TARGET_BASE_DIR
+ CHILD_DESC_DIRECTORY
+ File.separator
+ grammarLabel
+ "-msvRegEx.txt";
LOG.log(
Level.INFO,
"Writing MSV Child descriptions for " + grammarLabel + " + into file: {0}",
targetChildDescFile);
try (PrintWriter out = new PrintWriter(new FileWriter(targetChildDescFile))) {
out.print(odfDump);
}
}
/*DirectoryCompare.compareDirectories(
ConstantsBuildEnv.TARGET_BASE_DIR + CHILD_DESC_DIRECTORY,
ConstantsBuildEnv.REFERENCE_BASE_DIR + CHILD_DESC_DIRECTORY);*/
}
} catch (Exception ex) {
Logger.getLogger(MSVDescendantsToHTMLTest.class.getName()).log(Level.SEVERE, null, ex);
Assert.fail(ex.toString());
}
}
/**
* Iterates the MSVExpressionTree between parent and children and dumps their relation into a
* string
*
* @return the MSVExpressionTree serialized into a String
*/
private static String dumpChildRelation(Grammar xmlGrammar) throws Exception {
MSVExpressionIterator iterator = new MSVExpressionIterator(xmlGrammar.getTopLevel());
StringBuilder builder = new StringBuilder();
while (iterator.hasNext()) {
Expression expr = iterator.next();
if (expr instanceof NameClassAndExpression) {
List<String> names =
(List<String>)
((NameClassAndExpression) expr).getNameClass().visit(new MSVNameClassVisitorList());
// 2DO: ONGOING INITIAL DEBUG WITH ONE ELEMENT!!
// if (names.get(0).equals("form:property")) {

// see
// http://docs.oasis-open.org/office/OpenDocument/v1.3/os/schemas/OpenDocument-v1.3-schema-rng.html#table-table
if (names.get(0).equals("table:table")) {
expr.visit(
new MSVExpressionVisitorDescendantsAsHTMLString(
builder, XMLModel.getHeadsOfIslands(xmlGrammar)));
}
}
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testMSVExpressionTree() {
*
* @return the MSVExpressionTree serialized into a String
*/
public static String dumpMSVExpressionTree(Expression rootExpression) throws Exception {
private static String dumpMSVExpressionTree(Expression rootExpression) throws Exception {
MSVExpressionIterator iterator = new MSVExpressionIterator(rootExpression);
StringBuilder builder = new StringBuilder();
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ For example, <a href="#attribute_manifest:key-derivation-name_1">manifest:key-de
#end
&nbsp;</td>
</tr>
#set ($elementExp = $element.getExpression())
#if ($elementExp)
<tr>
<td class="left">Child Relations</td>
<td class="right">${xmlModel.getChildrenAsHTMLString($elementExp)}&nbsp;</td>
</tr>
#end
</table>
<br />
#end
Expand Down Expand Up @@ -244,18 +251,22 @@ For example, <a href="#attribute_manifest:key-derivation-name_1">manifest:key-de
<td class="left">Datatypes</td>
<td class="right">
#foreach ($datatype in ${attribute.getDatatypes()})
<span style="unbreakable">${datatype}</a>&nbsp;
<span style="unbreakable">${datatype}</span>&nbsp;
#end
&nbsp;</td>
</tr>
<tr>
<td class="left">Values</td>
<td class="right">
#foreach ($value in ${attribute.getValues()})
<span style="unbreakable">"${value}"</a>&nbsp;
<span style="unbreakable">"${value}"</span>&nbsp;
#end
&nbsp;</td>
</tr>
<tr>
<td class="left">RNG Relations</td>
<td class="right">${xmlModel.getChildrenAsHTMLString($attribute.getExpression())}&nbsp;</td>
</tr>
</table>
<br />
#end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ <h3><a name="element_ds:Signature_1">ds:Signature Element</a></h3>
[any org.w3c.dom.Element]&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Child Relations</td>
<td class="right">&lt;ds:Signature (*:*="TEXT" | TEXT | &lt;*:* ... &gt;)+ &gt;&nbsp;</td>
</tr>
</table>
<br />

Expand All @@ -72,6 +76,10 @@ <h3><a name="element_dsig:document-signatures_1">dsig:document-signatures Elemen
<a href="#element_ds:Signature_1" class="unbreakable mandatory">&lt;ds:Signature&gt;</a>*&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Child Relations</td>
<td class="right">&lt;dsig:document-signatures dsig:version="1.2" (&lt;ds:Signature ... &gt;)+ &gt;&nbsp;</td>
</tr>
</table>
<br />
<h3><a name="attribute_dsig:version_1">dsig:version Attribute</a></h3>
Expand All @@ -90,9 +98,13 @@ <h3><a name="attribute_dsig:version_1">dsig:version Attribute</a></h3>
<tr>
<td class="left">Values</td>
<td class="right">
<span style="unbreakable">"1.2"</a>&nbsp;
<span style="unbreakable">"1.2"</span>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">RNG Relations</td>
<td class="right">dsig:version="1.2" &nbsp;</td>
</tr>
</table>
<br />
</body>
Expand Down
Loading