View Javadoc
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  
16  package org.esigate.parser;
17  
18  import java.io.IOException;
19  
20  public class Adapter implements Appendable {
21      private final Element adaptable;
22      private char content;
23      private final CharSequence oneCharSequence = new CharSequence() {
24          @Override
25          public CharSequence subSequence(int start, int end) {
26              if (start == 0 && end == 0) {
27                  return this;
28              } else {
29                  throw new ArrayIndexOutOfBoundsException();
30              }
31          }
32  
33          @Override
34          public int length() {
35              return 1;
36          }
37  
38          @Override
39          public char charAt(int index) {
40              return content;
41          }
42      };
43  
44      @Override
45      public Appendable append(CharSequence csq, int start, int end) throws IOException {
46          adaptable.characters(csq, start, end);
47          return this;
48      }
49  
50      @Override
51      public Appendable append(char c) throws IOException {
52          content = c;
53          return append(oneCharSequence, 0, 1);
54      }
55  
56      @Override
57      public Appendable append(CharSequence csq) throws IOException {
58          return append(csq, 0, csq.length());
59      }
60  
61      public Adapter(Element adaptable) {
62          this.adaptable = adaptable;
63      }
64  }