    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.text.translate;
    018
    019import java.io.IOException;
    020import java.io.Writer;
    021
    022import org.apache.commons.lang3.CharUtils;
    023
    024/**
    025 * Translate escaped octal Strings back to their octal values.
    026 *
    027 * For example, "\45" should go back to being the specific value (a %).
    028 *
    029 * Note that this currently only supports the viable range of octal for Java; namely
    030 * 1 to 377. This is because parsing Java is the main use case.
    031 *
    032 * @since 3.0
    033 * @deprecated As of <a href="https://commons.apache.org/proper/commons-lang/changes-report.html#a3.6">3.6</a>, use Apache Commons Text
    034 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/OctalUnescaper.html">
    035 * OctalUnescaper</a>.
    036 */
    037@Deprecated
    038public class OctalUnescaper extends CharSequenceTranslator {
    039
    040    /**
    041     * Constructs a new instance.
    042     */
    043    public OctalUnescaper() {
    044        // empty
    045    }
    046
    047    /**
    048     * Checks if the given char is the character representation of one of the digit from 0 to 3.
    049     * @param ch the char to check.
    050     * @return true if the given char is the character representation of one of the digits from 0 to 3.
    051     */
    052    private boolean isZeroToThree(final char ch) {
    053        return ch >= '0' && ch <= '3';
    054    }
    055
    056    /**
    057     * {@inheritDoc}
    058     */
    059    @Override
    060    public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
    061        final int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
    062        final StringBuilder builder = new StringBuilder();
    063        if (input.charAt(index) == '\\' && remaining > 0 && CharUtils.isOctal(input.charAt(index + 1))) {
    064            final int next = index + 1;
    065            final int next2 = index + 2;
    066            final int next3 = index + 3;
    067            // we know this is good as we checked it in the if block above
    068            builder.append(input.charAt(next));
    069            if (remaining > 1 && CharUtils.isOctal(input.charAt(next2))) {
    070                builder.append(input.charAt(next2));
    071                if (remaining > 2 && isZeroToThree(input.charAt(next)) && CharUtils.isOctal(input.charAt(next3))) {
    072                    builder.append(input.charAt(next3));
    073                }
    074            }
    075            out.write(Integer.parseInt(builder.toString(), 8));
    076            return 1 + builder.length();
    077        }
    078        return 0;
    079    }
    080}