001/* 002 * Copyright (C) 2006 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.escape; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.HashMap; 022import java.util.Map; 023import java.util.Map.Entry; 024import javax.annotation.CheckForNull; 025import org.checkerframework.checker.nullness.qual.Nullable; 026 027/** 028 * Simple helper class to build a "sparse" array of objects based on the indexes that were added to 029 * it. The array will be from 0 to the maximum index given. All non-set indexes will contain null 030 * (so it's not really a sparse array, just a pseudo sparse array). The builder can also return a 031 * CharEscaper based on the generated array. 032 * 033 * @author Sven Mawson 034 * @since 15.0 035 */ 036@GwtCompatible 037@ElementTypesAreNonnullByDefault 038public final class CharEscaperBuilder { 039 /** 040 * Simple decorator that turns an array of replacement char[]s into a CharEscaper, this results in 041 * a very fast escape method. 042 */ 043 private static class CharArrayDecorator extends CharEscaper { 044 private final char[] @Nullable [] replacements; 045 private final int replaceLength; 046 047 CharArrayDecorator(char[] @Nullable [] replacements) { 048 this.replacements = replacements; 049 this.replaceLength = replacements.length; 050 } 051 052 /* 053 * Overriding escape method to be slightly faster for this decorator. We test the replacements 054 * array directly, saving a method call. 055 */ 056 @Override 057 public String escape(String s) { 058 int slen = s.length(); 059 for (int index = 0; index < slen; index++) { 060 char c = s.charAt(index); 061 if (c < replacements.length && replacements[c] != null) { 062 return escapeSlow(s, index); 063 } 064 } 065 return s; 066 } 067 068 @Override 069 @CheckForNull 070 protected char[] escape(char c) { 071 return c < replaceLength ? replacements[c] : null; 072 } 073 } 074 075 // Replacement mappings. 076 private final Map<Character, String> map; 077 078 // The highest index we've seen so far. 079 private int max = -1; 080 081 /** Construct a new sparse array builder. */ 082 public CharEscaperBuilder() { 083 this.map = new HashMap<>(); 084 } 085 086 /** Add a new mapping from an index to an object to the escaping. */ 087 @CanIgnoreReturnValue 088 public CharEscaperBuilder addEscape(char c, String r) { 089 map.put(c, checkNotNull(r)); 090 if (c > max) { 091 max = c; 092 } 093 return this; 094 } 095 096 /** Add multiple mappings at once for a particular index. */ 097 @CanIgnoreReturnValue 098 public CharEscaperBuilder addEscapes(char[] cs, String r) { 099 checkNotNull(r); 100 for (char c : cs) { 101 addEscape(c, r); 102 } 103 return this; 104 } 105 106 /** 107 * Convert this builder into an array of char[]s where the maximum index is the value of the 108 * highest character that has been seen. The array will be sparse in the sense that any unseen 109 * index will default to null. 110 * 111 * @return a "sparse" array that holds the replacement mappings. 112 */ 113 public char[] @Nullable [] toArray() { 114 char[][] result = new char[max + 1][]; 115 for (Entry<Character, String> entry : map.entrySet()) { 116 result[entry.getKey()] = entry.getValue().toCharArray(); 117 } 118 return result; 119 } 120 121 /** 122 * Convert this builder into a char escaper which is just a decorator around the underlying array 123 * of replacement char[]s. 124 * 125 * @return an escaper that escapes based on the underlying array. 126 */ 127 public Escaper toEscaper() { 128 return new CharArrayDecorator(toArray()); 129 } 130}