| 1 | /* Copyright (c) 2008 Google Inc.
|
|---|
| 2 | *
|
|---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 4 | * you may not use this file except in compliance with the License.
|
|---|
| 5 | * You may obtain a copy of the License at
|
|---|
| 6 | *
|
|---|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 8 | *
|
|---|
| 9 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 12 | * See the License for the specific language governing permissions and
|
|---|
| 13 | * limitations under the License.
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | package com.google.gdata.util.common.base;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * An object that converts literal text into a format safe for inclusion in a
|
|---|
| 21 | * particular context (such as an XML document). Typically (but not always), the
|
|---|
| 22 | * inverse process of "unescaping" the text is performed automatically by the
|
|---|
| 23 | * relevant parser.
|
|---|
| 24 | *
|
|---|
| 25 | * <p>For example, an XML escaper would convert the literal string {@code
|
|---|
| 26 | * "Foo<Bar>"} into {@code "Foo<Bar>"} to prevent {@code "<Bar>"} from
|
|---|
| 27 | * being confused with an XML tag. When the resulting XML document is parsed,
|
|---|
| 28 | * the parser API will return this text as the original literal string {@code
|
|---|
| 29 | * "Foo<Bar>"}.
|
|---|
| 30 | *
|
|---|
| 31 | * <p>An {@code Escaper} instance is required to be stateless, and safe when
|
|---|
| 32 | * used concurrently by multiple threads.
|
|---|
| 33 | *
|
|---|
| 34 | * <p>Several popular escapers are defined as constants in the class {@link
|
|---|
| 35 | * CharEscapers}. To create your own escapers, use {@link
|
|---|
| 36 | * CharEscaperBuilder}, or extend {@link CharEscaper} or {@code UnicodeEscaper}.
|
|---|
| 37 | *
|
|---|
| 38 | *
|
|---|
| 39 | */
|
|---|
| 40 | public interface Escaper {
|
|---|
| 41 | /**
|
|---|
| 42 | * Returns the escaped form of a given literal string.
|
|---|
| 43 | *
|
|---|
| 44 | * <p>Note that this method may treat input characters differently depending on
|
|---|
| 45 | * the specific escaper implementation.
|
|---|
| 46 | * <ul>
|
|---|
| 47 | * <li>{@link UnicodeEscaper} handles
|
|---|
| 48 | * <a href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> correctly,
|
|---|
| 49 | * including surrogate character pairs. If the input is badly formed the
|
|---|
| 50 | * escaper should throw {@link IllegalArgumentException}.
|
|---|
| 51 | * <li>{@link CharEscaper} handles Java characters independently and does not
|
|---|
| 52 | * verify the input for well formed characters. A CharEscaper should not be
|
|---|
| 53 | * used in situations where input is not guaranteed to be restricted to the
|
|---|
| 54 | * Basic Multilingual Plane (BMP).
|
|---|
| 55 | * </ul>
|
|---|
| 56 | *
|
|---|
| 57 | * @param string the literal string to be escaped
|
|---|
| 58 | * @return the escaped form of {@code string}
|
|---|
| 59 | * @throws NullPointerException if {@code string} is null
|
|---|
| 60 | * @throws IllegalArgumentException if {@code string} contains badly formed
|
|---|
| 61 | * UTF-16 or cannot be escaped for any other reason
|
|---|
| 62 | */
|
|---|
| 63 | public String escape(String string);
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Returns an {@code Appendable} instance which automatically escapes all
|
|---|
| 67 | * text appended to it before passing the resulting text to an underlying
|
|---|
| 68 | * {@code Appendable}.
|
|---|
| 69 | *
|
|---|
| 70 | * <p>Note that this method may treat input characters differently depending on
|
|---|
| 71 | * the specific escaper implementation.
|
|---|
| 72 | * <ul>
|
|---|
| 73 | * <li>{@link UnicodeEscaper} handles
|
|---|
| 74 | * <a href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a> correctly,
|
|---|
| 75 | * including surrogate character pairs. If the input is badly formed the
|
|---|
| 76 | * escaper should throw {@link IllegalArgumentException}.
|
|---|
| 77 | * <li>{@link CharEscaper} handles Java characters independently and does not
|
|---|
| 78 | * verify the input for well formed characters. A CharEscaper should not be
|
|---|
| 79 | * used in situations where input is not guaranteed to be restricted to the
|
|---|
| 80 | * Basic Multilingual Plane (BMP).
|
|---|
| 81 | * </ul>
|
|---|
| 82 | *
|
|---|
| 83 | * @param out the underlying {@code Appendable} to append escaped output to
|
|---|
| 84 | * @return an {@code Appendable} which passes text to {@code out} after
|
|---|
| 85 | * escaping it.
|
|---|
| 86 | */
|
|---|
| 87 | public Appendable escape(Appendable out);
|
|---|
| 88 | }
|
|---|