Debian Codegraph @

src/uk/me/parabola/imgfmt/app/Label.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2006,2014 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 10-Jan-2007
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.util.regex.Pattern;
20
21 /**
22 * Labels are used for names of roads, points of interest etc.
23 *
24 * There are different storage formats.
25 *
26 * 1. A 6 bit compact uppercase ascii format, that has escape codes for some
27 * special characters.
28 *
29 * 2. An 8 bit format. This seems to be a fairly straightforward latin-1 like
30 * encoding with no tricks to reduce the amount of space required.
31 *
32 * 3. A multi-byte format. For unicode, cp932 etc.
33 *
34 * @author Steve Ratcliffe
35 */
36 public class Label {
37 public static final Label NULL_LABEL = new Label("");
38 public static final Label NULL_OUT_LABEL = new Label(new char[0]);
39
40 private final String text;
41 private final char[] encText;
42
43 // The offset in to the data section.
44 private int offset;
45
46 public Label(String text) {
47 this.text = text;
48 this.encText = null;
49 }
50
51 public Label(char[] encText) {
52 this.encText = encText;
53 this.text = null;
54 }
55
56 /**
57 * @return a value > 0 if this label is not empty. TODO: replace by isEmpty()
58 */
59 public int getLength() {
60 if (text != null)
61 return text.length();
62 if (encText != null)
63 return encText.length;
64 return 0;
65 }
66
67 public String getText() {
68 assert text != null;
69 return text;
70 }
71
72 public char[] getEncText() {
73 return encText;
74 }
75
76 // highway shields and "thin" separators
77 public final static Pattern SHIELDS = Pattern.compile("[\u0001-\u0006\u001b-\u001c]");
78
79 // "fat" separators
80 private final static Pattern SEPARATORS = Pattern.compile("[\u001d-\u001f]");
81
82 // two or more whitespace characters
83 private final static Pattern SQUASH_SPACES = Pattern.compile("\\s\\s+");
84
85 // DEL character
86 public final static Pattern SQASH_DEL = Pattern.compile("[\u007f]");
87
88 public static String stripGarminCodes(String s) {
89 if(s == null)
90 return null;
91 s = SHIELDS.matcher(s).replaceAll(""); // remove
92 s = SEPARATORS.matcher(s).replaceAll(" "); // replace with a space
93 s = SQUASH_SPACES.matcher(s).replaceAll(" "); // replace with a space
94 // a leading separator would have turned into a space so trim it
95 return s.trim();
96 }
97
98 /**
99 * two or more whitespace characters are replaced by a single space
100 * @param s string to trim
101 * @return the trimmed string or null if s is null or empty
102 */
103 public static String squashSpaces(String s) {
104 if(s == null || s.isEmpty())
105 return null;
106 return SQUASH_SPACES.matcher(s).replaceAll(" "); // replace with single space
107 }
108
109 public static String squashDel(String s) {
110 if(s == null || s.isEmpty())
111 return null;
112 return SQASH_DEL.matcher(s).replaceAll(""); // remove 0x7f=DEL character
113 }
114
115 /**
116 * The offset of this label in the LBL file. The first byte of this file
117 * is zero and an offset of zero means that the label has a zero length/is
118 * empty.
119 *
120 * @return The offset within the LBL file of this string.
121 */
122 public int getOffset() {
123 return offset;
124 }
125
126 public void setOffset(int offset) {
127 this.offset = offset;
128 }
129
130 /**
131 * String version of the label, for diagnostic purposes.
132 */
133 public String toString() {
134 return text != null ? text : "[" + offset + "]";
135 }
136
137 public boolean equals(Object o) {
138 if (this == o) return true;
139 if (o == null || getClass() != o.getClass()) return false;
140
141 return offset == ((Label) o).offset;
142 }
143
144 public int hashCode() {
145 return offset;
146 }
147 }