BaseElement.java

  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  *
  14.  */
  15. package org.esigate.esi;

  16. import java.io.IOException;

  17. import org.esigate.HttpErrorPage;
  18. import org.esigate.parser.Element;
  19. import org.esigate.parser.ParserContext;

  20. abstract class BaseElement implements Element {
  21.     private Element parent = null;

  22.     protected BaseElement() {
  23.     }

  24.     /** Additional tag initialization callback. */
  25.     protected boolean parseTag(Tag tag, ParserContext ctx) throws HttpErrorPage {
  26.         // Default implementation does nothing
  27.         return true;
  28.     }

  29.     @Override
  30.     public boolean onTagStart(String tag, ParserContext ctx) throws IOException, HttpErrorPage {
  31.         Tag tagObj = Tag.create(tag);
  32.         parent = ctx.getCurrent();
  33.         return parseTag(tagObj, ctx);
  34.     }

  35.     @Override
  36.     public boolean onError(Exception e, ParserContext ctx) {
  37.         return false;
  38.     }

  39.     /*
  40.      * (non-Javadoc)
  41.      *
  42.      * @see org.esigate.parser.Element#characters(java.lang.CharSequence, int, int)
  43.      */
  44.     @Override
  45.     public void characters(CharSequence csq, int start, int end) throws IOException {
  46.         parent.characters(csq, start, end);
  47.     }

  48. }