001: package net.sf.saxon.sql;
002:
003: import net.sf.saxon.expr.*;
004: import net.sf.saxon.instruct.Executable;
005: import net.sf.saxon.instruct.GeneralVariable;
006: import net.sf.saxon.instruct.InstructionDetails;
007: import net.sf.saxon.instruct.TailCall;
008: import net.sf.saxon.om.AttributeCollection;
009: import net.sf.saxon.om.Navigator;
010: import net.sf.saxon.om.ValueRepresentation;
011: import net.sf.saxon.style.XSLGeneralVariable;
012: import net.sf.saxon.trace.InstructionInfo;
013: import net.sf.saxon.trace.Location;
014: import net.sf.saxon.trans.XPathException;
015: import net.sf.saxon.value.SequenceType;
016:
017: /**
018: * An sql:column element in the stylesheet.
019: */
020:
021: public class SQLColumn extends XSLGeneralVariable {
022:
023: /**
024: * Determine whether this node is an instruction.
025: * @return false - it is not an instruction
026: */
027:
028: public boolean isInstruction() {
029: return false;
030: }
031:
032: /**
033: * Determine whether this type of element is allowed to contain a template-body
034: * @return false: no, it may not contain a template-body
035: */
036:
037: public boolean mayContainSequenceConstructor() {
038: return false;
039: }
040:
041: public void prepareAttributes() throws XPathException {
042:
043: getVariableFingerprint();
044:
045: AttributeCollection atts = getAttributeList();
046:
047: String selectAtt = null;
048: String nameAtt = null;
049:
050: for (int a = 0; a < atts.getLength(); a++) {
051: String localName = atts.getLocalName(a);
052: if (localName.equals("name")) {
053: nameAtt = atts.getValue(a).trim();
054: } else if (localName.equals("select")) {
055: selectAtt = atts.getValue(a);
056: } else {
057: checkUnknownAttribute(atts.getNameCode(a));
058: }
059: }
060:
061: if (nameAtt == null) {
062: reportAbsence("name");
063: } else if (!getConfiguration().getNameChecker()
064: .isQName(nameAtt)) {
065: compileError("Column name must be a valid QName");
066: }
067:
068: if (selectAtt != null) {
069: select = makeExpression(selectAtt);
070: }
071:
072: }
073:
074: public void validate() throws XPathException {
075: if (!(getParent() instanceof SQLInsert)) {
076: compileError("parent node must be sql:insert");
077: }
078: select = typeCheck("select", select);
079: try {
080: RoleLocator role = new RoleLocator(RoleLocator.INSTRUCTION,
081: "sql:column/select", 0, null);
082: role.setSourceLocator(new ExpressionLocation(this ));
083: select = TypeChecker.staticTypeCheck(select,
084: SequenceType.SINGLE_ATOMIC, false, role,
085: getStaticContext());
086:
087: } catch (XPathException err) {
088: compileError(err);
089: }
090: }
091:
092: public Expression compile(Executable exec) throws XPathException {
093: ColumnInstruction inst = new ColumnInstruction();
094: initializeInstruction(exec, inst);
095: return inst;
096: }
097:
098: public String getColumnName() {
099: return Navigator.getAttributeValue(this , "", "name");
100: }
101:
102: protected static class ColumnInstruction extends GeneralVariable {
103:
104: public ColumnInstruction() {
105: }
106:
107: public InstructionInfo getInstructionInfo() {
108: InstructionDetails details = (InstructionDetails) super
109: .getInstructionInfo();
110: details.setConstructType(Location.EXTENSION_INSTRUCTION);
111: return details;
112: }
113:
114: public TailCall processLeavingTail(XPathContext context) {
115: return null;
116: }
117:
118: /**
119: * Evaluate the variable (method exists only to satisfy the interface)
120: */
121:
122: public ValueRepresentation evaluateVariable(XPathContext context)
123: throws XPathException {
124: throw new UnsupportedOperationException();
125: }
126:
127: }
128:
129: }
130:
131: //
132: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
133: // you may not use this file except in compliance with the License. You may obtain a copy of the
134: // License at http://www.mozilla.org/MPL/
135: //
136: // Software distributed under the License is distributed on an "AS IS" basis,
137: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
138: // See the License for the specific language governing rights and limitations under the License.
139: //
140: // The Original Code is: all this file.
141: //
142: // The Initial Developer of the Original Code is Michael H. Kay.
143: //
144: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
145: //
146: // Contributor(s): none.
147: //
|