| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Tag |
|
| 0.0;0 |
| 1 | // BSD License (http://www.galagosearch.org/license) | |
| 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 | 12 | 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 | 12 | public Tag(String name, Map<String, String> attributes, int begin, int end) { |
| 26 | 12 | this.name = name; |
| 27 | 12 | this.attributes = attributes; |
| 28 | 12 | this.begin = begin; |
| 29 | 12 | this.end = end; |
| 30 | 12 | } |
| 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 | 12 | int deltaBegin = begin - other.begin; |
| 42 | 12 | if (deltaBegin == 0) { |
| 43 | 4 | return other.end - end; |
| 44 | } | |
| 45 | 8 | return deltaBegin; |
| 46 | } | |
| 47 | ||
| 48 | @Override | |
| 49 | public String toString() { | |
| 50 | 0 | StringBuilder builder = new StringBuilder(); |
| 51 | ||
| 52 | 0 | builder.append("<"); |
| 53 | 0 | builder.append(name); |
| 54 | ||
| 55 | 0 | for (Entry<String, String> entry : attributes.entrySet()) { |
| 56 | 0 | builder.append(' '); |
| 57 | 0 | builder.append(entry.getKey()); |
| 58 | 0 | builder.append('='); |
| 59 | 0 | builder.append('"'); |
| 60 | 0 | builder.append(entry.getValue()); |
| 61 | 0 | builder.append('"'); |
| 62 | } | |
| 63 | ||
| 64 | 0 | builder.append('>'); |
| 65 | 0 | return builder.toString(); |
| 66 | } | |
| 67 | public String name; | |
| 68 | public Map<String, String> attributes; | |
| 69 | public int begin; | |
| 70 | public int end; | |
| 71 | } |