001    /*
002     * Copyright (c) 2007 Mozilla Foundation
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package nu.validator.xml;
024    
025    import org.xml.sax.Attributes;
026    
027    public final class IdnessChangingAttributesWrapper implements Attributes {
028    
029        private Attributes delegate;
030    
031        private int xmlIdIndex;
032        
033        private int idIndex;
034        
035        private String xmlIdValue;
036    
037        /**
038         * @param delegate
039         * @param xmlIdIndex
040         * @param idIndex
041         */
042        public IdnessChangingAttributesWrapper(Attributes delegate, int xmlIdIndex, int idIndex, String xmlIdValue) {
043            this.delegate = delegate;
044            this.xmlIdIndex = xmlIdIndex;
045            this.idIndex = idIndex;
046            this.xmlIdValue = xmlIdValue;
047        }
048        
049        public IdnessChangingAttributesWrapper() {
050            this.delegate = null;
051            this.xmlIdIndex = -1;
052            this.idIndex = -1;
053            this.xmlIdValue = null;
054        }
055        
056        public void setFields(Attributes delegate, int xmlIdIndex, int idIndex, String xmlIdValue) {
057            this.delegate = delegate;
058            this.xmlIdIndex = xmlIdIndex;
059            this.idIndex = idIndex;
060            this.xmlIdValue = xmlIdValue;
061        }
062        
063        /**
064         * @param arg0
065         * @param arg1
066         * @return
067         * @see org.xml.sax.Attributes#getIndex(java.lang.String, java.lang.String)
068         */
069        public int getIndex(String arg0, String arg1) {
070            return delegate.getIndex(arg0, arg1);
071        }
072    
073        /**
074         * @param arg0
075         * @return
076         * @see org.xml.sax.Attributes#getIndex(java.lang.String)
077         */
078        public int getIndex(String arg0) {
079            return delegate.getIndex(arg0);
080        }
081    
082        /**
083         * @return
084         * @see org.xml.sax.Attributes#getLength()
085         */
086        public int getLength() {
087            return delegate.getLength();
088        }
089    
090        /**
091         * @param arg0
092         * @return
093         * @see org.xml.sax.Attributes#getLocalName(int)
094         */
095        public String getLocalName(int arg0) {
096            return delegate.getLocalName(arg0);
097        }
098    
099        /**
100         * @param arg0
101         * @return
102         * @see org.xml.sax.Attributes#getQName(int)
103         */
104        public String getQName(int arg0) {
105            return delegate.getQName(arg0);
106        }
107    
108        /**
109         * @param index
110         * @return
111         * @see org.xml.sax.Attributes#getType(int)
112         */
113        public String getType(int index) {
114            if (idIndex == index || xmlIdIndex == index) {
115                return "ID";
116            } else {
117                return delegate.getType(index);
118            }
119        }
120    
121        /**
122         * @param uri
123         * @param localName
124         * @return
125         * @see org.xml.sax.Attributes#getType(java.lang.String, java.lang.String)
126         */
127        public String getType(String uri, String localName) {
128            int index = getIndex(uri, localName);
129            if (index >= 0) {
130                return getType(index);
131            } else {
132                return null;
133            }
134        }
135    
136        /**
137         * @param qName
138         * @return
139         * @see org.xml.sax.Attributes#getType(java.lang.String)
140         */
141        public String getType(String qName) {
142            int index = getIndex(qName);
143            if (index >= 0) {
144                return getType(index);
145            } else {
146                return null;
147            }
148        }
149    
150        /**
151         * @param arg0
152         * @return
153         * @see org.xml.sax.Attributes#getURI(int)
154         */
155        public String getURI(int arg0) {
156            return delegate.getURI(arg0);
157        }
158    
159        /**
160         * @param index
161         * @return
162         * @see org.xml.sax.Attributes#getValue(int)
163         */
164        public String getValue(int index) {
165            if (xmlIdValue == null) {
166                return delegate.getValue(index);            
167            } else {
168                if (xmlIdIndex == index) {
169                    return xmlIdValue;
170                } else {
171                    return delegate.getValue(index);
172                }
173            }
174        }
175    
176        /**
177         * @param uri
178         * @param localName
179         * @return
180         * @see org.xml.sax.Attributes#getValue(java.lang.String, java.lang.String)
181         */
182        public String getValue(String uri, String localName) {
183            if (xmlIdValue == null) {
184                return delegate.getValue(uri, localName);            
185            } else {
186                int index = getIndex(uri, localName);
187                if (index < 0) {
188                    return null;
189                } else if (xmlIdIndex == index) {
190                    return xmlIdValue;
191                } else {
192                    return delegate.getValue(index);
193                }
194            }
195        }
196    
197        /**
198         * @param qName
199         * @return
200         * @see org.xml.sax.Attributes#getValue(java.lang.String)
201         */
202        public String getValue(String qName) {
203            if (xmlIdValue == null) {
204                return delegate.getValue(qName);            
205            } else {
206                int index = getIndex(qName);
207                if (index < 0) {
208                    return null;
209                } else if (xmlIdIndex == index) {
210                    return xmlIdValue;
211                } else {
212                    return delegate.getValue(index);
213                }
214            }
215        }
216        
217    }