| 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.SimpleNodeConstructor; |
| 12 |
import net.sf.saxon.expr.parser.RetainedStaticContext; |
| 13 |
import net.sf.saxon.functions.SystemFunction; |
| 14 |
import net.sf.saxon.om.AttributeCollection; |
| 15 |
import net.sf.saxon.om.AxisInfo; |
| 16 |
import net.sf.saxon.om.NodeInfo; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 19 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 20 |
import net.sf.saxon.type.Type; |
| 21 |
import net.sf.saxon.value.StringValue; |
| 22 |
import net.sf.saxon.value.Whitespace; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public abstract class XSLLeafNodeConstructor extends StyleElement { |
| 30 |
|
| 31 |
|
| 32 |
protected Expression select = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected Expression prepareAttributesNameAndSelect() throws XPathException { |
| 43 |
|
| 44 |
Expression name = null; |
| 45 |
String nameAtt = null; |
| 46 |
String selectAtt = null; |
| 47 |
|
| 48 |
AttributeCollection atts = getAttributeList(); |
| 49 |
|
| 50 |
for (int a = 0; a < atts.getLength(); a++) { |
| 51 |
String f = atts.getQName(a); |
| 52 |
if (f.equals("name")) { |
| 53 |
nameAtt = Whitespace.trim(atts.getValue(a)); |
| 54 |
name = makeAttributeValueTemplate(nameAtt, a); |
| 55 |
} else if (f.equals("select")) { |
| 56 |
selectAtt = atts.getValue(a); |
| 57 |
select = makeExpression(selectAtt, a); |
| 58 |
} else { |
| 59 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if (nameAtt == null) { |
| 64 |
reportAbsence("name"); |
| 65 |
} |
| 66 |
|
| 67 |
return name; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public boolean isInstruction() { |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public boolean mayContainSequenceConstructor() { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 91 |
if (select != null && hasChildNodes()) { |
| 92 |
String errorCode = getErrorCodeForSelectPlusContent(); |
| 93 |
compileError("An " + getDisplayName() + " element with a select attribute must be empty", errorCode); |
| 94 |
} |
| 95 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 96 |
NodeInfo first = kids.next(); |
| 97 |
if (select == null) { |
| 98 |
if (first == null) { |
| 99 |
|
| 100 |
|
| 101 |
select = new StringLiteral(StringValue.EMPTY_STRING); |
| 102 |
select.setRetainedStaticContext(makeRetainedStaticContext()); |
| 103 |
} else { |
| 104 |
if (kids.next() == null && !isExpandingText()) { |
| 105 |
|
| 106 |
if (first.getNodeKind() == Type.TEXT) { |
| 107 |
|
| 108 |
select = new StringLiteral(first.getStringValue()); |
| 109 |
select.setRetainedStaticContext(makeRetainedStaticContext()); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected abstract String getErrorCodeForSelectPlusContent(); |
| 123 |
|
| 124 |
protected void compileContent(Compilation exec, ComponentDeclaration decl, SimpleNodeConstructor inst, Expression separator) throws XPathException { |
| 125 |
if (separator == null) { |
| 126 |
separator = new StringLiteral(StringValue.SINGLE_SPACE); |
| 127 |
} |
| 128 |
try { |
| 129 |
if (select == null) { |
| 130 |
select = compileSequenceConstructor(exec, decl, true); |
| 131 |
} |
| 132 |
select = makeSimpleContentConstructor(select, separator, getStaticContext()); |
| 133 |
inst.setSelect(select); |
| 134 |
|
| 135 |
} catch (XPathException err) { |
| 136 |
compileError(err); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static Expression makeSimpleContentConstructor(Expression select, Expression separator, StaticContext env) { |
| 152 |
RetainedStaticContext rsc = select.getLocalRetainedStaticContext(); |
| 153 |
if (rsc == null) { |
| 154 |
rsc = env.makeRetainedStaticContext(); |
| 155 |
} |
| 156 |
|
| 157 |
select = AdjacentTextNodeMerger.makeAdjacentTextNodeMerger(select); |
| 158 |
|
| 159 |
select = Atomizer.makeAtomizer(select, null); |
| 160 |
|
| 161 |
select = new AtomicSequenceConverter(select, BuiltInAtomicType.STRING); |
| 162 |
select.setRetainedStaticContext(rsc); |
| 163 |
((AtomicSequenceConverter) select).allocateConverterStatically(env.getConfiguration(), false); |
| 164 |
|
| 165 |
if (select.getCardinality() != StaticProperty.EXACTLY_ONE) { |
| 166 |
select = SystemFunction.makeCall("string-join", rsc, select, separator); |
| 167 |
} |
| 168 |
|
| 169 |
return select; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|