PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/burto/mkgmap
Java | 155 lines | 94 code | 19 blank | 42 comment | 16 complexity | 07369f4f951dd218e5c09cc3a092e4f3 MD5 | raw file
Possible License(s): GPL-2.0
  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: Jan 1, 2008
  16. */
  17. package uk.me.parabola.imgfmt.app.labelenc;
  18. /**
  19. * @author Steve Ratcliffe
  20. */
  21. public class CodeFunctions {
  22. // Label encoding length
  23. public static final int ENCODING_FORMAT6 = 6;
  24. private static final int ENCODING_FORMAT9 = 9;
  25. private static final int ENCODING_FORMAT10 = 10;
  26. private int codepage;
  27. private int encodingType;
  28. private CharacterEncoder encoder;
  29. private CharacterDecoder decoder;
  30. protected void setEncoder(CharacterEncoder encoder) {
  31. this.encoder = encoder;
  32. }
  33. public CharacterEncoder getEncoder() {
  34. return encoder;
  35. }
  36. protected void setDecoder(CharacterDecoder decoder) {
  37. this.decoder = decoder;
  38. }
  39. public CharacterDecoder getDecoder() {
  40. return decoder;
  41. }
  42. public int getEncodingType() {
  43. return encodingType;
  44. }
  45. protected void setEncodingType(int encodingType) {
  46. this.encodingType = encodingType;
  47. }
  48. public int getCodepage() {
  49. return codepage;
  50. }
  51. public void setCodepage(int codepage) {
  52. this.codepage = codepage;
  53. }
  54. /**
  55. * Create a CharacterEncoder for the given charset option. Note that this
  56. * routine also writes to the lblHeader parameter to set the encoding type.
  57. * @param charset The mkgmap command line option to be interpreted.
  58. * @return The various character set parameters that will be needed.
  59. */
  60. public static CodeFunctions createEncoderForLBL(String charset) {
  61. CodeFunctions funcs = new CodeFunctions();
  62. if ("ascii".equals(charset)) {
  63. funcs.setEncodingType(ENCODING_FORMAT6);
  64. funcs.setEncoder(new Format6Encoder());
  65. funcs.setDecoder(new Format6Decoder());
  66. } else if ("latin1".equals(charset)) {
  67. funcs.setEncodingType(ENCODING_FORMAT9);
  68. funcs.setEncoder(new LatinEncoder());
  69. funcs.setDecoder(new AnyCharsetDecoder("cp1252"));
  70. funcs.setCodepage(1252);
  71. } else if ("unicode".equals(charset)) {
  72. funcs.setEncodingType(ENCODING_FORMAT10);
  73. funcs.setEncoder(new Utf8Encoder());
  74. funcs.setDecoder(new Utf8Decoder());
  75. } else if ("simple8".equals(charset)) {
  76. funcs.setEncodingType(ENCODING_FORMAT9);
  77. funcs.setEncoder(new Simple8Encoder());
  78. } else {
  79. funcs.setEncodingType(ENCODING_FORMAT9);
  80. funcs.setEncoder(new AnyCharsetEncoder(charset));
  81. guessCodepage(funcs, charset);
  82. }
  83. return funcs;
  84. }
  85. /**
  86. * Guess the code page from the given charset. Only works with things
  87. * like cp1252, windows-1252 and some well known ones.
  88. * @param funcs The code page functions.
  89. * @param charset The charset that was given.
  90. */
  91. private static void guessCodepage(CodeFunctions funcs, String charset) {
  92. String cs = charset.toLowerCase();
  93. if (cs.startsWith("cp")) {
  94. try {
  95. funcs.setCodepage(Integer.parseInt(charset.substring(2)));
  96. } catch (NumberFormatException e) {
  97. // wasn't in the right form
  98. }
  99. } else if (cs.startsWith("windows-")) {
  100. try {
  101. funcs.setCodepage(Integer.parseInt(charset.substring(8)));
  102. } catch (NumberFormatException e) {
  103. // wasn't in the right form to guess
  104. }
  105. } else if (cs.equals("latin1")) {
  106. funcs.setCodepage(1252);
  107. }
  108. }
  109. /**
  110. * Sets encoding functions for a given format and code page. This is used
  111. * when reading from an existing file.
  112. *
  113. * @param format The format from the lbl header.
  114. * @return The various character set parameters that will be needed.
  115. */
  116. public static CodeFunctions createEncoderForLBL(int format) {
  117. CodeFunctions funcs = new CodeFunctions();
  118. if (format == ENCODING_FORMAT6) {
  119. funcs.setEncodingType(ENCODING_FORMAT6);
  120. funcs.setEncoder(new Format6Encoder());
  121. funcs.setDecoder(new Format6Decoder());
  122. } else {
  123. // TODO TEMP...
  124. funcs.setEncodingType(ENCODING_FORMAT9);
  125. funcs.setEncoder(new AnyCharsetEncoder("cp1252"));
  126. funcs.setDecoder(new AnyCharsetDecoder("cp1252"));
  127. }
  128. return funcs;
  129. }
  130. public static CharacterEncoder getDefaultEncoder() {
  131. return new Format6Encoder();
  132. }
  133. public static CharacterDecoder getDefaultDecoder() {
  134. return new Format6Decoder();
  135. }
  136. }