Adapter.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.parser;

  16. import java.io.IOException;

  17. public class Adapter implements Appendable {
  18.     private final Element adaptable;
  19.     private char content;
  20.     private final CharSequence oneCharSequence = new CharSequence() {
  21.         @Override
  22.         public CharSequence subSequence(int start, int end) {
  23.             if (start == 0 && end == 0) {
  24.                 return this;
  25.             } else {
  26.                 throw new ArrayIndexOutOfBoundsException();
  27.             }
  28.         }

  29.         @Override
  30.         public int length() {
  31.             return 1;
  32.         }

  33.         @Override
  34.         public char charAt(int index) {
  35.             return content;
  36.         }
  37.     };

  38.     @Override
  39.     public Appendable append(CharSequence csq, int start, int end) throws IOException {
  40.         adaptable.characters(csq, start, end);
  41.         return this;
  42.     }

  43.     @Override
  44.     public Appendable append(char c) throws IOException {
  45.         content = c;
  46.         return append(oneCharSequence, 0, 1);
  47.     }

  48.     @Override
  49.     public Appendable append(CharSequence csq) throws IOException {
  50.         return append(csq, 0, csq.length());
  51.     }

  52.     public Adapter(Element adaptable) {
  53.         this.adaptable = adaptable;
  54.     }
  55. }