1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001, 2002,2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.xs.opti;
  22 
  23 import org.w3c.dom.Attr;
  24 import org.w3c.dom.Document;
  25 import org.w3c.dom.NamedNodeMap;
  26 import org.w3c.dom.Node;
  27 
  28 /**
  29  * @xerces.internal
  30  *
  31  * @author Rahul Srivastava, Sun Microsystems Inc.
  32  * @author Sandy Gao, IBM
  33  *
  34  * @version $Id: ElementImpl.java,v 1.7 2010-11-01 04:40:01 joehw Exp $
  35  */
  36 public class ElementImpl extends DefaultElement {
  37 
  38     SchemaDOM schemaDOM;
  39     Attr[] attrs;
  40     int row;
  41     int col;
  42     int parentRow;
  43 
  44     int line;
  45     int column;
  46     int charOffset;
  47     String fAnnotation;
  48     String fSyntheticAnnotation;
  49 
  50     public ElementImpl(int line, int column, int offset) {
  51         row = -1;
  52         col = -1;
  53         parentRow = -1;
  54         nodeType = Node.ELEMENT_NODE;
  55 
  56         this.line = line;
  57         this.column = column;
  58         charOffset = offset;
  59     }
  60 
  61     public ElementImpl(int line, int column) {
  62         this(line, column, -1);
  63     }
  64 
  65 
  66     public ElementImpl(String prefix, String localpart, String rawname,
  67             String uri, int line, int column, int offset) {
  68         super(prefix, localpart, rawname, uri, Node.ELEMENT_NODE);
  69         row = -1;
  70         col = -1;
  71         parentRow = -1;
  72 
  73         this.line = line;
  74         this.column = column;
  75         charOffset = offset;
  76     }
  77 
  78     public ElementImpl(String prefix, String localpart, String rawname,
  79             String uri, int line, int column) {
  80         this(prefix, localpart, rawname, uri, line, column, -1);
  81     }
  82 
  83 
  84     //
  85     // org.w3c.dom.Node methods
  86     //
  87 
  88     public Document getOwnerDocument() {
  89         return schemaDOM;
  90     }
  91 
  92 
  93     public Node getParentNode() {
  94         return schemaDOM.relations[row][0];
  95     }
  96 
  97 
  98     public boolean hasChildNodes() {
  99         if (parentRow == -1) {
 100             return false;
 101         }
 102         else {
 103             return true;
 104         }
 105     }
 106 
 107 
 108     public Node getFirstChild() {
 109         if (parentRow == -1) {
 110             return null;
 111         }
 112         return schemaDOM.relations[parentRow][1];
 113     }
 114 
 115 
 116     public Node getLastChild() {
 117         if (parentRow == -1) {
 118             return null;
 119         }
 120         int i=1;
 121         for (; i<schemaDOM.relations[parentRow].length; i++) {
 122             if (schemaDOM.relations[parentRow][i] == null) {
 123                 return schemaDOM.relations[parentRow][i-1];
 124             }
 125         }
 126         if (i ==1) {
 127             i++;
 128         }
 129         return schemaDOM.relations[parentRow][i-1];
 130     }
 131 
 132 
 133     public Node getPreviousSibling() {
 134         if (col == 1) {
 135             return null;
 136         }
 137         return schemaDOM.relations[row][col-1];
 138     }
 139 
 140 
 141     public Node getNextSibling() {
 142         if (col == schemaDOM.relations[row].length-1) {
 143             return null;
 144         }
 145         return schemaDOM.relations[row][col+1];
 146     }
 147 
 148 
 149     public NamedNodeMap getAttributes() {
 150         return new NamedNodeMapImpl(attrs);
 151     }
 152 
 153 
 154     public boolean hasAttributes() {
 155         return (attrs.length == 0 ? false : true);
 156     }
 157 
 158 
 159 
 160     //
 161     // org.w3c.dom.Element methods
 162     //
 163 
 164     public String getTagName() {
 165         return rawname;
 166     }
 167 
 168 
 169     public String getAttribute(String name) {
 170 
 171         for (int i=0; i<attrs.length; i++) {
 172             if (attrs[i].getName().equals(name)) {
 173                 return attrs[i].getValue();
 174             }
 175         }
 176         return "";
 177     }
 178 
 179 
 180     public Attr getAttributeNode(String name) {
 181         for (int i=0; i<attrs.length; i++) {
 182             if (attrs[i].getName().equals(name)) {
 183                 return attrs[i];
 184             }
 185         }
 186         return null;
 187     }
 188 
 189 
 190     public String getAttributeNS(String namespaceURI, String localName) {
 191         for (int i=0; i<attrs.length; i++) {
 192             if (attrs[i].getLocalName().equals(localName) && nsEquals(attrs[i].getNamespaceURI(), namespaceURI)) {
 193                 return attrs[i].getValue();
 194             }
 195         }
 196         return "";
 197     }
 198 
 199 
 200     public Attr getAttributeNodeNS(String namespaceURI, String localName) {
 201         for (int i=0; i<attrs.length; i++) {
 202             if (attrs[i].getName().equals(localName) && nsEquals(attrs[i].getNamespaceURI(), namespaceURI)) {
 203                 return attrs[i];
 204             }
 205         }
 206         return null;
 207     }
 208 
 209 
 210     public boolean hasAttribute(String name) {
 211         for (int i=0; i<attrs.length; i++) {
 212             if (attrs[i].getName().equals(name)) {
 213                 return true;
 214             }
 215         }
 216         return false;
 217     }
 218 
 219 
 220     public boolean hasAttributeNS(String namespaceURI, String localName) {
 221         for (int i=0; i<attrs.length; i++) {
 222             if (attrs[i].getName().equals(localName) && nsEquals(attrs[i].getNamespaceURI(), namespaceURI)) {
 223                 return true;
 224             }
 225         }
 226         return false;
 227     }
 228 
 229 
 230     public void setAttribute(String name, String value) {
 231         for (int i=0; i<attrs.length; i++) {
 232             if (attrs[i].getName().equals(name)) {
 233                 attrs[i].setValue(value);
 234                 return;
 235             }
 236         }
 237     }
 238 
 239     /** Returns the line number. */
 240     public int getLineNumber() {
 241         return line;
 242     }
 243 
 244     /** Returns the column number. */
 245     public int getColumnNumber() {
 246         return column;
 247     }
 248 
 249     /** Returns the character offset. */
 250     public int getCharacterOffset() {
 251         return charOffset;
 252     }
 253 
 254     public String getAnnotation() {
 255         return fAnnotation;
 256     }
 257 
 258     public String getSyntheticAnnotation() {
 259         return fSyntheticAnnotation;
 260     }
 261 
 262     /**
 263      * Compares two namespace URIs with an extra case for null entries
 264      */
 265     private static boolean nsEquals(String nsURI_1, String nsURI_2) {
 266         if (nsURI_1 == null) {
 267             return (nsURI_2 == null);
 268         }
 269         else {
 270             return nsURI_1.equals(nsURI_2);
 271         }
 272     }
 273 
 274 }