| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.xml; |
| 27 |
|
| 28 |
import org.jdom.Element; |
| 29 |
import org.jdom.output.XMLOutputter; |
| 30 |
|
| 31 |
import java.io.IOException; |
| 32 |
import java.io.OutputStream; |
| 33 |
import java.util.LinkedList; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class XMLModelIdentifiable { |
| 46 |
private String name; |
| 47 |
private Element definition; |
| 48 |
private Element definitionIdref; |
| 49 |
private boolean defined; |
| 50 |
private LinkedList<Element> references; |
| 51 |
|
| 52 |
public XMLModelIdentifiable(String name, Element definition) { |
| 53 |
this.name = name; |
| 54 |
this.definition = definition; |
| 55 |
definitionIdref = new Element(definition.getName()); |
| 56 |
|
| 57 |
definitionIdref.setAttribute("idref", name); |
| 58 |
|
| 59 |
defined = true; |
| 60 |
references = new LinkedList<Element>(); |
| 61 |
} |
| 62 |
|
| 63 |
public void removeDefinition() { |
| 64 |
if (defined) { |
| 65 |
definition.getParentElement().addContent(definitionIdref); |
| 66 |
definition.detach(); |
| 67 |
defined = false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public void restoreDefinition() { |
| 72 |
if (!defined) { |
| 73 |
definitionIdref.getParentElement().addContent(definition); |
| 74 |
definitionIdref.detach(); |
| 75 |
defined = true; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
public void rename(String newName) { |
| 81 |
name = newName; |
| 82 |
definition.setAttribute("id", newName); |
| 83 |
definitionIdref.setAttribute("idref", newName); |
| 84 |
for (Element ref : references) { |
| 85 |
ref.setAttribute("idref", newName); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public void addReference(Element newRef) { |
| 91 |
if (newRef.getAttribute("idref").getValue().equals(name)) { |
| 92 |
references.addLast(newRef); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public void print(XMLOutputter outputter, OutputStream ostream) { |
| 97 |
if (ostream == null) { |
| 98 |
ostream = System.out; |
| 99 |
} |
| 100 |
try { |
| 101 |
if (defined) { |
| 102 |
outputter.output(definition, ostream); |
| 103 |
} else { |
| 104 |
outputter.output(definitionIdref, ostream); |
| 105 |
} |
| 106 |
for (Element ref : references) { |
| 107 |
outputter.output(ref, ostream); |
| 108 |
} |
| 109 |
|
| 110 |
} catch (IOException e) { |
| 111 |
System.err.println(e); |
| 112 |
} |
| 113 |
} |
| 114 |
} |