in blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/transformation/SessionPostTransformer.java [231:398]
public void endTransformingElement(String uri, String name, String raw)
throws ProcessingException, IOException, SAXException {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("BEGIN endTransformingElement uri=" + uri + ", name=" + name + ", raw=" + raw);
}
if (name.equals(DELETECONTEXT_ELEMENT)) {
// do nothing, the context was destroyed on the startElement event
// Element: setxml
} else if (name.equals(SETXML_ELEMENT)) {
String path = (String)stack.pop();
String contextName = (String)stack.pop();
this.getSessionManager().setContextFragment(contextName, path, this.endRecording());
// Element: mergexml
} else if (name.equals(MERGEXML_ELEMENT)) {
String path = (String)stack.pop();
String contextName = (String)stack.pop();
this.getSessionManager().mergeContextFragment(contextName, path, this.endRecording());
// Element: appendxml
} else if (name.equals(APPENDXML_ELEMENT)) {
String path = (String)stack.pop();
String contextName = (String)stack.pop();
this.getSessionManager().appendContextFragment(contextName, path, this.endRecording());
// Element: removexml
} else if (name.equals(REMOVEXML_ELEMENT)) {
String path = (String)stack.pop();
String contextName = (String)stack.pop();
// result is ignored
endTextRecording();
this.getSessionManager().removeContextFragment(contextName, path);
// Element: savexml
} else if (name.equals(SAVECONTEXT_ELEMENT)) {
String path = (String)stack.pop();
String contextName = (String)stack.pop();
SourceParameters pars = this.endParametersRecording((SourceParameters)null);
pars.setSingleParameterValue("contextname", contextName);
pars.setSingleParameterValue("path", path);
this.getContextManager().getContext(contextName).saveXML(path, pars);
// Element: inputxml
} else if (name.equals(INPUTXML_ELEMENT)) {
String path = (String)this.stack.pop();
String fieldname = (String)this.stack.pop();
String contextname = (String)this.stack.pop();
DocumentFragment defaultFragment = this.endRecording();
if (this.formName == null) {
throw new ProcessingException("The inputxml must be contained inside a form.");
}
DocumentFragment value = this.getFormManager().registerInputField(contextname, path,
fieldname, formName);
if (value == null) {
value = defaultFragment;
}
this.sendEvents(value);
super.endTransformingElement("", name, name);
// Element form
} else if (name.equals(FORM_ELEMENT) && this.state == STATE_FORM) {
this.state = ((Integer)this.stack.pop()).intValue();
this.sendEndElementEvent("form");
this.formName = null;
// Element form action
} else if (name.equals(FORM_ACTION_ELEMENT) && this.state == STATE_FORM) {
String action = this.endTextRecording();
AttributesImpl a = (AttributesImpl)this.stack.pop();
this.formName = a.getValue("name");
boolean hasPars = (action.indexOf("?") != -1);
action = this.response.encodeURL(action + (hasPars ? '&' : '?')
+ SessionConstants.SESSION_FORM_PARAMETER + '=' + this.formName);
a.addAttribute("", "action", "action", "CDATA", action);
if (a.getValue("method") == null) {
a.addAttribute("", "method", "method", "CDATA", "POST");
}
this.sendStartElementEvent("form", a);
// Element form content
} else if (name.equals(FORM_CONTENT_ELEMENT) && this.state == STATE_FORM) {
// ignore this
// Element form validation rules
} else if (name.equals(FORM_VALIDATION_ELEMENT) && this.state == STATE_FORM) {
if (this.formName == null) {
throw new ProcessingException("The validate element must be contained inside a form.");
}
DocumentFragment validationDoc = this.endRecording();
String source = (String)stack.pop();
if (!Objects.equals(source, "EMPTY")) {
// get configuration from external file
// referenced by "src" attribute of "validate" element
try {
Source resource = this.resolver.resolveURI(source);
SAXConfigurationHandler saxBuilder = new SAXConfigurationHandler();
SourceUtil.parse(this.manager, resource, saxBuilder);
Configuration conf = saxBuilder.getConfiguration();
HttpSession session = this.getSessionManager().getSession(true);
session.setAttribute(this.formName, conf);
if (validationDoc != null) {
//validationDoc contains "constraint-set" element
validationDoc.normalize();
Node validationNode = validationDoc.getFirstChild();
while (validationNode.getNodeType() != Node.ELEMENT_NODE) {
validationNode = validationNode.getNextSibling();
if (validationNode == null) {
break;
}
}
if (validationNode != null && validationNode.getNodeType() == Node.ELEMENT_NODE
&& validationNode.getNodeName().equals(FORM_VALIDATESET_ELEMENT)) {
Properties props = XMLUtils.createPropertiesForXML(false);
props.put(OutputKeys.ENCODING, "ISO-8859-1");
String validationXML = XMLUtils.serializeNode(validationNode, props);
DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
conf = builder.build(new ByteArrayInputStream(validationXML.getBytes()));
session.setAttribute(this.formName + "constraint-set", conf);
}
}
} catch (SourceException se) {
throw new ProcessingException("Cannot resolve" + source, se);
} catch (ConfigurationException ce) {
throw new ProcessingException("Error building Configuration out of constraint-set element", ce);
}
} else if (validationDoc != null) {
//validationDoc contains the validation rules inline
try {
validationDoc.normalize();
Node validationNode = validationDoc.getFirstChild();
while (validationNode.getNodeType() != Node.ELEMENT_NODE) {
validationNode = validationNode.getNextSibling();
if (validationNode == null) {
break;
}
}
if (validationNode != null && validationNode.getNodeType() == Node.ELEMENT_NODE
&& validationNode.getNodeName().equals("root")) {
Properties props = XMLUtils.createPropertiesForXML(false);
props.put(OutputKeys.ENCODING, "ISO-8859-1");
String validationXML = XMLUtils.serializeNode(validationNode, props);
DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
Configuration conf = builder.build(new ByteArrayInputStream(validationXML.getBytes()));
HttpSession session = this.getSessionManager().getSession(true);
session.setAttribute(this.formName, conf);
//the constraint-set to validate is the first and single one
session.setAttribute(this.formName + "constraint-set", conf.getChildren("constraint-set")[0]);
}
} catch (ConfigurationException ce) {
throw new ProcessingException("Error building Configuration out of validation XML", ce);
}
}
} else {
super.endTransformingElement(uri, name, raw);
}
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("END endTransformingElement");
}
}