Debian Codegraph @

src/dr/xml/XMLModelIdentifiable.java

raw symbols SCIP
1 /*
2 * XMLModelIdentifiable.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 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 * Package: XMLModelIdentifiable
37 * Description:
38 * <p/>
39 * <p/>
40 * Created by
41 * Alexander V. Alekseyenko ([email protected])
42 * Date: Apr 13, 2009
43 * Time: 10:38:05 AM
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 }