Debian Codegraph @

src/dr/xml/XMLModelFile.java

raw symbols SCIP
1 /*
2 * XMLModelFile.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Package: XMLModelFile
46 * Description:
47 * <p/>
48 * <p/>
49 * Created by
50 * Alexander V. Alekseyenko ([email protected])
51 * Date: Apr 10, 2009
52 * Time: 12:35:42 PM
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; //doc.getRootElement();
60
61 identifiedElements = new HashMap<String, XMLModelIdentifiable>();
62 /* Parse out all the elements with id's and idref's
63 Create a mapping of parameter names to the corresponding xml elements
64 and a mapping of other identified elements to the corresponding xml elements
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())) {//assign a special name
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 // ListModel implementation
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 //AUTOGENERATED METHOD IMPLEMENTATION
176 }
177
178 public void removeListDataListener(ListDataListener listDataListener) {
179 //AUTOGENERATED METHOD IMPLEMENTATION
180 }
181
182
183 public static void main(String[] args) { //main method for debugging
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 }