1*650b9f74SAndroid Build Coastguard Worker /* 2*650b9f74SAndroid Build Coastguard Worker * Copyright (C) 2010 Google Inc. 3*650b9f74SAndroid Build Coastguard Worker * 4*650b9f74SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*650b9f74SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*650b9f74SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*650b9f74SAndroid Build Coastguard Worker * 8*650b9f74SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*650b9f74SAndroid Build Coastguard Worker * 10*650b9f74SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*650b9f74SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*650b9f74SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*650b9f74SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*650b9f74SAndroid Build Coastguard Worker * limitations under the License. 15*650b9f74SAndroid Build Coastguard Worker */ 16*650b9f74SAndroid Build Coastguard Worker 17*650b9f74SAndroid Build Coastguard Worker package com.google.streamhtmlparser; 18*650b9f74SAndroid Build Coastguard Worker 19*650b9f74SAndroid Build Coastguard Worker import com.google.common.base.Preconditions; 20*650b9f74SAndroid Build Coastguard Worker 21*650b9f74SAndroid Build Coastguard Worker /** 22*650b9f74SAndroid Build Coastguard Worker * A representation of the parser state suitable for use by the caller 23*650b9f74SAndroid Build Coastguard Worker * of the Parser. The meaning of each state and therefore which action 24*650b9f74SAndroid Build Coastguard Worker * the caller should perform on that state is not self-evident. In particular, 25*650b9f74SAndroid Build Coastguard Worker * it depends on which parser is used (currently {@link HtmlParser} and 26*650b9f74SAndroid Build Coastguard Worker * {@link JavascriptParser}). For examples, you will have to look 27*650b9f74SAndroid Build Coastguard Worker * at the <code>Google Template System</code> and <code>ClearSilver</code> 28*650b9f74SAndroid Build Coastguard Worker * both of which support Auto-Escaping by interfacing with our parser 29*650b9f74SAndroid Build Coastguard Worker * (using the parser written in C++). 30*650b9f74SAndroid Build Coastguard Worker * 31*650b9f74SAndroid Build Coastguard Worker * <p>The caller of the Parser will query for the current parser state at 32*650b9f74SAndroid Build Coastguard Worker * points of interest during parsing of templates. Based on the parser's 33*650b9f74SAndroid Build Coastguard Worker * current state as represented by this class, the caller can determine 34*650b9f74SAndroid Build Coastguard Worker * the appropriate escaping to apply. 35*650b9f74SAndroid Build Coastguard Worker * 36*650b9f74SAndroid Build Coastguard Worker * <p>Note: Given this class is external-facing, I considered creating 37*650b9f74SAndroid Build Coastguard Worker * an interface but it is not likely we'll ever need to add more flexibility 38*650b9f74SAndroid Build Coastguard Worker * and the class is so simple, I figured it was not warranted. 39*650b9f74SAndroid Build Coastguard Worker * 40*650b9f74SAndroid Build Coastguard Worker * 41*650b9f74SAndroid Build Coastguard Worker * @see HtmlParser 42*650b9f74SAndroid Build Coastguard Worker * @see JavascriptParser 43*650b9f74SAndroid Build Coastguard Worker */ 44*650b9f74SAndroid Build Coastguard Worker public class ExternalState { 45*650b9f74SAndroid Build Coastguard Worker 46*650b9f74SAndroid Build Coastguard Worker private final String name; 47*650b9f74SAndroid Build Coastguard Worker 48*650b9f74SAndroid Build Coastguard Worker /** 49*650b9f74SAndroid Build Coastguard Worker * Creates an {@code ExternalState} object. 50*650b9f74SAndroid Build Coastguard Worker * 51*650b9f74SAndroid Build Coastguard Worker * @param name the name to assign to that state 52*650b9f74SAndroid Build Coastguard Worker * @see HtmlParser 53*650b9f74SAndroid Build Coastguard Worker * @see JavascriptParser 54*650b9f74SAndroid Build Coastguard Worker */ ExternalState(String name)55*650b9f74SAndroid Build Coastguard Worker public ExternalState(String name) { 56*650b9f74SAndroid Build Coastguard Worker Preconditions.checkNotNull(name); // Developer error if it happens. 57*650b9f74SAndroid Build Coastguard Worker this.name = name; 58*650b9f74SAndroid Build Coastguard Worker } 59*650b9f74SAndroid Build Coastguard Worker 60*650b9f74SAndroid Build Coastguard Worker /** 61*650b9f74SAndroid Build Coastguard Worker * Returns the name of the object. The name is only needed 62*650b9f74SAndroid Build Coastguard Worker * to provide human-readable information when debugging. 63*650b9f74SAndroid Build Coastguard Worker * 64*650b9f74SAndroid Build Coastguard Worker * @return the name of that object 65*650b9f74SAndroid Build Coastguard Worker */ getName()66*650b9f74SAndroid Build Coastguard Worker public String getName() { 67*650b9f74SAndroid Build Coastguard Worker return name; 68*650b9f74SAndroid Build Coastguard Worker } 69*650b9f74SAndroid Build Coastguard Worker 70*650b9f74SAndroid Build Coastguard Worker /** 71*650b9f74SAndroid Build Coastguard Worker * Returns the string representation of this external state. 72*650b9f74SAndroid Build Coastguard Worker * The details of this representation are subject to change. 73*650b9f74SAndroid Build Coastguard Worker */ 74*650b9f74SAndroid Build Coastguard Worker @Override toString()75*650b9f74SAndroid Build Coastguard Worker public String toString() { 76*650b9f74SAndroid Build Coastguard Worker return String.format("ExternalState: %s", name); 77*650b9f74SAndroid Build Coastguard Worker } 78*650b9f74SAndroid Build Coastguard Worker } 79