1 package net.sf.saxon.instruct; 2 import net.sf.saxon.event.Receiver; 3 import net.sf.saxon.expr.*; 4 import net.sf.saxon.om.NamePool; 5 import net.sf.saxon.om.Validation; 6 import net.sf.saxon.pattern.CombinedNodeTest; 7 import net.sf.saxon.pattern.ContentTypeTest; 8 import net.sf.saxon.pattern.NameTest; 9 import net.sf.saxon.style.StandardNames; 10 import net.sf.saxon.trace.InstructionInfo; 11 import net.sf.saxon.trace.Location; 12 import net.sf.saxon.trans.StaticError; 13 import net.sf.saxon.trans.XPathException; 14 import net.sf.saxon.type.*; 15 16 import java.io.PrintStream ; 17 18 19 25 26 public class FixedElement extends ElementCreator { 27 28 private int nameCode; 29 protected int[] namespaceCodes = null; 30 private ItemType itemType; 31 32 40 public FixedElement(int nameCode, 41 int[] namespaceCodes, 42 boolean inheritNamespaces, 43 SchemaType schemaType, 44 int validation) { 45 this.nameCode = nameCode; 46 this.namespaceCodes = namespaceCodes; 47 this.inheritNamespaces = inheritNamespaces; 48 setSchemaType(schemaType); 49 this.validation = validation; 50 } 51 52 public InstructionInfo getInstructionInfo() { 53 InstructionDetails details = (InstructionDetails)super.getInstructionInfo(); 54 details.setConstructType(Location.LITERAL_RESULT_ELEMENT); 55 details.setObjectNameCode(nameCode); 56 return details; 57 } 58 59 67 68 public Expression simplify(StaticContext env) throws XPathException { 69 setLazyConstruction(env.getConfiguration().isLazyConstructionMode()); 70 if (getSchemaType() == null) { 71 if (validation == Validation.STRICT) { 72 SchemaDeclaration decl = env.getConfiguration().getElementDeclaration(nameCode & 0xfffff); 73 if (decl == null) { 74 StaticError err = new StaticError("There is no global element declaration for " + 75 env.getNamePool().getDisplayName(nameCode) + 76 ", so strict validation will fail"); 77 err.setIsTypeError(true); 78 err.setLocator(this); 79 throw err; 80 } 81 setSchemaType(decl.getType()); 82 itemType = new CombinedNodeTest( 83 new NameTest(Type.ELEMENT, nameCode, env.getNamePool()), 84 Token.INTERSECT, 85 new ContentTypeTest(Type.ELEMENT, getSchemaType(), env.getConfiguration())); 86 getSchemaType().analyzeContentExpression(content, Type.ELEMENT, env); 87 } else if (validation == Validation.LAX) { 88 SchemaDeclaration decl = env.getConfiguration().getElementDeclaration(nameCode & 0xfffff); 89 if (decl == null) { 90 env.issueWarning("There is no global element declaration for " + 91 env.getNamePool().getDisplayName(nameCode) + 92 ", so lax validation has no effect", this); 93 itemType = new CombinedNodeTest( 94 new NameTest(Type.ELEMENT, nameCode, env.getNamePool()), 95 Token.INTERSECT, 96 new ContentTypeTest(Type.ELEMENT, 97 BuiltInSchemaFactory.getSchemaType(StandardNames.XDT_UNTYPED), 98 env.getConfiguration())); 99 } else { 100 setSchemaType(decl.getType()); 101 itemType = new CombinedNodeTest( 102 new NameTest(Type.ELEMENT, nameCode, env.getNamePool()), 103 Token.INTERSECT, 104 new ContentTypeTest(Type.ELEMENT, getSchemaType(), env.getConfiguration())); 105 getSchemaType().analyzeContentExpression(content, Type.ELEMENT, env); 106 } 107 } else if (validation == Validation.PRESERVE) { 108 itemType = new CombinedNodeTest( 110 new NameTest(Type.ELEMENT, nameCode, env.getNamePool()), 111 Token.INTERSECT, 112 new ContentTypeTest(Type.ELEMENT, 113 BuiltInSchemaFactory.getSchemaType(StandardNames.XS_ANY_TYPE), 114 env.getConfiguration())); 115 } else { 116 itemType = new CombinedNodeTest( 118 new NameTest(Type.ELEMENT, nameCode, env.getNamePool()), 119 Token.INTERSECT, 120 new ContentTypeTest(Type.ELEMENT, 121 BuiltInSchemaFactory.getSchemaType(StandardNames.XDT_UNTYPED), 122 env.getConfiguration())); 123 } 124 } else { 125 itemType = new CombinedNodeTest( 126 new NameTest(Type.ELEMENT, nameCode, env.getNamePool()), 127 Token.INTERSECT, 128 new ContentTypeTest(Type.ELEMENT, getSchemaType(), env.getConfiguration()) 129 ); 130 getSchemaType().analyzeContentExpression(content, Type.ELEMENT, env); 131 } 132 return super.simplify(env); 133 } 134 135 139 public ItemType getItemType() { 140 if (itemType == null) { 141 return super.getItemType(); 142 } 143 return itemType; 144 } 145 146 152 153 public int getNameCode(XPathContext context) { 154 return nameCode; 155 } 156 157 164 165 public void checkPermittedContents(SchemaType parentType, StaticContext env, boolean whole) throws XPathException { 166 if (parentType instanceof SimpleType) { 167 StaticError err = new StaticError("Element " + env.getNamePool().getDisplayName(nameCode) + 168 " is not permitted here: the containing element is of simple type " + parentType.getDescription()); 169 err.setIsTypeError(true); 170 err.setLocator(this); 171 throw err; 172 } else if (((ComplexType)parentType).isSimpleContent()) { 173 StaticError err = new StaticError("Element " + env.getNamePool().getDisplayName(nameCode) + 174 " is not permitted here: the containing element has a complex type with simple content"); 175 err.setIsTypeError(true); 176 err.setLocator(this); 177 throw err; 178 } 179 SchemaType type; 180 try { 181 type = ((ComplexType)parentType).getElementParticleType(nameCode & 0xfffff); 182 } catch (SchemaException e) { 183 throw new StaticError(e); 184 } 185 if (type == null) { 186 if (parentType.allowsDerivation(SchemaType.DERIVATION_EXTENSION)) { 187 env.issueWarning("Element " + env.getNamePool().getDisplayName(nameCode) + 189 " is not permitted in the content model of the complex type " + 190 parentType.getDescription() + 191 ". Validation will fail unless there is an extended type that permits this element", this); 192 return; 193 } else { 194 StaticError err = new StaticError("Element " + env.getNamePool().getDisplayName(nameCode) + 195 " is not permitted in the content model of the complex type " + 196 parentType.getDescription()); 197 err.setIsTypeError(true); 198 err.setLocator(this); 199 throw err; 200 } 201 } 202 if (type instanceof AnyType) { 203 return; 204 } 205 206 try { 207 content.checkPermittedContents(type, env, true); 208 } catch (XPathException e) { 209 if (e.getLocator() == null || e.getLocator() == e) { 210 e.setLocator(this); 211 } 212 throw e; 213 } 214 } 215 216 221 222 protected void outputNamespaceNodes(XPathContext context, Receiver out) 223 throws XPathException { 224 if (namespaceCodes != null) { 225 for (int i=0; i<namespaceCodes.length; i++) { 226 out.namespace(namespaceCodes[i], 0); 227 } 228 } 229 } 230 231 236 237 public int[] getActiveNamespaces() throws XPathException { 238 return namespaceCodes; 239 } 240 241 244 245 public void display(int level, NamePool pool, PrintStream out) { 246 out.println(ExpressionTool.indent(level) + "element "); 247 out.println(ExpressionTool.indent(level+1) + "name " + 248 (pool==null ? nameCode+"" : pool.getDisplayName(nameCode))); 249 out.println(ExpressionTool.indent(level+1) + "content"); 250 content.display(level+1, pool, out); 251 } 252 } 253 254 | Popular Tags |