Debian Codegraph @

src/uk/me/parabola/imgfmt/app/labelenc/BaseEncoder.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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: 14-Jan-2007
16 */
17 package uk.me.parabola.imgfmt.app.labelenc;
18
19 import java.nio.charset.Charset;
20
21 import uk.me.parabola.log.Logger;
22
23 /**
24 * Useful routines for the other encoders.
25 * Provides some default behaviour when a conversion is not possible for
26 * example.
27 *
28 * @author Steve Ratcliffe
29 */
30 public class BaseEncoder {
31 private static final Logger log = Logger.getLogger(BaseEncoder.class);
32
33 public static final EncodedText NO_TEXT = new EncodedText(null, 0, null);
34
35 private boolean charsetSupported = true;
36
37 // Whether to uppercase the labels or not. Default is true because many
38 // GPS devices do not display labels in lower case.
39 private boolean upperCase;
40
41 protected boolean isCharsetSupported() {
42 return charsetSupported;
43 }
44
45 protected void prepareForCharacterSet(String name) {
46 if (Charset.isSupported(name)) {
47 charsetSupported = true;
48 } else {
49 charsetSupported = false;
50 log.warn("requested character set not found " + name);
51 }
52 }
53
54 protected EncodedText simpleEncode(String text) {
55 if (text == null)
56 return NO_TEXT;
57
58 char[] in = text.toCharArray();
59 byte[] out = new byte[in.length + 1];
60
61 int off = 0;
62 for (char c : in)
63 out[off++] = (byte) (c & 0xff);
64
65 return new EncodedText(out, out.length, in);
66 }
67
68 public boolean isUpperCase() {
69 return upperCase;
70 }
71
72 public void setUpperCase(boolean upperCase) {
73 this.upperCase = upperCase;
74 }
75 }