Skip to content

Commit 723a24a

Browse files
Fixes #207 - Adding a parent Expression to PuzzlePiece to allow a context for attribute, which have different types depending on the element context
1 parent 99b98ea commit 723a24a

File tree

57 files changed

+2949
-2902
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2949
-2902
lines changed

generator/schema2template/src/main/java/schema2template/grammar/OdfModel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,15 @@ public SortedSet<String> getStyleFamilies() {
155155
*/
156156
public String getDefaultAttributeValue(String attributeName, String parentElementName) {
157157
String defaultValue = null;
158-
if (parentElementName.equals("table:table-cell") && attributeName.equals("table:protect")) {
159-
System.err.println("YEAH!");
160-
}
161158
if (mAttributeDefaults == null || attributeName == null || attributeName.isBlank()) {
162159
return null;
163160
} else {
164161
Map<String, String> defaultValueByElementParents = mAttributeDefaults.get(attributeName);
165162
if (defaultValueByElementParents == null) {
166163
return null;
167164
}
165+
// Not for ODF, but need extension if there are two attributes (same name)
166+
// with different defaults in same named parent?
168167
defaultValue = defaultValueByElementParents.get(parentElementName);
169168
if (defaultValue == null) {
170169
defaultValue = defaultValueByElementParents.get(ALL_ELEMENTS);

generator/schema2template/src/main/java/schema2template/grammar/PuzzlePiece.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public class PuzzlePiece implements Comparable<PuzzlePiece>, PuzzleComponent {
7171
private PuzzlePieceSet mMultiples = new PuzzlePieceSet();
7272
// definitions of elements which can have this as children
7373
private PuzzlePieceSet mParents = new PuzzlePieceSet();
74+
private Expression mParentExpression = null;
75+
7476
// DEFINITION CONTENT
7577
// ns:local tagname
7678
private String mName;
@@ -182,7 +184,7 @@ public boolean equals(Object b) {
182184
* a distinct Hash Code.
183185
*/
184186
public int hashCode() {
185-
return (31 * mName.hashCode()) + mExpression.hashCode();
187+
return (mName.hashCode()) ^ mExpression.hashCode();
186188
}
187189

188190
/**
@@ -355,6 +357,15 @@ public PuzzlePieceSet getParents() {
355357
return mParents;
356358
}
357359

360+
/**
361+
* Gets the Parent Expression which can contain this PuzzlePiece as a child
362+
*
363+
* @return The parent expression
364+
*/
365+
public Expression getParent() {
366+
return mParentExpression;
367+
}
368+
358369
/**
359370
* Gets the child elements of this PuzzlePiece. Please note that only Definitions of type ELEMENT
360371
* can have child elements.
@@ -550,7 +561,6 @@ private static <T extends Expression> void extractTypedPuzzlePieces(
550561
}
551562
}
552563
}
553-
554564
// Fills multiple information
555565
Iterator<PuzzlePiece> defIter = setToBeFilled.iterator();
556566
while (defIter.hasNext()) {
@@ -664,59 +674,59 @@ private static void addChildExpression(
664674
// Handle Element Definitions
665675
Iterator<PuzzlePiece> iter = elements.iterator();
666676
while (iter.hasNext()) {
667-
PuzzlePiece puzzlePiece = iter.next();
668-
MSVExpressionIterator childFinder =
677+
PuzzlePiece ppElement = iter.next();
678+
MSVExpressionIterator childIterator =
669679
new MSVExpressionIterator(
670-
puzzlePiece.getExpression(),
680+
ppElement.getExpression(),
671681
NameClassAndExpression.class,
672682
MSVExpressionIterator.DIRECT_CHILDREN_ONLY);
673-
while (childFinder.hasNext()) {
674-
Expression child_exp = childFinder.next();
683+
while (childIterator.hasNext()) {
684+
Expression child_exp = childIterator.next();
675685
List<PuzzlePiece> child_defs = null;
676686
PuzzlePieceSet whereToAdd = null;
677687
if (child_exp instanceof ElementExp) {
678688
child_defs = reverseElementMap.get(child_exp);
679-
whereToAdd = puzzlePiece.mChildElements;
689+
whereToAdd = ppElement.mChildElements;
680690
} else if (child_exp instanceof AttributeExp) {
681691
child_defs = reverseAttributeMap.get(child_exp);
682-
whereToAdd = puzzlePiece.mAttributes;
692+
whereToAdd = ppElement.mAttributes;
683693
}
684694
if (child_defs != null) {
685695
whereToAdd.addAll(child_defs);
686696
for (PuzzlePiece child_def : child_defs) {
687-
child_def.mParents.add(puzzlePiece);
697+
child_def.mParents.add(ppElement);
688698
}
689699
}
690700
}
691701

692702
if (graphMLTargetDir != null) {
693703
TinkerPopGraph tinkerPopGraph =
694-
new TinkerPopGraph(puzzlePiece.getExpression(), schemaFileName);
704+
new TinkerPopGraph(ppElement.getExpression(), schemaFileName);
695705
File f = new File(graphMLTargetDir);
696706
f.mkdirs();
697707
tinkerPopGraph.exportAsGraphML(f.getAbsolutePath());
698708
}
699709
MSVExpressionInformation elementInfo =
700-
new MSVExpressionInformation(puzzlePiece.getExpression(), schemaFileName);
701-
puzzlePiece.mCanHaveText = elementInfo.canHaveText();
710+
new MSVExpressionInformation(ppElement.getExpression(), schemaFileName);
711+
ppElement.mCanHaveText = elementInfo.canHaveText();
702712

703-
Map<String, List<Expression>> atnameToDefs = buildNameExpressionsMap(puzzlePiece.mAttributes);
713+
Map<String, List<Expression>> atnameToDefs = buildNameExpressionsMap(ppElement.mAttributes);
704714
for (String name : atnameToDefs.keySet()) {
705715
if (elementInfo.isMandatory(atnameToDefs.get(name))) {
706-
puzzlePiece.mMandatoryChildAttributeNames.add(name);
716+
ppElement.mMandatoryChildAttributeNames.add(name);
707717
}
708718
}
709719

710720
Map<String, List<Expression>> elnameToDefs =
711-
buildNameExpressionsMap(puzzlePiece.mChildElements);
721+
buildNameExpressionsMap(ppElement.mChildElements);
712722
for (String name : elnameToDefs.keySet()) {
713723
if (elementInfo.isMandatory(elnameToDefs.get(name))) {
714-
puzzlePiece.mMandatoryChildElementNames.add(name);
724+
ppElement.mMandatoryChildElementNames.add(name);
715725
}
716726
}
717727

718-
puzzlePiece.mSingletonChildExpressions = elementInfo.getSingletons();
719-
puzzlePiece.mMultipleChildExpressions = elementInfo.getMultiples();
728+
ppElement.mSingletonChildExpressions = elementInfo.getSingletons();
729+
ppElement.mMultipleChildExpressions = elementInfo.getMultiples();
720730
}
721731

722732
// Handle Attribute Definitions

generator/schema2template/src/main/java/schema2template/grammar/PuzzlePieceSet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ private void assertMultiples(String plannedAction) {
9191
}
9292
}
9393

94+
@Override
9495
public boolean equals(Object o) {
9596
return (o instanceof PuzzlePieceSet && ((PuzzlePieceSet) o).mDefinitions.equals(mDefinitions));
9697
}
9798

99+
@Override
98100
public int hashCode() {
99101
return mDefinitions.hashCode();
100102
}

generator/schema2template/src/main/java/schema2template/grammar/XMLModel.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525

2626
import com.sun.msv.grammar.Expression;
2727
import com.sun.msv.grammar.Grammar;
28+
import com.sun.msv.grammar.NameClassAndExpression;
2829
import com.sun.msv.reader.trex.ng.RELAXNGReader;
2930
import com.sun.msv.reader.xmlschema.XMLSchemaReader;
3031
import com.sun.msv.writer.relaxng.RELAXNGWriter;
3132
import java.io.File;
3233
import java.util.Collection;
3334
import java.util.HashMap;
35+
import java.util.Iterator;
36+
import java.util.List;
3437
import java.util.Map;
3538
import java.util.Set;
3639
import java.util.SortedSet;
@@ -42,6 +45,7 @@
4245
import org.apache.xml.serialize.OutputFormat;
4346
import org.apache.xml.serialize.XMLSerializer;
4447
import org.xml.sax.SAXException;
48+
import static schema2template.grammar.PuzzlePiece.NAME_VISITOR;
4549

4650
/**
4751
* The most important model, the first access to the XML Schema information.
@@ -294,17 +298,46 @@ public PuzzlePiece getElement(String name, int hashCode) {
294298
* Get attribute by tag name. If there are multiple attributes sharing the same tag name, a
295299
* PuzzlePieceSet is returned. If not, a single PuzzlePiece is returned.
296300
*
297-
* @param name
301+
* @param qName
298302
* @return Attribute PuzzlePiece(s)
299303
*/
300-
public PuzzleComponent getAttribute(String name) {
301-
PuzzlePiece attribute = mNameAttributeMap.get(name);
304+
public PuzzleComponent getAttribute(String qName) {
305+
PuzzlePiece attribute = mNameAttributeMap.get(qName);
302306
if (attribute == null) {
303307
return null;
304308
}
305309
return attribute.withMultiples();
306310
}
307311

312+
/**
313+
* Get attribute by tag name. If there are multiple attributes sharing the same tag name, a
314+
* PuzzlePieceSet is returned. If not, a single PuzzlePiece is returned.
315+
*
316+
* @param qName
317+
* @param qParentName
318+
* @return Attribute PuzzlePiece(s)
319+
*/
320+
public PuzzlePiece getAttribute(String qName, String qParentName) {
321+
PuzzlePiece attributes = mNameAttributeMap.get(qName);
322+
PuzzlePiece attribute = null;
323+
if (attributes == null) {
324+
return null;
325+
}else {
326+
for (PuzzlePiece ppAttribute : attributes.withMultiples().getCollection()) {
327+
// If there is more than one name for this expression, create more than one PuzzlePiece
328+
for (PuzzlePiece ppElement : ppAttribute.getParents().getCollection()) {
329+
List<String> names =
330+
(List<String>) ((NameClassAndExpression) ppElement.getExpression()).getNameClass().visit(NAME_VISITOR);
331+
if(names != null && names.contains(qParentName)){
332+
attribute = ppAttribute;
333+
break;
334+
}
335+
}
336+
}
337+
}
338+
return attribute;
339+
}
340+
308341
/**
309342
* Get attribute by tag name and hash code. The hash code distincts Attributes sharing the same
310343
* tag name.

generator/schema2template/src/test/java/schema2template/grammar/GenerationOdfdomJavaTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
package schema2template.grammar;
2323

2424
import java.io.File;
25+
import java.io.IOException;
2526
import java.util.ArrayList;
2627
import java.util.logging.Logger;
28+
import javax.xml.parsers.ParserConfigurationException;
2729
import org.junit.Assert;
2830
import org.junit.Test;
31+
import org.xml.sax.SAXException;
2932
import schema2template.GenerationParameters;
3033
import schema2template.SchemaToTemplate;
3134

@@ -154,7 +157,6 @@ public void testAllExampleGenerations() {
154157
SchemaToTemplate.run(generations);
155158
} catch (Exception e) {
156159
Assert.fail("Exception during test run: " + e.toString());
157-
e.printStackTrace();
158160
throw new RuntimeException(e);
159161
}
160162
DirectoryCompare.compareDirectories(

generator/schema2template/src/test/resources/test-input/odf/generation/odfdom-java/dom/template/java-odfdom-element-template.vm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ public ${abstract}class $className extends $superClassName {
274274
## ---------------------------------------------------
275275
##
276276
#foreach ($attr in $remainingAttributes)
277-
#set($attri = ${xmlModel.getAttribute($attr.getQName())})
277+
## receiving the attribute that has this element as parent (@text:value has different types dependent on parent element)
278+
#set($attri = ${xmlModel.getAttribute($attr.getQName(), $element.getQName())})
278279
#set ($valueObject = "String")
279280
#set ($simpleValue = "")
280281
#set ($dataTypes = ${attri.getDatatypes().withoutMultiples()})

generator/schema2template/src/test/resources/test-reference/odf/generation/odf-reference/odf-package-manifest-1.2/OdfReference.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ <h3><a name="element_manifest:file-entry_1">manifest:file-entry Element</a></h3>
9595
<a href="#attribute_manifest:media-type_1" class="unbreakable mandatory">manifest:media-type</a>&nbsp;
9696
<a href="#attribute_manifest:preferred-view-mode_1" class="unbreakable ">manifest:preferred-view-mode</a>&nbsp;
9797
<a href="#attribute_manifest:size_1" class="unbreakable ">manifest:size</a>&nbsp;
98-
<a href="#attribute_manifest:version_1" class="unbreakable ">manifest:version[1]</a>&nbsp;
98+
<a href="#attribute_manifest:version_2" class="unbreakable ">manifest:version[2]</a>&nbsp;
9999
&nbsp;</td>
100100
</tr>
101101
<tr>
@@ -142,7 +142,7 @@ <h3><a name="element_manifest:manifest_1">manifest:manifest Element</a></h3>
142142
<tr>
143143
<td class="left">Attributes</td>
144144
<td class="right">
145-
<a href="#attribute_manifest:version_2" class="unbreakable mandatory">manifest:version[2]</a>&nbsp;
145+
<a href="#attribute_manifest:version_1" class="unbreakable mandatory">manifest:version[1]</a>&nbsp;
146146
&nbsp;</td>
147147
</tr>
148148
<tr>
@@ -463,18 +463,18 @@ <h3><a name="attribute_manifest:version_1">manifest:version[1] Attribute</a>&nbs
463463
<tr>
464464
<td class="left">Parent Elements</td>
465465
<td class="right">
466-
<a href="#element_manifest:file-entry_1" class="unbreakable">manifest:file-entry</a>&nbsp;
466+
<a href="#element_manifest:manifest_1" class="unbreakable">manifest:manifest</a>&nbsp;
467467
&nbsp;</td>
468468
</tr>
469469
<tr>
470470
<td class="left">Datatypes</td>
471471
<td class="right">
472-
<span style="unbreakable">string</a>&nbsp;
473472
&nbsp;</td>
474473
</tr>
475474
<tr>
476475
<td class="left">Values</td>
477476
<td class="right">
477+
<span style="unbreakable">"1.2"</a>&nbsp;
478478
&nbsp;</td>
479479
</tr>
480480
</table>
@@ -485,18 +485,18 @@ <h3><a name="attribute_manifest:version_2">manifest:version[2] Attribute</a>&nbs
485485
<tr>
486486
<td class="left">Parent Elements</td>
487487
<td class="right">
488-
<a href="#element_manifest:manifest_1" class="unbreakable">manifest:manifest</a>&nbsp;
488+
<a href="#element_manifest:file-entry_1" class="unbreakable">manifest:file-entry</a>&nbsp;
489489
&nbsp;</td>
490490
</tr>
491491
<tr>
492492
<td class="left">Datatypes</td>
493493
<td class="right">
494+
<span style="unbreakable">string</a>&nbsp;
494495
&nbsp;</td>
495496
</tr>
496497
<tr>
497498
<td class="left">Values</td>
498499
<td class="right">
499-
<span style="unbreakable">"1.2"</a>&nbsp;
500500
&nbsp;</td>
501501
</tr>
502502
</table>

generator/schema2template/src/test/resources/test-reference/odf/generation/odf-reference/odf-package-manifest-1.3/OdfReference.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,13 @@ <h3><a name="attribute_manifest:key-derivation-name_1">manifest:key-derivation-n
515515
<tr>
516516
<td class="left">Datatypes</td>
517517
<td class="right">
518+
<span style="unbreakable">anyURI</a>&nbsp;
518519
&nbsp;</td>
519520
</tr>
520521
<tr>
521522
<td class="left">Values</td>
522523
<td class="right">
523-
<span style="unbreakable">"PGP"</a>&nbsp;
524+
<span style="unbreakable">"PBKDF2"</a>&nbsp;
524525
&nbsp;</td>
525526
</tr>
526527
</table>
@@ -537,13 +538,12 @@ <h3><a name="attribute_manifest:key-derivation-name_2">manifest:key-derivation-n
537538
<tr>
538539
<td class="left">Datatypes</td>
539540
<td class="right">
540-
<span style="unbreakable">anyURI</a>&nbsp;
541541
&nbsp;</td>
542542
</tr>
543543
<tr>
544544
<td class="left">Values</td>
545545
<td class="right">
546-
<span style="unbreakable">"PBKDF2"</a>&nbsp;
546+
<span style="unbreakable">"PGP"</a>&nbsp;
547547
&nbsp;</td>
548548
</tr>
549549
</table>

0 commit comments

Comments
 (0)