1 16 package org.apache.cocoon.components.serializers.util; 17 18 import org.xml.sax.SAXException ; 19 20 27 public class Namespaces { 28 29 private String uri[] = new String [512]; 30 31 private String pre[] = new String [512]; 32 33 private int depth = 0; 34 35 private int last = 0; 36 37 38 public static final int NAMESPACE_PREFIX = 0; 39 40 public static final int NAMESPACE_URI = 1; 41 42 45 public Namespaces() { 46 super(); 47 48 this.push("", ""); 49 this.push("xml", "http://www.w3.org/XML/1998/namespace"); 50 this.last = this.depth; 51 } 52 53 59 public synchronized void push(String prefix, String uri) { 60 if (this.depth == this.uri.length) { 61 int newDepth = this.uri.length + (this.uri.length >> 1); 62 String newUri[] = new String [newDepth]; 63 String newPre[] = new String [newDepth]; 64 System.arraycopy(this.uri, 0, newUri, 0, this.depth); 65 System.arraycopy(this.pre, 0, newPre, 0, this.depth); 66 this.uri = newUri; 67 this.pre = newPre; 68 } 69 70 this.uri[this.depth] = uri; 71 this.pre[this.depth] = prefix; 72 this.depth ++; 73 } 74 75 84 public synchronized void pop(String prefix) 85 throws SAXException { 86 for (int x = this.position(prefix, pre); x < this.depth; x++) { 87 int k = (x + 1); 88 this.pre[x] = this.pre[k]; 89 this.uri[x] = this.uri[k]; 90 } 91 this.last--; 94 this.depth--; 95 } 96 97 114 public String qualify(String nsuri, String local, String qualified) 115 throws SAXException { 116 if (nsuri == null) nsuri = ""; 117 if (local == null) local = ""; 118 if (qualified == null) qualified = ""; 119 120 121 if ((nsuri.length() == 0 ) && (local.length() == 0)) return(qualified); 122 123 127 int position = position(nsuri, this.uri); 128 if (this.pre[position].length() > 0) { 129 return(this.pre[position] + ':' + local); 130 } 131 return(local); 132 } 133 134 138 public String [][] commit() { 139 int size = this.depth - this.last; 140 String result[][] = new String [size][2]; 141 int k = 0; 142 for (int x = this.last; x < this.depth; x++) { 143 result[k][NAMESPACE_PREFIX] = this.pre[x]; 144 result[k][NAMESPACE_URI] = this.uri[x]; 145 k++; 146 } 147 this.last = this.depth; 148 return(result); 149 } 150 151 156 public String getUri(String prefix) 157 throws SAXException { 158 return(this.uri[this.position(prefix, this.pre)]); 159 } 160 161 166 public String getPrefix(String nsuri) 167 throws SAXException { 168 return(this.pre[this.position(nsuri, this.uri)]); 169 } 170 171 175 private int position(String check, String array[]) 176 throws SAXException { 177 int x = this.depth; 178 while (true) { 179 if (check.equals(array[--x])) return(x); 180 if (x == 0) break; 181 } 182 throw new SAXException ("Unable to map \"" + check + "\""); 183 } 184 185 } 186 | Popular Tags |