1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.apache.harmony.xml;
18 
19 import org.xml.sax.Attributes;
20 
21 import android.compat.annotation.UnsupportedAppUsage;
22 
23 /**
24  * Wraps native attribute array.
25  */
26 abstract class ExpatAttributes implements Attributes {
27 
28     /**
29      * Since we don't do validation, pretty much everything is CDATA type.
30      */
31     private static final String CDATA = "CDATA";
32 
33     @UnsupportedAppUsage
ExpatAttributes()34     public ExpatAttributes() {
35     }
36 
37     /**
38      * Gets the number of attributes.
39      */
getLength()40     public abstract int getLength();
41 
42     /**
43      * Gets the pointer to the parser. We need this so we can get to the
44      * interned string pool.
45      */
getParserPointer()46     abstract long getParserPointer();
47 
48     /**
49      * Gets the pointer to the underlying attribute array. Can be 0 if the
50      * length is 0.
51      */
getPointer()52     public abstract long getPointer();
53 
getURI(int index)54     public String getURI(int index) {
55         if (index < 0 || index >= getLength()) {
56             return null;
57         }
58         return getURI(getParserPointer(), getPointer(), index);
59     }
60 
getLocalName(int index)61     public String getLocalName(int index) {
62         return (index < 0 || index >= getLength())
63                 ? null
64                 : getLocalName(getParserPointer(), getPointer(), index);
65     }
66 
getQName(int index)67     public String getQName(int index) {
68         return (index < 0 || index >= getLength())
69                 ? null
70                 : getQName(getParserPointer(), getPointer(), index);
71     }
72 
getType(int index)73     public String getType(int index) {
74         return (index < 0 || index >= getLength()) ? null : CDATA;
75     }
76 
getValue(int index)77     public String getValue(int index) {
78         return (index < 0 || index >= getLength())
79                 ? null
80                 : getValueByIndex(getPointer(), index);
81     }
82 
getIndex(String uri, String localName)83     public int getIndex(String uri, String localName) {
84         if (uri == null) {
85             throw new NullPointerException("uri == null");
86         }
87         if (localName == null) {
88             throw new NullPointerException("localName == null");
89         }
90         long pointer = getPointer();
91         if (pointer == 0) {
92             return -1;
93         }
94         return getIndex(pointer, uri, localName);
95     }
96 
getIndex(String qName)97     public int getIndex(String qName) {
98         if (qName == null) {
99             throw new NullPointerException("qName == null");
100         }
101         long pointer = getPointer();
102         if (pointer == 0) {
103             return -1;
104         }
105         return getIndexForQName(pointer, qName);
106     }
107 
getType(String uri, String localName)108     public String getType(String uri, String localName) {
109         if (uri == null) {
110             throw new NullPointerException("uri == null");
111         }
112         if (localName == null) {
113             throw new NullPointerException("localName == null");
114         }
115         return getIndex(uri, localName) == -1 ? null : CDATA;
116     }
117 
getType(String qName)118     public String getType(String qName) {
119         return getIndex(qName) == -1 ? null : CDATA;
120     }
121 
getValue(String uri, String localName)122     public String getValue(String uri, String localName) {
123         if (uri == null) {
124             throw new NullPointerException("uri == null");
125         }
126         if (localName == null) {
127             throw new NullPointerException("localName == null");
128         }
129         long pointer = getPointer();
130         if (pointer == 0) {
131             return null;
132         }
133         return getValue(pointer, uri, localName);
134     }
135 
getValue(String qName)136     public String getValue(String qName) {
137         if (qName == null) {
138             throw new NullPointerException("qName == null");
139         }
140         long pointer = getPointer();
141         if (pointer == 0) {
142             return null;
143         }
144         return getValueForQName(pointer, qName);
145     }
146 
getURI(long pointer, long attributePointer, int index)147     private static native String getURI(long pointer, long attributePointer, int index);
getLocalName(long pointer, long attributePointer, int index)148     private static native String getLocalName(long pointer, long attributePointer, int index);
getQName(long pointer, long attributePointer, int index)149     private static native String getQName(long pointer, long attributePointer, int index);
getValueByIndex(long attributePointer, int index)150     private static native String getValueByIndex(long attributePointer, int index);
getIndex(long attributePointer, String uri, String localName)151     private static native int getIndex(long attributePointer, String uri, String localName);
getIndexForQName(long attributePointer, String qName)152     private static native int getIndexForQName(long attributePointer, String qName);
getValue(long attributePointer, String uri, String localName)153     private static native String getValue(long attributePointer, String uri, String localName);
getValueForQName(long attributePointer, String qName)154     private static native String getValueForQName(long attributePointer, String qName);
freeAttributes(long pointer)155     protected native void freeAttributes(long pointer);
156 }
157