1 57 58 package org.enhydra.apache.xerces.utils; 59 60 65 public class NamespacesScope { 66 69 public interface NamespacesHandler { 70 77 public void startNamespaceDeclScope(int prefix, int uri) throws Exception ; 78 84 public void endNamespaceDeclScope(int prefix) throws Exception ; 85 } 86 public NamespacesScope() { 87 this(new NamespacesHandler() { 88 public void startNamespaceDeclScope(int prefix, int uri) throws Exception { 89 } 90 public void endNamespaceDeclScope(int prefix) throws Exception { 91 } 92 }); 93 } 94 public NamespacesScope(NamespacesHandler handler) { 95 fHandler = handler; 96 fNamespaceMappings[0] = new int[9]; 97 fNamespaceMappings[0][0] = 1; 98 } 99 public NamespacesScope(NamespacesHandler handler, int elemDepth, int[][] map) { 100 fHandler = handler; 101 fElementDepth = elemDepth; 102 if (map == null) { 103 fNamespaceMappings = new int[8][]; 104 return; 105 } 106 fNamespaceMappings = new int[map.length][]; 107 for(int i = 0; i <= fElementDepth; i++) { 108 if(map[i]!= null) { 109 fNamespaceMappings[i] = new int[map[i].length]; 110 System.arraycopy(map[i], 0, fNamespaceMappings[i], 0, map[i].length); 111 } 112 } 113 } 114 120 public void setNamespaceForPrefix(int prefix, int namespace) throws Exception { 121 int offset = fNamespaceMappings[fElementDepth][0]; 122 if (offset == fNamespaceMappings[fElementDepth].length) { 123 int[] newMappings = new int[offset + 8]; 124 System.arraycopy(fNamespaceMappings[fElementDepth], 0, newMappings, 0, offset); 125 fNamespaceMappings[fElementDepth] = newMappings; 126 } 127 fNamespaceMappings[fElementDepth][offset++] = prefix; 128 fNamespaceMappings[fElementDepth][offset++] = namespace; 129 fNamespaceMappings[fElementDepth][0] = offset; 130 if (fElementDepth > 0) 131 fHandler.startNamespaceDeclScope(prefix, namespace); 132 } 133 138 public int getNamespaceForPrefix(int prefix) { 139 for (int depth = fElementDepth; depth >= 0; depth--) { 140 int offset = fNamespaceMappings[depth][0]; 141 for (int i = 1; i < offset; i += 2) { 142 if (prefix == fNamespaceMappings[depth][i]) { 143 return fNamespaceMappings[depth][i+1]; 144 } 145 } 146 } 147 return StringPool.EMPTY_STRING; 148 } 149 152 public void increaseDepth() throws Exception { 153 fElementDepth++; 154 if (fElementDepth == fNamespaceMappings.length) { 155 int[][] newMappings = new int[fElementDepth + 8][]; 156 System.arraycopy(fNamespaceMappings, 0, newMappings, 0, fElementDepth); 157 fNamespaceMappings = newMappings; 158 } 159 if (fNamespaceMappings[fElementDepth] == null) 160 fNamespaceMappings[fElementDepth] = new int[9]; 161 fNamespaceMappings[fElementDepth][0] = 1; 162 } 163 166 public void decreaseDepth() throws Exception { 167 if (fElementDepth > 0) { 168 int offset = fNamespaceMappings[fElementDepth][0]; 169 while (offset > 1) { 170 offset -= 2; 171 fHandler.endNamespaceDeclScope(fNamespaceMappings[fElementDepth][offset]); 172 } 173 } 174 fElementDepth--; 175 } 176 177 public Object clone() { 179 return new NamespacesScope(fHandler, fElementDepth, fNamespaceMappings); 180 } private NamespacesHandler fHandler = null; 182 private int fElementDepth = 0; 183 private int[][] fNamespaceMappings = new int[8][]; 184 } 185 | Popular Tags |