You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Native2AsciiUtils.java 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.util;
  19. /**
  20. * Contains helper methods for Ant's built-in implementation of native2ascii.
  21. *
  22. * @since Ant 1.9.8
  23. */
  24. public class Native2AsciiUtils {
  25. private static final int MAX_ASCII = 127;
  26. /**
  27. * Replaces non-ASCII characters with their Unicode-Escapes.
  28. * <p>Expects to be called once per line if applied to a file.</p>
  29. * @param line the input line
  30. * @return the translated line
  31. */
  32. public static String native2ascii(String line) {
  33. StringBuilder sb = new StringBuilder();
  34. for (char c : line.toCharArray()) {
  35. if (c <= MAX_ASCII) {
  36. sb.append(c);
  37. } else {
  38. sb.append("\\u");
  39. String encoded = Integer.toHexString(c);
  40. for (int i = encoded.length(); i < 4; i++) {
  41. sb.append("0");
  42. }
  43. sb.append(encoded);
  44. }
  45. }
  46. return sb.toString();
  47. }
  48. /**
  49. * Replaces Unicode-Escapes.
  50. * <p>Expects to be called once per line if applied to a file.</p>
  51. * @param line the input line
  52. * @return the translated line
  53. */
  54. public static String ascii2native(String line) {
  55. StringBuilder sb = new StringBuilder();
  56. int inputLen = line.length();
  57. for (int i = 0; i < inputLen; i++) {
  58. char c = line.charAt(i);
  59. if (c != '\\' || i >= inputLen - 5) {
  60. sb.append(c);
  61. } else { // backslash with enough remaining characters
  62. char u = line.charAt(++i);
  63. if (u == 'u') {
  64. int unescaped = tryParse(line, i + 1);
  65. if (unescaped >= 0) {
  66. sb.append((char) unescaped);
  67. i += 4;
  68. continue;
  69. }
  70. }
  71. // not a unicode escape
  72. sb.append(c).append(u);
  73. }
  74. }
  75. return sb.toString();
  76. }
  77. private static int tryParse(String line, int startIdx) {
  78. try {
  79. return Integer.parseInt(line.substring(startIdx, startIdx + 4), 16);
  80. } catch (NumberFormatException ex) {
  81. return -1;
  82. }
  83. }
  84. }