OctalUnescaper.java

  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.  *      https://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. package org.apache.commons.lang3.text.translate;

  18. import java.io.IOException;
  19. import java.io.Writer;

  20. import org.apache.commons.lang3.CharUtils;

  21. /**
  22.  * Translate escaped octal Strings back to their octal values.
  23.  *
  24.  * For example, "\45" should go back to being the specific value (a %).
  25.  *
  26.  * Note that this currently only supports the viable range of octal for Java; namely
  27.  * 1 to 377. This is because parsing Java is the main use case.
  28.  *
  29.  * @since 3.0
  30.  * @deprecated As of <a href="https://commons.apache.org/proper/commons-lang/changes-report.html#a3.6">3.6</a>, use Apache Commons Text
  31.  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/OctalUnescaper.html">
  32.  * OctalUnescaper</a>.
  33.  */
  34. @Deprecated
  35. public class OctalUnescaper extends CharSequenceTranslator {

  36.     /**
  37.      * Constructs a new instance.
  38.      */
  39.     public OctalUnescaper() {
  40.         // empty
  41.     }

  42.     /**
  43.      * Checks if the given char is the character representation of one of the digit from 0 to 3.
  44.      * @param ch the char to check
  45.      * @return true if the given char is the character representation of one of the digits from 0 to 3
  46.      */
  47.     private boolean isZeroToThree(final char ch) {
  48.         return ch >= '0' && ch <= '3';
  49.     }

  50.     /**
  51.      * {@inheritDoc}
  52.      */
  53.     @Override
  54.     public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
  55.         final int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
  56.         final StringBuilder builder = new StringBuilder();
  57.         if (input.charAt(index) == '\\' && remaining > 0 && CharUtils.isOctal(input.charAt(index + 1))) {
  58.             final int next = index + 1;
  59.             final int next2 = index + 2;
  60.             final int next3 = index + 3;
  61.             // we know this is good as we checked it in the if block above
  62.             builder.append(input.charAt(next));
  63.             if (remaining > 1 && CharUtils.isOctal(input.charAt(next2))) {
  64.                 builder.append(input.charAt(next2));
  65.                 if (remaining > 2 && isZeroToThree(input.charAt(next)) && CharUtils.isOctal(input.charAt(next3))) {
  66.                     builder.append(input.charAt(next3));
  67.                 }
  68.             }
  69.             out.write(Integer.parseInt(builder.toString(), 8));
  70.             return 1 + builder.length();
  71.         }
  72.         return 0;
  73.     }
  74. }