1 16 19 package org.apache.xalan.processor; 20 21 import javax.xml.transform.SourceLocator ; 22 23 import org.apache.xalan.templates.ElemApplyImport; 24 import org.apache.xalan.templates.ElemApplyTemplates; 25 import org.apache.xalan.templates.ElemAttribute; 26 import org.apache.xalan.templates.ElemCallTemplate; 27 import org.apache.xalan.templates.ElemComment; 28 import org.apache.xalan.templates.ElemCopy; 29 import org.apache.xalan.templates.ElemCopyOf; 30 import org.apache.xalan.templates.ElemElement; 31 import org.apache.xalan.templates.ElemExsltFuncResult; 32 import org.apache.xalan.templates.ElemExsltFunction; 33 import org.apache.xalan.templates.ElemFallback; 34 import org.apache.xalan.templates.ElemLiteralResult; 35 import org.apache.xalan.templates.ElemNumber; 36 import org.apache.xalan.templates.ElemPI; 37 import org.apache.xalan.templates.ElemParam; 38 import org.apache.xalan.templates.ElemTemplate; 39 import org.apache.xalan.templates.ElemTemplateElement; 40 import org.apache.xalan.templates.ElemText; 41 import org.apache.xalan.templates.ElemTextLiteral; 42 import org.apache.xalan.templates.ElemValueOf; 43 import org.apache.xalan.templates.ElemVariable; 44 import org.apache.xalan.templates.ElemMessage; 45 import org.apache.xalan.templates.Stylesheet; 46 47 import org.xml.sax.Attributes ; 48 import org.xml.sax.SAXException ; 49 50 51 55 public class ProcessorExsltFunction extends ProcessorTemplateElem 56 { 57 61 public void startElement( 62 StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) 63 throws SAXException 64 { 65 String msg = ""; 67 if (!(handler.getElemTemplateElement() instanceof Stylesheet)) 68 { 69 msg = "func:function element must be top level."; 70 handler.error(msg, new SAXException (msg)); 71 } 72 super.startElement(handler, uri, localName, rawName, attributes); 73 74 String val = attributes.getValue("name"); 75 int indexOfColon = val.indexOf(":"); 76 if (indexOfColon > 0) 77 { 78 String prefix = val.substring(0, indexOfColon); 79 String localVal = val.substring(indexOfColon + 1); 80 String ns = handler.getNamespaceSupport().getURI(prefix); 81 } 84 else 85 { 86 msg = "func:function name must have namespace"; 87 handler.error(msg, new SAXException (msg)); 88 } 89 } 90 91 94 protected void appendAndPush( 95 StylesheetHandler handler, ElemTemplateElement elem) 96 throws SAXException 97 { 98 super.appendAndPush(handler, elem); 100 elem.setDOMBackPointer(handler.getOriginatingNode()); 102 handler.getStylesheet().setTemplate((ElemTemplate) elem); 103 } 104 105 108 public void endElement( 109 StylesheetHandler handler, String uri, String localName, String rawName) 110 throws SAXException 111 { 112 ElemTemplateElement function = handler.getElemTemplateElement(); 113 SourceLocator locator = handler.getLocator(); 114 validate(function, handler); super.endElement(handler, uri, localName, rawName); 116 } 117 118 123 public void validate(ElemTemplateElement elem, StylesheetHandler handler) 124 throws SAXException 125 { 126 String msg = ""; 127 while (elem != null) 128 { 129 if (elem instanceof ElemExsltFuncResult 131 && elem.getNextSiblingElem() != null 132 && !(elem.getNextSiblingElem() instanceof ElemFallback)) 133 { 134 msg = "func:result has an illegal following sibling (only xsl:fallback allowed)"; 135 handler.error(msg, new SAXException (msg)); 136 } 137 138 if((elem instanceof ElemApplyImport 139 || elem instanceof ElemApplyTemplates 140 || elem instanceof ElemAttribute 141 || elem instanceof ElemCallTemplate 142 || elem instanceof ElemComment 143 || elem instanceof ElemCopy 144 || elem instanceof ElemCopyOf 145 || elem instanceof ElemElement 146 || elem instanceof ElemLiteralResult 147 || elem instanceof ElemNumber 148 || elem instanceof ElemPI 149 || elem instanceof ElemText 150 || elem instanceof ElemTextLiteral 151 || elem instanceof ElemValueOf) 152 && !(ancestorIsOk(elem))) 153 { 154 msg ="misplaced literal result in a func:function container."; 155 handler.error(msg, new SAXException (msg)); 156 } 157 ElemTemplateElement nextElem = elem.getFirstChildElem(); 158 while (nextElem == null) 159 { 160 nextElem = elem.getNextSiblingElem(); 161 if (nextElem == null) 162 elem = elem.getParentElem(); 163 if (elem == null || elem instanceof ElemExsltFunction) 164 return; } 166 elem = nextElem; 167 } 168 } 169 170 174 175 boolean ancestorIsOk(ElemTemplateElement child) 176 { 177 while (child.getParentElem() != null && !(child.getParentElem() instanceof ElemExsltFunction)) 178 { 179 ElemTemplateElement parent = child.getParentElem(); 180 if (parent instanceof ElemExsltFuncResult 181 || parent instanceof ElemVariable 182 || parent instanceof ElemParam 183 || parent instanceof ElemMessage) 184 return true; 185 child = parent; 186 } 187 return false; 188 } 189 190 } 191 | Popular Tags |