Commit 612e5733 authored by Jos van den Oever's avatar Jos van den Oever

Handle <define/> elements with the same name with the combine attribute.

parent e18f9aa0
......@@ -128,16 +128,57 @@ class OdfRngInfoParser {
return names;
}
/**
* Move all child nodes in source to target
*/
static private void appendContents(Element source, Element target) {
while (source.getFirstChild() != null) {
target.appendChild(source.getFirstChild());
}
}
/**
* Return a map with all <define/> elements. The key in the map is the
* 'name' attribute.
* 'name' attribute. If there are <define/> elements with the same name,
* they are combined according to the rule in the 'combine' attribute.
*/
static private HashMap<String, Element> getDefinitions(Document grammar) {
static HashMap<String, Element> getDefinitions(Document grammar) {
HashMap<String, Element> defs = new HashMap<String, Element>();
XPathResult<Element> elements = XPath.elementIterator(grammar,
"/rng:grammar/rng:define");
for (Element e : elements) {
defs.put(e.getAttribute("name"), e);
final String name = e.getAttribute("name");
final Element def = defs.get(name);
if (def == null) {
defs.put(name, e);
} else {
final String combineMethod = def.hasAttribute("combine") ? def
.getAttribute("combine") : e.getAttribute("combine");
if (!"choice".equals(combineMethod)
&& !"interleave".equals(combineMethod)) {
throw new RuntimeException(
"combine attribute has wrong value '"
+ combineMethod + "' on definition '"
+ name + "'.");
}
Element combine = getFirstElementChild(def);
if (combine != null && isRng(combineMethod, combine)) {
appendContents(e, combine);
} else {
combine = getFirstElementChild(e);
if (combine != null && isRng(combineMethod, combine)) {
appendContents(def, combine);
} else {
combine = e.getOwnerDocument().createElementNS(NC.rng,
def.getPrefix() + ":" + combineMethod);
appendContents(def, combine);
appendContents(e, combine);
}
def.appendChild(combine);
}
def.removeAttribute("combine");
e.getParentNode().removeChild(e);
}
}
return defs;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment