1 package net.sf.saxon.style; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.Err; 4 import net.sf.saxon.expr.Expression; 5 import net.sf.saxon.expr.ExpressionTool; 6 import net.sf.saxon.instruct.*; 7 import net.sf.saxon.om.*; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.SchemaType; 10 import net.sf.saxon.value.EmptySequence; 11 import net.sf.saxon.value.StringValue; 12 13 14 17 18 public class XSLElement extends StyleElement { 19 20 private Expression elementName; 21 private Expression namespace = null; 22 private String use; 23 private AttributeSet[] attributeSets = null; 24 private int validation; 25 private SchemaType schemaType = null; 26 private boolean inheritNamespaces = true; 27 28 32 33 public boolean isInstruction() { 34 return true; 35 } 36 37 41 42 public boolean mayContainSequenceConstructor() { 43 return true; 44 } 45 46 public void prepareAttributes() throws XPathException { 47 48 AttributeCollection atts = getAttributeList(); 49 50 String nameAtt = null; 51 String namespaceAtt = null; 52 String validationAtt = null; 53 String typeAtt = null; 54 String inheritAtt = null; 55 56 for (int a=0; a<atts.getLength(); a++) { 57 int nc = atts.getNameCode(a); 58 String f = getNamePool().getClarkName(nc); 59 if (f==StandardNames.NAME) { 60 nameAtt = atts.getValue(a).trim(); 61 } else if (f==StandardNames.NAMESPACE) { 62 namespaceAtt = atts.getValue(a); 63 } else if (f==StandardNames.VALIDATION) { 64 validationAtt = atts.getValue(a).trim(); 65 } else if (f==StandardNames.TYPE) { 66 typeAtt = atts.getValue(a).trim(); 67 } else if (f==StandardNames.INHERIT_NAMESPACES) { 68 inheritAtt = atts.getValue(a).trim(); 69 } else if (f==StandardNames.USE_ATTRIBUTE_SETS) { 70 use = atts.getValue(a); 71 } else { 72 checkUnknownAttribute(nc); 73 } 74 } 75 76 if (nameAtt==null) { 77 reportAbsence("name"); 78 } else { 79 elementName = makeAttributeValueTemplate(nameAtt); 80 if (elementName instanceof StringValue) { 81 if (!Name.isQName(((StringValue)elementName).getStringValue())) { 82 compileError("Element name " + 83 Err.wrap(((StringValue)elementName).getStringValue(), Err.ELEMENT) + 84 " is not a valid QName", "XTDE0820"); 85 elementName = new StringValue("saxon-error-element"); 87 } 88 } 89 } 90 91 if (namespaceAtt!=null) { 92 namespace = makeAttributeValueTemplate(namespaceAtt); 93 } 94 95 if (validationAtt!=null) { 96 validation = Validation.getCode(validationAtt); 97 if (validation != Validation.STRIP && !getConfiguration().isSchemaAware(Configuration.XSLT)) { 98 compileError("To perform validation, a schema-aware XSLT processor is needed", "XTSE1660"); 99 } 100 if (validation == Validation.INVALID) { 101 compileError("Invalid value for @validation attribute. " + 102 "Permitted values are (strict, lax, preserve, strip)", "XTSE0020"); 103 } 104 } else { 105 validation = getContainingStylesheet().getDefaultValidation(); 106 } 107 108 if (typeAtt!=null) { 109 if (!getConfiguration().isSchemaAware(Configuration.XSLT)) { 110 compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660"); 111 } 112 schemaType = getSchemaType(typeAtt); 113 } 114 115 if (typeAtt != null && validationAtt != null) { 116 compileError("The @validation and @type attributes are mutually exclusive", "XTSE1505"); 117 } 118 119 if (inheritAtt != null) { 120 if (inheritAtt.equals("yes")) { 121 inheritNamespaces = true; 122 } else if (inheritAtt.equals("no")) { 123 inheritNamespaces = false; 124 } else { 125 compileError("The @inherit-namespaces attribute has permitted values (yes, no)", "XTSE0020"); 126 } 127 } 128 } 129 130 public void validate() throws XPathException { 131 checkWithinTemplate(); 132 if (use!=null) { 133 attributeSets = getAttributeSets(use, null); } 135 elementName = typeCheck("name", elementName); 136 namespace = typeCheck("namespace", namespace); 137 } 138 139 public Expression compile(Executable exec) throws XPathException { 140 141 NamespaceResolver nsContext = null; 142 143 145 if (elementName instanceof StringValue) { 146 CharSequence qName = ((StringValue)elementName).getStringValueCS(); 147 148 String [] parts; 149 try { 150 parts = Name.getQNameParts(qName); 151 } catch (QNameException e) { 152 compileError("Invalid element name: " + qName, "XTDE0820"); 153 return null; 154 } 155 156 String nsuri = null; 157 if (namespace instanceof StringValue) { 158 nsuri = ((StringValue)namespace).getStringValue(); 159 if (nsuri.equals("")) { 160 parts[0] = ""; 161 } 162 } else if (namespace==null) { 163 nsuri = getURIForPrefix(parts[0], true); 164 if (nsuri == null) { 165 undeclaredNamespaceError(parts[0], "XTDE0280"); 166 } 167 } 168 if (nsuri != null) { 169 int nameCode = getTargetNamePool().allocate(parts[0], nsuri, parts[1]); 171 FixedElement inst = new FixedElement(nameCode, 172 null, 173 inheritNamespaces, 174 schemaType, 175 validation); 176 Expression content = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true); 177 178 if (attributeSets != null) { 179 UseAttributeSets use = new UseAttributeSets(attributeSets); 180 if (content == null) { 181 content = use; 182 } else { 183 content = Block.makeBlock(use, content); 184 } 185 } 186 if (content == null) { 187 content = EmptySequence.getInstance(); 188 } 189 inst.setContentExpression(content); 190 ExpressionTool.makeParentReferences(inst); 191 return inst; 192 } 193 } else { 194 197 if (namespace==null) { 198 nsContext = makeNamespaceContext(); 199 } 200 } 201 202 ComputedElement inst = new ComputedElement( elementName, 203 namespace, 204 nsContext, 205 schemaType, 206 validation, 207 inheritNamespaces, 208 false); 209 Expression content = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true); 210 if (attributeSets != null) { 211 UseAttributeSets use = new UseAttributeSets(attributeSets); 212 if (content == null) { 213 content = use; 214 } else { 215 content = Block.makeBlock(use, content); 216 } 217 } 218 if (content == null) { 219 content = EmptySequence.getInstance(); 220 } 221 inst.setContentExpression(content); 222 ExpressionTool.makeParentReferences(inst); 223 return inst; 224 } 225 226 227 } 228 229 | Popular Tags |