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 *      http://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.text.translate;
018
019import java.io.IOException;
020import java.io.Writer;
021
022/**
023 * Translate escaped octal Strings back to their octal values.
024 *
025 * For example, "\45" should go back to being the specific value (a %).
026 *
027 * Note that this currently only supports the viable range of octal for Java; namely 
028 * 1 to 377. This is because parsing Java is the main use case.
029 *
030 * @since 1.0
031 */
032public class OctalUnescaper extends CharSequenceTranslator {
033
034    /**
035     * {@inheritDoc}
036     */
037    @Override
038    public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
039        final int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
040        final StringBuilder builder = new StringBuilder();
041        if(input.charAt(index) == '\\' && remaining > 0 && isOctalDigit(input.charAt(index + 1)) ) {
042            final int next = index + 1;
043            final int next2 = index + 2;
044            final int next3 = index + 3;
045
046            // we know this is good as we checked it in the if block above
047            builder.append(input.charAt(next));
048
049            if(remaining > 1 && isOctalDigit(input.charAt(next2))) {
050                builder.append(input.charAt(next2));
051                if(remaining > 2 && isZeroToThree(input.charAt(next)) && isOctalDigit(input.charAt(next3))) {
052                    builder.append(input.charAt(next3));
053                }
054            }
055
056            out.write( Integer.parseInt(builder.toString(), 8) );
057            return 1 + builder.length();
058        }
059        return 0;
060    }
061
062    /**
063     * Checks if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.
064     * @param ch the char to check
065     * @return true if the given char is the character representation of one of the digits from 0 to 7
066     */
067    private boolean isOctalDigit(final char ch) {
068        return ch >= '0' && ch <= '7';
069    }
070
071    /**
072     * Checks if the given char is the character representation of one of the digit from 0 to 3.
073     * @param ch the char to check
074     * @return true if the given char is the character representation of one of the digits from 0 to 3
075     */
076    private boolean isZeroToThree(final char ch) {
077        return ch >= '0' && ch <= '3';
078    }
079}