1 package fr.ove.openmath.jome.ctrl.mml;
2
3 import java.util.*;
4
5 /***
6 * A class for the representation of an tag element (XML element).<BR>
7 * By default, the class represents a start empty tag.
8 *
9 * @author © 2000 DIRAT Laurent
10 * @version 1.0 25/05/2000
11 */
12 public class Tag {
13 /***
14 * The name of the tag.
15 */
16 private String name;
17
18 /***
19 * The list of attributes.
20 */
21 private Vector attributes = new Vector();
22
23 /***
24 * Indicates if this is an empty element or not.<BR>
25 * By default, it is an empty element.
26 */
27 private boolean isEmpty = true;
28
29 /***
30 * Indicates if it is a start tag or an end tag.<BR>
31 * By default, it is a start element.
32 */
33 private boolean isStart = true;
34
35 /***
36 * The default constructor.<BR>
37 * The default is an empty element, with "name" name and no attributes
38 */
39 public Tag() {
40 this("name", true, true);
41 }
42
43 /***
44 * The constructor.
45 * Constructs an empty element with the specified name.
46 * @param name the name of the element
47 */
48 public Tag(String name) {
49 this(name, true, true);
50 }
51
52 /***
53 * The constructor.
54 * @param name the name of the element
55 * @param isStart <CODE>true</CODE> if the tag is an start one. <CODE>false</CODE> otherwise.
56 * @param isEmpty <CODE>true</CODE> if the element is an empty one. <CODE>false</CODE> otherwise.
57 */
58 public Tag(String name, boolean isStart, boolean isEmpty) {
59 this.name = name;
60 this.isStart = isStart;
61 if (!isStart)
62 this.isEmpty = false;
63 else
64 this.isEmpty = isEmpty;
65 }
66
67
68 /***
69 * Sets the name of the element.
70 * @param name the name of the element.
71 */
72 public void setName(String name) {
73 this.name = name;
74 }
75
76 /***
77 * Returns the name of the element.
78 */
79 public String getName() {
80 return name;
81 }
82
83 /***
84 * Adds an attribute to the element.
85 * @param att the attribute to add.
86 */
87 public void addAttribute(Attribute att) {
88 attributes.addElement(att);
89 }
90
91 /***
92 * Returns <CODE>true</CODE> if the element is an empty one. <CODE>false</CODE> otherwise.
93 */
94 public boolean isEmpty() {
95 return isEmpty;
96 }
97
98 /***
99 * Sets the element as empty.
100 * @param isEmpty <CODE>true</CODE> if the element is an empty one. <CODE>false</CODE> otherwise.
101 */
102 public void setIsEmpty(boolean isEmpty) {
103 this.isEmpty = isEmpty;
104 }
105
106 /***
107 * Returns <CODE>true</CODE> if the tag is an start one. <CODE>false</CODE> otherwise.
108 */
109 public boolean isStart() {
110 return isStart;
111 }
112
113 /***
114 * Sets the element as start element.
115 * @param isStart <CODE>true</CODE> if the element is an start one. <CODE>false</CODE> otherwise.
116 */
117 public void setIsStart(boolean isStart) {
118 this.isStart = isStart;
119 }
120
121 /***
122 * Returns <CODE>true</CODE> if the instance if the end tag of the specified one. <CODE>false</CODE>
123 * otherwise.
124 * @param tag the specified start tag
125 */
126 public boolean isEndTag(Tag start) {
127 boolean isEndTag = false;
128
129 isEndTag = (!isStart && start.isStart && name.equals(start.name));
130
131 return isEndTag;
132 }
133
134 /***
135 * Returns the string representation of the element.
136 */
137 public String toString() {
138 String toString = "<";
139
140 if (isStart) {
141 toString += name;
142
143 int count = attributes.size();
144 for (int i = 0; i < count; i++)
145 toString += " " + ((Attribute) attributes.elementAt(i)).toString();
146
147 if (isEmpty)
148 toString += "/";
149 }
150 else
151 toString += "/" + name;
152
153 toString += ">";
154
155 return toString;
156 }
157 }
158