| 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 static dr.util.HeapSort.sort; |
| 29 |
import org.jdom.Attribute; |
| 30 |
import org.jdom.Document; |
| 31 |
import org.jdom.Element; |
| 32 |
import org.jdom.JDOMException; |
| 33 |
import org.jdom.filter.Filter; |
| 34 |
import org.jdom.input.SAXBuilder; |
| 35 |
import org.jdom.output.XMLOutputter; |
| 36 |
|
| 37 |
import javax.swing.*; |
| 38 |
import javax.swing.event.ListDataListener; |
| 39 |
import java.io.File; |
| 40 |
import java.io.IOException; |
| 41 |
import java.io.OutputStream; |
| 42 |
import java.util.*; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class XMLModelFile implements ListModel { |
| 55 |
private final Element xmlModel; |
| 56 |
private final Map<String, XMLModelIdentifiable> identifiedElements; |
| 57 |
|
| 58 |
public XMLModelFile(Element root) { |
| 59 |
xmlModel = root; |
| 60 |
|
| 61 |
identifiedElements = new HashMap<String, XMLModelIdentifiable>(); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Iterator identifiedIter = xmlModel.getDescendants(new IdentifiedFilter()); |
| 68 |
|
| 69 |
while (identifiedIter.hasNext()) { |
| 70 |
Element child = (Element) identifiedIter.next(); |
| 71 |
Attribute idAttr = child.getAttribute("id"); |
| 72 |
if (idAttr != null) { |
| 73 |
identifiedElements.put(idAttr.getValue(), new XMLModelIdentifiable(idAttr.getValue(), child)); |
| 74 |
} else { |
| 75 |
idAttr = child.getAttribute("idref"); |
| 76 |
identifiedElements.get(idAttr.getValue()).addReference(child); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
Iterator mcmcIter = xmlModel.getDescendants(new MCMCFilter()); |
| 81 |
Element mcmc = (Element) mcmcIter.next(); |
| 82 |
if (mcmc == null) { |
| 83 |
System.err.println("Error: cannot find mcmc element"); |
| 84 |
} else { |
| 85 |
for (Object child : mcmc.getChildren("posterior")) { |
| 86 |
if ((child instanceof Element)) { |
| 87 |
Element posteriorElement = (Element) child; |
| 88 |
} |
| 89 |
} |
| 90 |
mcmc.detach(); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
public Set<String> getIdentifiedElementNames() { |
| 96 |
return identifiedElements.keySet(); |
| 97 |
} |
| 98 |
|
| 99 |
public void prefixIdentifiedNames(String prefix, Map<String, String> special, boolean keepID) { |
| 100 |
if (special == null) { |
| 101 |
special = new HashMap<String, String>(); |
| 102 |
} |
| 103 |
|
| 104 |
for (Map.Entry<String, XMLModelIdentifiable> stringXMLModelIdentifiableEntry : identifiedElements.entrySet()) { |
| 105 |
String newName; |
| 106 |
if (special.containsKey(stringXMLModelIdentifiableEntry.getKey())) { |
| 107 |
newName = special.get(stringXMLModelIdentifiableEntry.getKey()); |
| 108 |
if (keepID) { |
| 109 |
stringXMLModelIdentifiableEntry.getValue().restoreDefinition(); |
| 110 |
} else { |
| 111 |
stringXMLModelIdentifiableEntry.getValue().removeDefinition(); |
| 112 |
} |
| 113 |
} else { |
| 114 |
newName = prefix + stringXMLModelIdentifiableEntry.getKey(); |
| 115 |
} |
| 116 |
stringXMLModelIdentifiableEntry.getValue().rename(newName); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public void printIdentified() { |
| 121 |
System.out.println("Identified elements follow"); |
| 122 |
XMLOutputter outputter = new XMLOutputter(); |
| 123 |
for (Map.Entry<String, XMLModelIdentifiable> stringXMLModelIdentifiableEntry : identifiedElements.entrySet()) { |
| 124 |
System.out.println("Original name: " + stringXMLModelIdentifiableEntry.getKey()); |
| 125 |
stringXMLModelIdentifiableEntry.getValue().print(outputter, System.out); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public void print(XMLOutputter outputter, OutputStream ostream) { |
| 130 |
try { |
| 131 |
outputter.output(xmlModel.getContent(), ostream); |
| 132 |
} |
| 133 |
catch (IOException e) { |
| 134 |
System.err.println("Error writing model!"); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
public class MCMCFilter implements Filter { |
| 140 |
public boolean matches(Object o) { |
| 141 |
return (o instanceof Element) && ((Element) o).getName().equals("mcmc"); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public class IdentifiedFilter implements Filter { |
| 146 |
|
| 147 |
public boolean matches(Object o) { |
| 148 |
if (!(o instanceof Element)) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
return ((Element) o).getAttribute("id") != null || ((Element) o).getAttribute("idref") != null; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public int getSize() { |
| 158 |
return identifiedElements.size(); |
| 159 |
} |
| 160 |
|
| 161 |
public Object getElementAt(int i) { |
| 162 |
if (i < identifiedElements.size()) { |
| 163 |
Object[] elements = identifiedElements.keySet().toArray(); |
| 164 |
sort(elements, new Comparator() { |
| 165 |
public int compare(Object o, Object o1) { |
| 166 |
return o.toString().compareTo(o1.toString()); |
| 167 |
} |
| 168 |
}); |
| 169 |
return elements[i]; |
| 170 |
} |
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
public void addListDataListener(ListDataListener listDataListener) { |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
public void removeListDataListener(ListDataListener listDataListener) { |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
public static void main(String[] args) { |
| 184 |
SAXBuilder parser = new SAXBuilder(); |
| 185 |
|
| 186 |
Document doc; |
| 187 |
try { |
| 188 |
doc = parser.build(new File("testSimplePathSampling.xml")); |
| 189 |
|
| 190 |
XMLModelFile z = new XMLModelFile(doc.getRootElement()); |
| 191 |
|
| 192 |
z.printIdentified(); |
| 193 |
HashMap<String, String> hm = new HashMap<String, String>(); |
| 194 |
hm.put("samplingMean", "samplingMean"); |
| 195 |
z.prefixIdentifiedNames("model1.", hm, false); |
| 196 |
z.printIdentified(); |
| 197 |
} catch (IOException e) { |
| 198 |
|
| 199 |
} |
| 200 |
catch (JDOMException e) { |
| 201 |
|
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
} |