156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/*
256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Copyright (C) 2010 Google Inc.
356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Licensed under the Apache License, Version 2.0 (the "License");
556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * you may not use this file except in compliance with the License.
656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * You may obtain a copy of the License at
756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * http://www.apache.org/licenses/LICENSE-2.0
956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
1056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Unless required by applicable law or agreed to in writing, software
1156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * distributed under the License is distributed on an "AS IS" BASIS,
1256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * See the License for the specific language governing permissions and
1456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * limitations under the License.
1556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */
1656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
1756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpackage com.google.streamhtmlparser;
1856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
1956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport com.google.common.base.Preconditions;
2056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
2156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/**
2256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * A representation of the parser state suitable for use by the caller
2356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * of the Parser. The meaning of each state and therefore which action
2456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * the caller should perform on that state is not self-evident. In particular,
2556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * it depends on which parser is used (currently {@link HtmlParser} and
2656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * {@link JavascriptParser}). For examples, you will have to look
2756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * at the <code>Google Template System</code> and <code>ClearSilver</code>
2856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * both of which support Auto-Escaping by interfacing with our parser
2956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * (using the parser written in C++).
3056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
3156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * <p>The caller of the Parser will query for the current parser state at
3256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * points of interest during parsing of templates. Based on the parser's
3356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * current state as represented by this class, the caller can determine
3456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * the appropriate escaping to apply.
3556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
3656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * <p>Note: Given this class is external-facing, I considered creating
3756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * an interface but it is not likely we'll ever need to add more flexibility
3856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * and the class is so simple, I figured it was not warranted.
3956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
4056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
4156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * @see HtmlParser
4256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * @see JavascriptParser
4356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */
4456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpublic class ExternalState {
4556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
4656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  private final String name;
4756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
4856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
4956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Creates an {@code ExternalState} object.
5056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
5156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param name the name to assign to that state
5256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @see HtmlParser
5356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @see JavascriptParser
5456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
5556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public ExternalState(String name) {
5656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    Preconditions.checkNotNull(name);   // Developer error if it happens.
5756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    this.name = name;
5856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
5956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
6056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
6156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Returns the name of the object. The name is only needed
6256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * to provide human-readable information when debugging.
6356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
6456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @return the name of that object
6556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
6656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public String getName() {
6756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    return name;
6856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
6956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
7056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
7156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Returns the string representation of this external state.
7256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * The details of this representation are subject to change.
7356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
7456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  @Override
7556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public String toString() {
7656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    return String.format("ExternalState: %s", name);
7756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
7856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson}
79