| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.instruct.*; |
| 12 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 13 |
import net.sf.saxon.lib.Validation; |
| 14 |
import net.sf.saxon.om.AttributeCollection; |
| 15 |
import net.sf.saxon.om.StructuredQName; |
| 16 |
import net.sf.saxon.pattern.NodeKindTest; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.SchemaType; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
import net.sf.saxon.value.Whitespace; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class XSLCopy extends StyleElement { |
| 27 |
|
| 28 |
private String use; |
| 29 |
private StructuredQName[] attributeSets = null; |
| 30 |
private boolean copyNamespaces = true; |
| 31 |
private boolean inheritNamespaces = true; |
| 32 |
private int validationAction = Validation.PRESERVE; |
| 33 |
private SchemaType schemaType = null; |
| 34 |
private Expression select = null; |
| 35 |
private boolean selectSpecified = false; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public boolean isInstruction() { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public boolean mayContainSequenceConstructor() { |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
public void prepareAttributes() throws XPathException { |
| 58 |
|
| 59 |
AttributeCollection atts = getAttributeList(); |
| 60 |
String copyNamespacesAtt = null; |
| 61 |
String validationAtt = null; |
| 62 |
String typeAtt = null; |
| 63 |
String inheritAtt = null; |
| 64 |
String selectAtt = null; |
| 65 |
|
| 66 |
for (int a = 0; a < atts.getLength(); a++) { |
| 67 |
String f = atts.getQName(a); |
| 68 |
if (f.equals("use-attribute-sets")) { |
| 69 |
use = atts.getValue(a); |
| 70 |
} else if (f.equals("copy-namespaces")) { |
| 71 |
copyNamespacesAtt = Whitespace.trim(atts.getValue(a)); |
| 72 |
} else if (f.equals("select")) { |
| 73 |
selectAtt = atts.getValue(a); |
| 74 |
} else if (f.equals("type")) { |
| 75 |
typeAtt = Whitespace.trim(atts.getValue(a)); |
| 76 |
} else if (f.equals("validation")) { |
| 77 |
validationAtt = Whitespace.trim(atts.getValue(a)); |
| 78 |
} else if (f.equals("inherit-namespaces")) { |
| 79 |
inheritAtt = Whitespace.trim(atts.getValue(a)); |
| 80 |
} else { |
| 81 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (copyNamespacesAtt != null) { |
| 86 |
copyNamespaces = processBooleanAttribute("copy-namespaces", copyNamespacesAtt); |
| 87 |
} |
| 88 |
|
| 89 |
if (typeAtt != null && validationAtt != null) { |
| 90 |
compileError("The type and validation attributes must not both be specified", "XTSE1505"); |
| 91 |
} |
| 92 |
|
| 93 |
if (validationAtt != null) { |
| 94 |
validationAction = validateValidationAttribute(validationAtt); |
| 95 |
} else { |
| 96 |
validationAction = getDefaultValidation(); |
| 97 |
} |
| 98 |
|
| 99 |
if (typeAtt != null) { |
| 100 |
schemaType = getSchemaType(typeAtt); |
| 101 |
if (!isSchemaAware()) { |
| 102 |
compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660"); |
| 103 |
} |
| 104 |
validationAction = Validation.BY_TYPE; |
| 105 |
} |
| 106 |
if (inheritAtt != null) { |
| 107 |
inheritNamespaces = processBooleanAttribute("inherit-namespaces", inheritAtt); |
| 108 |
} |
| 109 |
|
| 110 |
if (selectAtt != null) { |
| 111 |
select = makeExpression(selectAtt, getAttributeList().getIndex("", "select")); |
| 112 |
selectSpecified = true; |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 118 |
if (use != null) { |
| 119 |
|
| 120 |
attributeSets = getUsedAttributeSets(use); |
| 121 |
} |
| 122 |
|
| 123 |
if (select == null) { |
| 124 |
select = new ContextItemExpression(); |
| 125 |
select.setLocation(allocateLocation()); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 130 |
|
| 131 |
select = typeCheck("select", select); |
| 132 |
try { |
| 133 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:copy/select", 0); |
| 134 |
role.setErrorCode("XTTE3180"); |
| 135 |
select = getConfiguration().getTypeChecker(false).staticTypeCheck(select, SequenceType.OPTIONAL_ITEM, |
| 136 |
role, makeExpressionVisitor()); |
| 137 |
} catch (XPathException err) { |
| 138 |
compileError(err); |
| 139 |
} |
| 140 |
|
| 141 |
Expression content = compileSequenceConstructor(exec, decl, true); |
| 142 |
|
| 143 |
if (attributeSets != null) { |
| 144 |
Expression use = UseAttributeSet.makeUseAttributeSets(attributeSets, this); |
| 145 |
|
| 146 |
|
| 147 |
Expression condition = new InstanceOfExpression( |
| 148 |
new ContextItemExpression(), |
| 149 |
SequenceType.makeSequenceType(NodeKindTest.ELEMENT, StaticProperty.EXACTLY_ONE)); |
| 150 |
Expression choice = Choose.makeConditional(condition, use); |
| 151 |
if (content == null) { |
| 152 |
content = choice; |
| 153 |
} else { |
| 154 |
content = Block.makeBlock(choice, content); |
| 155 |
content.setLocation( |
| 156 |
allocateLocation()); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if (content == null) { |
| 161 |
content = Literal.makeEmptySequence(); |
| 162 |
} |
| 163 |
|
| 164 |
Copy inst = new Copy( |
| 165 |
|
| 166 |
copyNamespaces, |
| 167 |
inheritNamespaces, |
| 168 |
schemaType, |
| 169 |
validationAction); |
| 170 |
|
| 171 |
inst.setContentExpression(content); |
| 172 |
|
| 173 |
if (selectSpecified) { |
| 174 |
return new ForEach(select, inst); |
| 175 |
} else { |
| 176 |
return inst; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|