1 package net.sf.saxon.style; 2 import net.sf.saxon.Err; 3 import net.sf.saxon.expr.Expression; 4 import net.sf.saxon.instruct.Executable; 5 import net.sf.saxon.om.*; 6 import net.sf.saxon.trans.XPathException; 7 8 import java.util.*; 9 10 13 14 public class XSLCharacterMap extends StyleElement { 15 16 19 String use; 20 22 List characterMapElements = null; 23 25 boolean validated = false; 26 28 boolean redundant = false; 29 31 35 36 public int getCharacterMapFingerprint() { 37 return getObjectNameCode() & 0xfffff; 38 } 39 40 47 48 public boolean isRedundant() { 49 return redundant; 50 } 51 52 56 57 public void prepareAttributes() throws XPathException { 58 59 String name = null; 60 use = null; 61 62 AttributeCollection atts = getAttributeList(); 63 64 for (int a=0; a<atts.getLength(); a++) { 65 int nc = atts.getNameCode(a); 66 String f = getNamePool().getClarkName(nc); 67 if (f==StandardNames.NAME) { 68 name = atts.getValue(a).trim(); 69 } else if (f==StandardNames.USE_CHARACTER_MAPS) { 70 use = atts.getValue(a); 71 } else { 72 checkUnknownAttribute(nc); 73 } 74 } 75 76 if (name==null) { 77 reportAbsence("name"); 78 return; 79 } 80 81 try { 82 setObjectNameCode(makeNameCode(name.trim())); 83 } catch (NamespaceException err) { 84 compileError(err.getMessage(), "XTSE0280"); 85 } catch (XPathException err) { 86 compileError(err.getMessage()); 87 } 88 89 } 90 91 public void validate() throws XPathException { 92 93 if (validated) return; 94 95 97 checkTopLevel(null); 98 99 101 AxisIterator kids = iterateAxis(Axis.CHILD); 102 while (true) { 103 Item child = kids.next(); 104 if (child == null) { 105 break; 106 } 107 if (!(child instanceof XSLOutputCharacter)) { 108 compileError("Only xsl:output-character is allowed within xsl:character-map", "XTSE0010"); 109 } 110 } 111 112 115 XSLStylesheet principal = getPrincipalStylesheet(); 116 XSLCharacterMap other = principal.getCharacterMap(getObjectFingerprint()); 117 if (other != this) { 118 if (this.getPrecedence() == other.getPrecedence()) { 119 compileError("There are two character-maps with the same name and import precedence", "XTSE1580"); 120 } else if (this.getPrecedence() < other.getPrecedence()) { 121 redundant = true; 122 } 123 } 124 125 127 if (use!=null) { 128 129 131 characterMapElements = new ArrayList(5); 132 StringTokenizer st = new StringTokenizer(use); 133 134 while (st.hasMoreTokens()) { 135 String displayname = st.nextToken(); 136 try { 137 String [] parts = Name.getQNameParts(displayname); 138 String uri = getURIForPrefix(parts[0], false); 139 if (uri == null) { 140 compileError("Undeclared namespace prefix " + Err.wrap(parts[0]) 141 + " in character map name", "XTSE0280"); 142 } 143 int nameCode = getTargetNamePool().allocate(parts[0], uri, parts[1]); 144 XSLCharacterMap ref = 145 principal.getCharacterMap(nameCode & 0xfffff); 146 if (ref == null) { 147 compileError("No character-map named '" + displayname + "' has been defined", "XTSE1590"); 148 } else { 149 characterMapElements.add(ref); 150 } 151 } catch (QNameException err) { 152 compileError("Invalid character-map name. " + err.getMessage(), "XTSE1590"); 153 } 154 } 155 156 158 for (Iterator it=characterMapElements.iterator(); it.hasNext();) { 159 ((XSLCharacterMap)it.next()).checkCircularity(this); 160 } 161 } 162 163 validated = true; 164 } 165 166 170 171 private void checkCircularity(XSLCharacterMap origin) throws XPathException { 172 if (this==origin) { 173 compileError("The definition of the character map is circular", "XTSE1600"); 174 characterMapElements = null; } else { 176 if (!validated) { 177 return; 181 } 182 if (characterMapElements != null) { 183 for (Iterator it=characterMapElements.iterator(); it.hasNext();) { 184 ((XSLCharacterMap)it.next()).checkCircularity(origin); 185 } 186 } 187 } 188 } 189 190 194 195 public void assemble(HashMap map) { 196 if (characterMapElements != null) { 197 for (int i = 0; i < characterMapElements.size(); i++) { 198 XSLCharacterMap charmap = (XSLCharacterMap) characterMapElements.get(i); 199 charmap.assemble(map); 200 } 201 } 202 AxisIterator kids = iterateAxis(Axis.CHILD); 203 while (true) { 204 Item child = kids.next(); 205 if (child == null) { 206 return; 207 } 208 XSLOutputCharacter oc = (XSLOutputCharacter)child; 209 map.put(new Integer (oc.getCodePoint()), oc.getReplacementString()); 210 } 211 } 212 213 public Expression compile(Executable exec) throws XPathException { 214 return null; 215 } 216 217 } 218 219 | Popular Tags |