Skip to content

Commit

Permalink
Fixes #207 - Adding a parent Expression to PuzzlePiece to allow a con…
Browse files Browse the repository at this point in the history
…text for attribute, which have different types depending on the element context
  • Loading branch information
svanteschubert committed Feb 8, 2023
1 parent 99b98ea commit 723a24a
Show file tree
Hide file tree
Showing 57 changed files with 2,949 additions and 2,902 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,15 @@ public SortedSet<String> getStyleFamilies() {
*/
public String getDefaultAttributeValue(String attributeName, String parentElementName) {
String defaultValue = null;
if (parentElementName.equals("table:table-cell") && attributeName.equals("table:protect")) {
System.err.println("YEAH!");
}
if (mAttributeDefaults == null || attributeName == null || attributeName.isBlank()) {
return null;
} else {
Map<String, String> defaultValueByElementParents = mAttributeDefaults.get(attributeName);
if (defaultValueByElementParents == null) {
return null;
}
// Not for ODF, but need extension if there are two attributes (same name)
// with different defaults in same named parent?
defaultValue = defaultValueByElementParents.get(parentElementName);
if (defaultValue == null) {
defaultValue = defaultValueByElementParents.get(ALL_ELEMENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class PuzzlePiece implements Comparable<PuzzlePiece>, PuzzleComponent {
private PuzzlePieceSet mMultiples = new PuzzlePieceSet();
// definitions of elements which can have this as children
private PuzzlePieceSet mParents = new PuzzlePieceSet();
private Expression mParentExpression = null;

// DEFINITION CONTENT
// ns:local tagname
private String mName;
Expand Down Expand Up @@ -182,7 +184,7 @@ public boolean equals(Object b) {
* a distinct Hash Code.
*/
public int hashCode() {
return (31 * mName.hashCode()) + mExpression.hashCode();
return (mName.hashCode()) ^ mExpression.hashCode();
}

/**
Expand Down Expand Up @@ -355,6 +357,15 @@ public PuzzlePieceSet getParents() {
return mParents;
}

/**
* Gets the Parent Expression which can contain this PuzzlePiece as a child
*
* @return The parent expression
*/
public Expression getParent() {
return mParentExpression;
}

/**
* Gets the child elements of this PuzzlePiece. Please note that only Definitions of type ELEMENT
* can have child elements.
Expand Down Expand Up @@ -550,7 +561,6 @@ private static <T extends Expression> void extractTypedPuzzlePieces(
}
}
}

// Fills multiple information
Iterator<PuzzlePiece> defIter = setToBeFilled.iterator();
while (defIter.hasNext()) {
Expand Down Expand Up @@ -664,59 +674,59 @@ private static void addChildExpression(
// Handle Element Definitions
Iterator<PuzzlePiece> iter = elements.iterator();
while (iter.hasNext()) {
PuzzlePiece puzzlePiece = iter.next();
MSVExpressionIterator childFinder =
PuzzlePiece ppElement = iter.next();
MSVExpressionIterator childIterator =
new MSVExpressionIterator(
puzzlePiece.getExpression(),
ppElement.getExpression(),
NameClassAndExpression.class,
MSVExpressionIterator.DIRECT_CHILDREN_ONLY);
while (childFinder.hasNext()) {
Expression child_exp = childFinder.next();
while (childIterator.hasNext()) {
Expression child_exp = childIterator.next();
List<PuzzlePiece> child_defs = null;
PuzzlePieceSet whereToAdd = null;
if (child_exp instanceof ElementExp) {
child_defs = reverseElementMap.get(child_exp);
whereToAdd = puzzlePiece.mChildElements;
whereToAdd = ppElement.mChildElements;
} else if (child_exp instanceof AttributeExp) {
child_defs = reverseAttributeMap.get(child_exp);
whereToAdd = puzzlePiece.mAttributes;
whereToAdd = ppElement.mAttributes;
}
if (child_defs != null) {
whereToAdd.addAll(child_defs);
for (PuzzlePiece child_def : child_defs) {
child_def.mParents.add(puzzlePiece);
child_def.mParents.add(ppElement);
}
}
}

if (graphMLTargetDir != null) {
TinkerPopGraph tinkerPopGraph =
new TinkerPopGraph(puzzlePiece.getExpression(), schemaFileName);
new TinkerPopGraph(ppElement.getExpression(), schemaFileName);
File f = new File(graphMLTargetDir);
f.mkdirs();
tinkerPopGraph.exportAsGraphML(f.getAbsolutePath());
}
MSVExpressionInformation elementInfo =
new MSVExpressionInformation(puzzlePiece.getExpression(), schemaFileName);
puzzlePiece.mCanHaveText = elementInfo.canHaveText();
new MSVExpressionInformation(ppElement.getExpression(), schemaFileName);
ppElement.mCanHaveText = elementInfo.canHaveText();

Map<String, List<Expression>> atnameToDefs = buildNameExpressionsMap(puzzlePiece.mAttributes);
Map<String, List<Expression>> atnameToDefs = buildNameExpressionsMap(ppElement.mAttributes);
for (String name : atnameToDefs.keySet()) {
if (elementInfo.isMandatory(atnameToDefs.get(name))) {
puzzlePiece.mMandatoryChildAttributeNames.add(name);
ppElement.mMandatoryChildAttributeNames.add(name);
}
}

Map<String, List<Expression>> elnameToDefs =
buildNameExpressionsMap(puzzlePiece.mChildElements);
buildNameExpressionsMap(ppElement.mChildElements);
for (String name : elnameToDefs.keySet()) {
if (elementInfo.isMandatory(elnameToDefs.get(name))) {
puzzlePiece.mMandatoryChildElementNames.add(name);
ppElement.mMandatoryChildElementNames.add(name);
}
}

puzzlePiece.mSingletonChildExpressions = elementInfo.getSingletons();
puzzlePiece.mMultipleChildExpressions = elementInfo.getMultiples();
ppElement.mSingletonChildExpressions = elementInfo.getSingletons();
ppElement.mMultipleChildExpressions = elementInfo.getMultiples();
}

// Handle Attribute Definitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ private void assertMultiples(String plannedAction) {
}
}

@Override
public boolean equals(Object o) {
return (o instanceof PuzzlePieceSet && ((PuzzlePieceSet) o).mDefinitions.equals(mDefinitions));
}

@Override
public int hashCode() {
return mDefinitions.hashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@

import com.sun.msv.grammar.Expression;
import com.sun.msv.grammar.Grammar;
import com.sun.msv.grammar.NameClassAndExpression;
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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
Expand All @@ -42,6 +45,7 @@
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.xml.sax.SAXException;
import static schema2template.grammar.PuzzlePiece.NAME_VISITOR;

/**
* The most important model, the first access to the XML Schema information.
Expand Down Expand Up @@ -294,17 +298,46 @@ public PuzzlePiece getElement(String name, int hashCode) {
* Get attribute by tag name. If there are multiple attributes sharing the same tag name, a
* PuzzlePieceSet is returned. If not, a single PuzzlePiece is returned.
*
* @param name
* @param qName
* @return Attribute PuzzlePiece(s)
*/
public PuzzleComponent getAttribute(String name) {
PuzzlePiece attribute = mNameAttributeMap.get(name);
public PuzzleComponent getAttribute(String qName) {
PuzzlePiece attribute = mNameAttributeMap.get(qName);
if (attribute == null) {
return null;
}
return attribute.withMultiples();
}

/**
* Get attribute by tag name. If there are multiple attributes sharing the same tag name, a
* PuzzlePieceSet is returned. If not, a single PuzzlePiece is returned.
*
* @param qName
* @param qParentName
* @return Attribute PuzzlePiece(s)
*/
public PuzzlePiece getAttribute(String qName, String qParentName) {
PuzzlePiece attributes = mNameAttributeMap.get(qName);
PuzzlePiece attribute = null;
if (attributes == null) {
return null;
}else {
for (PuzzlePiece ppAttribute : attributes.withMultiples().getCollection()) {
// If there is more than one name for this expression, create more than one PuzzlePiece
for (PuzzlePiece ppElement : ppAttribute.getParents().getCollection()) {
List<String> names =
(List<String>) ((NameClassAndExpression) ppElement.getExpression()).getNameClass().visit(NAME_VISITOR);
if(names != null && names.contains(qParentName)){
attribute = ppAttribute;
break;
}
}
}
}
return attribute;
}

/**
* Get attribute by tag name and hash code. The hash code distincts Attributes sharing the same
* tag name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
package schema2template.grammar;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
import schema2template.GenerationParameters;
import schema2template.SchemaToTemplate;

Expand Down Expand Up @@ -154,7 +157,6 @@ public void testAllExampleGenerations() {
SchemaToTemplate.run(generations);
} catch (Exception e) {
Assert.fail("Exception during test run: " + e.toString());
e.printStackTrace();
throw new RuntimeException(e);
}
DirectoryCompare.compareDirectories(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ public ${abstract}class $className extends $superClassName {
## ---------------------------------------------------
##
#foreach ($attr in $remainingAttributes)
#set($attri = ${xmlModel.getAttribute($attr.getQName())})
## receiving the attribute that has this element as parent (@text:value has different types dependent on parent element)
#set($attri = ${xmlModel.getAttribute($attr.getQName(), $element.getQName())})
#set ($valueObject = "String")
#set ($simpleValue = "")
#set ($dataTypes = ${attri.getDatatypes().withoutMultiples()})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h3><a name="element_manifest:file-entry_1">manifest:file-entry Element</a></h3>
<a href="#attribute_manifest:media-type_1" class="unbreakable mandatory">manifest:media-type</a>&nbsp;
<a href="#attribute_manifest:preferred-view-mode_1" class="unbreakable ">manifest:preferred-view-mode</a>&nbsp;
<a href="#attribute_manifest:size_1" class="unbreakable ">manifest:size</a>&nbsp;
<a href="#attribute_manifest:version_1" class="unbreakable ">manifest:version[1]</a>&nbsp;
<a href="#attribute_manifest:version_2" class="unbreakable ">manifest:version[2]</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
Expand Down Expand Up @@ -142,7 +142,7 @@ <h3><a name="element_manifest:manifest_1">manifest:manifest Element</a></h3>
<tr>
<td class="left">Attributes</td>
<td class="right">
<a href="#attribute_manifest:version_2" class="unbreakable mandatory">manifest:version[2]</a>&nbsp;
<a href="#attribute_manifest:version_1" class="unbreakable mandatory">manifest:version[1]</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
Expand Down Expand Up @@ -463,18 +463,18 @@ <h3><a name="attribute_manifest:version_1">manifest:version[1] Attribute</a>&nbs
<tr>
<td class="left">Parent Elements</td>
<td class="right">
<a href="#element_manifest:file-entry_1" class="unbreakable">manifest:file-entry</a>&nbsp;
<a href="#element_manifest:manifest_1" class="unbreakable">manifest:manifest</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Datatypes</td>
<td class="right">
<span style="unbreakable">string</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Values</td>
<td class="right">
<span style="unbreakable">"1.2"</a>&nbsp;
&nbsp;</td>
</tr>
</table>
Expand All @@ -485,18 +485,18 @@ <h3><a name="attribute_manifest:version_2">manifest:version[2] Attribute</a>&nbs
<tr>
<td class="left">Parent Elements</td>
<td class="right">
<a href="#element_manifest:manifest_1" class="unbreakable">manifest:manifest</a>&nbsp;
<a href="#element_manifest:file-entry_1" class="unbreakable">manifest:file-entry</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Datatypes</td>
<td class="right">
<span style="unbreakable">string</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Values</td>
<td class="right">
<span style="unbreakable">"1.2"</a>&nbsp;
&nbsp;</td>
</tr>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,13 @@ <h3><a name="attribute_manifest:key-derivation-name_1">manifest:key-derivation-n
<tr>
<td class="left">Datatypes</td>
<td class="right">
<span style="unbreakable">anyURI</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Values</td>
<td class="right">
<span style="unbreakable">"PGP"</a>&nbsp;
<span style="unbreakable">"PBKDF2"</a>&nbsp;
&nbsp;</td>
</tr>
</table>
Expand All @@ -537,13 +538,12 @@ <h3><a name="attribute_manifest:key-derivation-name_2">manifest:key-derivation-n
<tr>
<td class="left">Datatypes</td>
<td class="right">
<span style="unbreakable">anyURI</a>&nbsp;
&nbsp;</td>
</tr>
<tr>
<td class="left">Values</td>
<td class="right">
<span style="unbreakable">"PBKDF2"</a>&nbsp;
<span style="unbreakable">"PGP"</a>&nbsp;
&nbsp;</td>
</tr>
</table>
Expand Down
Loading

0 comments on commit 723a24a

Please sign in to comment.