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     */
017    package org.apache.commons.lang3.text.translate;
018    
019    import java.io.IOException;
020    import java.io.Writer;
021    
022    /**
023     * Translates codepoints to their XML numeric entity escaped value.
024     * 
025     * @author Apache Software Foundation
026     * @since 3.0
027     * @version $Id: NumericEntityEscaper.java 967237 2010-07-23 20:08:57Z mbenson $
028     */
029    public class NumericEntityEscaper extends CodePointTranslator {
030    
031        private final int below;
032        private final int above;
033        private final boolean between;
034    
035        private NumericEntityEscaper(int below, int above, boolean between) {
036            this.below = below;
037            this.above = above;
038            this.between = between;
039        }
040    
041        public NumericEntityEscaper() { 
042            this(0, Integer.MAX_VALUE, true);
043        }
044    
045        public static NumericEntityEscaper below(int codepoint) {
046            return outsideOf(codepoint, Integer.MAX_VALUE);
047        }
048    
049        public static NumericEntityEscaper above(int codepoint) {
050            return outsideOf(0, codepoint);
051        }
052    
053        public static NumericEntityEscaper between(int codepointLow, int codepointHigh) {
054            return new NumericEntityEscaper(codepointLow, codepointHigh, true);
055        }
056    
057        public static NumericEntityEscaper outsideOf(int codepointLow, int codepointHigh) {
058            return new NumericEntityEscaper(codepointLow, codepointHigh, false);
059        }
060    
061        /**
062         * {@inheritDoc}
063         */
064        @Override
065        public boolean translate(int codepoint, Writer out) throws IOException {
066            if(between) {
067                if (codepoint < below || codepoint > above) {
068                    return false;
069                }
070            } else {
071                if (codepoint >= below && codepoint <= above) {
072                    return false;
073                }
074            }
075    
076            out.write("&#");
077            out.write(Integer.toString(codepoint, 10));
078            out.write(';');
079            return true;
080        }
081    }