Skip to content

Commit 8c3eb89

Browse files
Attribute @draw:concave of <draw:regular-polygon> does not have the datatype boolean, but only 'true' and 'false' as explicit values. Added method to assume in this case the datatype Boolean
1 parent 1d1c7f7 commit 8c3eb89

6 files changed

Lines changed: 97 additions & 67 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ public String getDefaultAttributeValue(String attributeName, String parentElemen
162162
if (defaultValueByElementParents == null) {
163163
return null;
164164
}
165-
// Not for ODF, but need extension if there are two attributes (same name)
166-
// with different defaults in same named parent?
165+
// Not for ODF, but need extension if there are two attributes (same name)
166+
// with different defaults in same named parent?
167167
defaultValue = defaultValueByElementParents.get(parentElementName);
168168
if (defaultValue == null) {
169169
defaultValue = defaultValueByElementParents.get(ALL_ELEMENTS);

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

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
*/
2424
package schema2template.grammar;
2525

26+
import static schema2template.grammar.PuzzlePiece.NAME_VISITOR;
27+
2628
import com.sun.msv.grammar.Expression;
2729
import com.sun.msv.grammar.Grammar;
2830
import com.sun.msv.grammar.NameClassAndExpression;
2931
import com.sun.msv.reader.trex.ng.RELAXNGReader;
3032
import com.sun.msv.reader.xmlschema.XMLSchemaReader;
3133
import com.sun.msv.writer.relaxng.RELAXNGWriter;
3234
import java.io.File;
35+
import java.util.ArrayList;
3336
import java.util.Collection;
3437
import java.util.HashMap;
3538
import java.util.Iterator;
@@ -45,7 +48,6 @@
4548
import org.apache.xml.serialize.OutputFormat;
4649
import org.apache.xml.serialize.XMLSerializer;
4750
import org.xml.sax.SAXException;
48-
import static schema2template.grammar.PuzzlePiece.NAME_VISITOR;
4951

5052
/**
5153
* The most important model, the first access to the XML Schema information.
@@ -322,18 +324,22 @@ public PuzzlePiece getAttribute(String qName, String qParentName) {
322324
PuzzlePiece attribute = null;
323325
if (attributes == null) {
324326
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-
}
327+
} else {
328+
for (PuzzlePiece ppAttribute : attributes.withMultiples().getCollection()) {
329+
// If there is more than one name for this expression, create more than one PuzzlePiece
330+
for (PuzzlePiece ppElement : ppAttribute.getParents().getCollection()) {
331+
List<String> names =
332+
(List<String>)
333+
((NameClassAndExpression) ppElement.getExpression())
334+
.getNameClass()
335+
.visit(NAME_VISITOR);
336+
// takes the first attribute definition with a correct parent
337+
if (names != null && names.contains(qParentName)) {
338+
attribute = ppAttribute;
339+
break;
340+
}
336341
}
342+
}
337343
}
338344
return attribute;
339345
}
@@ -347,34 +353,58 @@ public PuzzlePiece getAttribute(String qName, String qParentName) {
347353
* @return String of given Datatype
348354
*/
349355
public String getAttributeDataType(String qName, String qParentName) {
350-
String dataType = "String";
351-
PuzzlePiece attr = getAttribute(qName, qParentName);
352-
Collection<PuzzlePiece> d = attr.getDatatypes().getCollection();
353-
Boolean isBoolean = null;
354-
if(d.size() == 1){
355-
dataType = d.iterator().next().getQName();
356-
}else if(!d.isEmpty()){
357-
System.out.println("There are multiple datatypes!");
358-
} else {
359-
Iterator valueIterator = attr.getValues().getCollection().iterator();
360-
361-
while(valueIterator.hasNext()){
362-
String value = valueIterator.next().toString();
363-
if(value.equals("true") || value.equals("false")){
364-
isBoolean = Boolean.TRUE;
365-
}else{
366-
isBoolean = null;
367-
break;
368-
}
369-
}
370-
if(isBoolean != null && isBoolean){
371-
dataType = "Boolean";
372-
}
356+
String dataType = "String";
357+
PuzzlePiece attr = getAttribute(qName, qParentName);
358+
Collection<PuzzlePiece> d = attr.getDatatypes().getCollection();
359+
Boolean isBoolean = null;
360+
if (d.size() == 1) {
361+
dataType = d.iterator().next().getQName();
362+
} else if (!d.isEmpty()) {
363+
System.err.println(
364+
"\n***********\nThere are multiple datatypes for attribute '" + qName + "'\n");
365+
Iterator dataTypesIter = d.iterator();
366+
while (dataTypesIter.hasNext()) {
367+
System.err.println(dataTypesIter.next().toString());
373368
}
374-
return dataType;
369+
} else { // @draw:concave just uses 'false' and 'true' without datatype
370+
/** As there are sometimes multiple times definitions of the same attribute for an element
371+
* for an example https://docs.oasis-open.org/office/OpenDocument/v1.3/os/schemas/OpenDocument-v1.3-schema-rng.html#13591
372+
* which was taken as boolean, all attributes definitions of all elements of the same name are now takin into account!
373+
* BETTER FOR THE FUTURE: If the attribute definitons only for a certain element would be taken into account, e.g.
374+
* https://docs.oasis-open.org/office/OpenDocument/v1.3/os/schemas/OpenDocument-v1.3-schema-rng.html#6508
375+
*/
376+
PuzzleComponent elements = getElement(qParentName);
377+
List<String> attributeValues = null;
378+
if(elements != null && elements.getCollection().size() > 0){
379+
attributeValues = new ArrayList<String>();
380+
}
381+
for(PuzzlePiece ppElement : elements.getCollection()){
382+
PuzzlePieceSet attributes = ppElement.getAttributes();
383+
for(PuzzlePiece ppAttribute : attributes.getCollection()){
384+
if(ppAttribute.getQName().equals(qName)){
385+
for(PuzzlePiece ppValue : ppAttribute.getValues().getCollection()){
386+
// strange that the value has a localName (but the puzzlePiece abstraction should be overworked anyway)
387+
attributeValues.add(ppValue.getLocalName());
388+
}
389+
}
390+
}
391+
}
392+
393+
for (String value : attributeValues) {
394+
if (value.equals("true") || value.equals("false")) {
395+
isBoolean = Boolean.TRUE;
396+
} else {
397+
isBoolean = null;
398+
break;
399+
}
400+
}
401+
if (isBoolean != null && isBoolean) {
402+
dataType = "Boolean";
403+
}
404+
}
405+
return dataType;
375406
}
376407

377-
378408
/**
379409
* Get attribute by tag name and hash code. The hash code distincts Attributes sharing the same
380410
* tag name.

generator/schema2template/src/test/resources/test-reference/odf/generation/odfdom-java/odf-schema-1.0/org/odftoolkit/odfdom/dom/element/draw/DrawRegularPolygonElement.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,25 @@ public OdfName getOdfName() {
7979
*
8080
* Attribute is mandatory.
8181
*
82-
* @return - the <code>String</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
82+
* @return - the <code>Boolean</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
8383
*/
84-
public String getDrawConcaveAttribute() {
84+
public Boolean getDrawConcaveAttribute() {
8585
DrawConcaveAttribute attr = (DrawConcaveAttribute) getOdfAttribute(OdfDocumentNamespace.DRAW, "concave");
86-
if (attr != null) {
87-
return String.valueOf(attr.getValue());
86+
if (attr != null && !attr.getValue().isEmpty()) {
87+
return Boolean.valueOf(attr.booleanValue());
8888
}
8989
return null;
9090
}
9191

9292
/**
9393
* Sets the value of ODFDOM attribute representation <code>DrawConcaveAttribute</code> , See {@odf.attribute draw:concave}
9494
*
95-
* @param drawConcaveValue The type is <code>String</code>
95+
* @param drawConcaveValue The type is <code>Boolean</code>
9696
*/
97-
public void setDrawConcaveAttribute(String drawConcaveValue) {
97+
public void setDrawConcaveAttribute(Boolean drawConcaveValue) {
9898
DrawConcaveAttribute attr = new DrawConcaveAttribute((OdfFileDom) this.ownerDocument);
9999
setOdfAttribute(attr);
100-
attr.setValue(drawConcaveValue);
100+
attr.setBooleanValue(drawConcaveValue.booleanValue());
101101
}
102102

103103
/**

generator/schema2template/src/test/resources/test-reference/odf/generation/odfdom-java/odf-schema-1.1/org/odftoolkit/odfdom/dom/element/draw/DrawRegularPolygonElement.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,25 @@ public void setDrawCaptionIdAttribute(String drawCaptionIdValue) {
106106
*
107107
* Attribute is mandatory.
108108
*
109-
* @return - the <code>String</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
109+
* @return - the <code>Boolean</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
110110
*/
111-
public String getDrawConcaveAttribute() {
111+
public Boolean getDrawConcaveAttribute() {
112112
DrawConcaveAttribute attr = (DrawConcaveAttribute) getOdfAttribute(OdfDocumentNamespace.DRAW, "concave");
113-
if (attr != null) {
114-
return String.valueOf(attr.getValue());
113+
if (attr != null && !attr.getValue().isEmpty()) {
114+
return Boolean.valueOf(attr.booleanValue());
115115
}
116116
return null;
117117
}
118118

119119
/**
120120
* Sets the value of ODFDOM attribute representation <code>DrawConcaveAttribute</code> , See {@odf.attribute draw:concave}
121121
*
122-
* @param drawConcaveValue The type is <code>String</code>
122+
* @param drawConcaveValue The type is <code>Boolean</code>
123123
*/
124-
public void setDrawConcaveAttribute(String drawConcaveValue) {
124+
public void setDrawConcaveAttribute(Boolean drawConcaveValue) {
125125
DrawConcaveAttribute attr = new DrawConcaveAttribute((OdfFileDom) this.ownerDocument);
126126
setOdfAttribute(attr);
127-
attr.setValue(drawConcaveValue);
127+
attr.setBooleanValue(drawConcaveValue.booleanValue());
128128
}
129129

130130
/**

generator/schema2template/src/test/resources/test-reference/odf/generation/odfdom-java/odf-schema-1.2/org/odftoolkit/odfdom/dom/element/draw/DrawRegularPolygonElement.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,25 @@ public void setDrawCaptionIdAttribute(String drawCaptionIdValue) {
106106
*
107107
* Attribute is mandatory.
108108
*
109-
* @return - the <code>String</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
109+
* @return - the <code>Boolean</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
110110
*/
111-
public String getDrawConcaveAttribute() {
111+
public Boolean getDrawConcaveAttribute() {
112112
DrawConcaveAttribute attr = (DrawConcaveAttribute) getOdfAttribute(OdfDocumentNamespace.DRAW, "concave");
113-
if (attr != null) {
114-
return String.valueOf(attr.getValue());
113+
if (attr != null && !attr.getValue().isEmpty()) {
114+
return Boolean.valueOf(attr.booleanValue());
115115
}
116116
return null;
117117
}
118118

119119
/**
120120
* Sets the value of ODFDOM attribute representation <code>DrawConcaveAttribute</code> , See {@odf.attribute draw:concave}
121121
*
122-
* @param drawConcaveValue The type is <code>String</code>
122+
* @param drawConcaveValue The type is <code>Boolean</code>
123123
*/
124-
public void setDrawConcaveAttribute(String drawConcaveValue) {
124+
public void setDrawConcaveAttribute(Boolean drawConcaveValue) {
125125
DrawConcaveAttribute attr = new DrawConcaveAttribute((OdfFileDom) this.ownerDocument);
126126
setOdfAttribute(attr);
127-
attr.setValue(drawConcaveValue);
127+
attr.setBooleanValue(drawConcaveValue.booleanValue());
128128
}
129129

130130
/**

generator/schema2template/src/test/resources/test-reference/odf/generation/odfdom-java/odf-schema-1.3/org/odftoolkit/odfdom/dom/element/style/StyleTextPropertiesElement.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,25 +2395,25 @@ public void setTextConditionAttribute(String textConditionValue) {
23952395
*
23962396
* Attribute is mandatory.
23972397
*
2398-
* @return - the <code>Boolean</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
2398+
* @return - the <code>String</code> , the value or <code>null</code>, if the attribute is not set and no default value defined.
23992399
*/
2400-
public Boolean getTextDisplayAttribute() {
2400+
public String getTextDisplayAttribute() {
24012401
TextDisplayAttribute attr = (TextDisplayAttribute) getOdfAttribute(OdfDocumentNamespace.TEXT, "display");
2402-
if (attr != null && !attr.getValue().isEmpty()) {
2403-
return Boolean.valueOf(attr.booleanValue());
2402+
if (attr != null) {
2403+
return String.valueOf(attr.getValue());
24042404
}
24052405
return null;
24062406
}
24072407

24082408
/**
24092409
* Sets the value of ODFDOM attribute representation <code>TextDisplayAttribute</code> , See {@odf.attribute text:display}
24102410
*
2411-
* @param textDisplayValue The type is <code>Boolean</code>
2411+
* @param textDisplayValue The type is <code>String</code>
24122412
*/
2413-
public void setTextDisplayAttribute(Boolean textDisplayValue) {
2413+
public void setTextDisplayAttribute(String textDisplayValue) {
24142414
TextDisplayAttribute attr = new TextDisplayAttribute((OdfFileDom) this.ownerDocument);
24152415
setOdfAttribute(attr);
2416-
attr.setBooleanValue(textDisplayValue.booleanValue());
2416+
attr.setValue(textDisplayValue);
24172417
}
24182418

24192419
/**

0 commit comments

Comments
 (0)