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
17package org.apache.harmony.xml;
18
19import org.xml.sax.Attributes;
20
21/**
22 * Wraps native attribute array.
23 */
24abstract class ExpatAttributes implements Attributes {
25
26    /**
27     * Since we don't do validation, pretty much everything is CDATA type.
28     */
29    private static final String CDATA = "CDATA";
30
31    /**
32     * Gets the number of attributes.
33     */
34    public abstract int getLength();
35
36    /**
37     * Gets the pointer to the parser. We need this so we can get to the
38     * interned string pool.
39     */
40    abstract long getParserPointer();
41
42    /**
43     * Gets the pointer to the underlying attribute array. Can be 0 if the
44     * length is 0.
45     */
46    public abstract long getPointer();
47
48    public String getURI(int index) {
49        if (index < 0 || index >= getLength()) {
50            return null;
51        }
52        return getURI(getParserPointer(), getPointer(), index);
53    }
54
55    public String getLocalName(int index) {
56        return (index < 0 || index >= getLength())
57                ? null
58                : getLocalName(getParserPointer(), getPointer(), index);
59    }
60
61    public String getQName(int index) {
62        return (index < 0 || index >= getLength())
63                ? null
64                : getQName(getParserPointer(), getPointer(), index);
65    }
66
67    public String getType(int index) {
68        return (index < 0 || index >= getLength()) ? null : CDATA;
69    }
70
71    public String getValue(int index) {
72        return (index < 0 || index >= getLength())
73                ? null
74                : getValueByIndex(getPointer(), index);
75    }
76
77    public int getIndex(String uri, String localName) {
78        if (uri == null) {
79            throw new NullPointerException("uri == null");
80        }
81        if (localName == null) {
82            throw new NullPointerException("localName == null");
83        }
84        long pointer = getPointer();
85        if (pointer == 0) {
86            return -1;
87        }
88        return getIndex(pointer, uri, localName);
89    }
90
91    public int getIndex(String qName) {
92        if (qName == null) {
93            throw new NullPointerException("qName == null");
94        }
95        long pointer = getPointer();
96        if (pointer == 0) {
97            return -1;
98        }
99        return getIndexForQName(pointer, qName);
100    }
101
102    public String getType(String uri, String localName) {
103        if (uri == null) {
104            throw new NullPointerException("uri == null");
105        }
106        if (localName == null) {
107            throw new NullPointerException("localName == null");
108        }
109        return getIndex(uri, localName) == -1 ? null : CDATA;
110    }
111
112    public String getType(String qName) {
113        return getIndex(qName) == -1 ? null : CDATA;
114    }
115
116    public String getValue(String uri, String localName) {
117        if (uri == null) {
118            throw new NullPointerException("uri == null");
119        }
120        if (localName == null) {
121            throw new NullPointerException("localName == null");
122        }
123        long pointer = getPointer();
124        if (pointer == 0) {
125            return null;
126        }
127        return getValue(pointer, uri, localName);
128    }
129
130    public String getValue(String qName) {
131        if (qName == null) {
132            throw new NullPointerException("qName == null");
133        }
134        long pointer = getPointer();
135        if (pointer == 0) {
136            return null;
137        }
138        return getValueForQName(pointer, qName);
139    }
140
141    private static native String getURI(long pointer, long attributePointer, int index);
142    private static native String getLocalName(long pointer, long attributePointer, int index);
143    private static native String getQName(long pointer, long attributePointer, int index);
144    private static native String getValueByIndex(long attributePointer, int index);
145    private static native int getIndex(long attributePointer, String uri, String localName);
146    private static native int getIndexForQName(long attributePointer, String qName);
147    private static native String getValue(long attributePointer, String uri, String localName);
148    private static native String getValueForQName(long attributePointer, String qName);
149    protected native void freeAttributes(long pointer);
150}
151