1
2 package org.galagosearch.core.parse;
3
4 import java.util.Map.Entry;
5 import java.util.Map;
6
7 /***
8 * This class represents a tag in a XML/HTML document.
9 *
10 * A tag has a name, an optional set of attributes, a beginning position and an
11 * end position. The positions are in terms of tokens, so if begin = 5, that means
12 * the open tag is between token 5 and token 6.
13 *
14 * @author trevor
15 */
16 public class Tag implements Comparable<Tag> {
17 /***
18 * Constructs a tag.
19 *
20 * @param name The name of the tag.
21 * @param attributes Attributes of the tag.
22 * @param begin Location of the start tag within the document, in tokens.
23 * @param end Location of the end tag within the document, in tokens.
24 */
25 public Tag(String name, Map<String, String> attributes, int begin, int end) {
26 this.name = name;
27 this.attributes = attributes;
28 this.begin = begin;
29 this.end = end;
30 }
31
32 /***
33 * Compares two tags together. Tags are ordered by the location of
34 * the open tag. If we find two tags opening at the same location, the tie
35 * is broken by the location of the closing tag.
36 *
37 * @param other
38 * @return
39 */
40 public int compareTo(Tag other) {
41 int deltaBegin = begin - other.begin;
42 if (deltaBegin == 0) {
43 return other.end - end;
44 }
45 return deltaBegin;
46 }
47
48 @Override
49 public String toString() {
50 StringBuilder builder = new StringBuilder();
51
52 builder.append("<");
53 builder.append(name);
54
55 for (Entry<String, String> entry : attributes.entrySet()) {
56 builder.append(' ');
57 builder.append(entry.getKey());
58 builder.append('=');
59 builder.append('"');
60 builder.append(entry.getValue());
61 builder.append('"');
62 }
63
64 builder.append('>');
65 return builder.toString();
66 }
67 public String name;
68 public Map<String, String> attributes;
69 public int begin;
70 public int end;
71 }