001: package org.apache.dvsl;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Map;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.io.Writer;
027:
028: import org.apache.velocity.context.InternalContextAdapterImpl;
029: import org.apache.velocity.runtime.parser.node.SimpleNode;
030: import org.apache.velocity.context.Context;
031:
032: import org.dom4j.DocumentHelper;
033: import org.dom4j.Document;
034: import org.dom4j.XPath;
035: import org.dom4j.Node;
036: import org.dom4j.rule.Pattern;
037: import org.dom4j.rule.Rule;
038:
039: /**
040: * Currently provides the match rule accumulation
041: * as well as the AST storage and rendering
042: *
043: * Rule stuff might be replaced with the dom4j RuleManager
044: *
045: * @author <a href="mailto:geirm@apache.org?">Geir Magnusson Jr.</a>
046: */
047: public class TemplateHandler {
048: private Map astmap = new HashMap();
049: private List xpathList = new ArrayList();
050:
051: public void registerMatch(String xpath, SimpleNode node) {
052: //System.out.println("registering : " + xpath );
053:
054: Pattern pattern = DocumentHelper.createPattern(xpath);
055: Rule rule = new Rule(pattern);
056: Map foo = new HashMap();
057:
058: foo.put("rule", rule);
059: foo.put("xpath", xpath);
060: foo.put("ast", node);
061: xpathList.add(foo);
062: }
063:
064: boolean render(DVSLNode node, Context context, Writer writer)
065: throws Exception {
066: /*
067: * find if we have an AST where the xpath expression mathes
068: * for this node
069: */
070:
071: Node dom4jnode = (Node) node.getNodeImpl();
072: SimpleNode sn = null;
073:
074: for (int i = 0; i < xpathList.size(); i++) {
075: Map m = (Map) xpathList.get(i);
076:
077: Rule xpathrule = (Rule) m.get("rule");
078:
079: if (xpathrule.matches(dom4jnode)) {
080: sn = (SimpleNode) m.get("ast");
081: //System.out.println("using : " + (String) m.get("xpath") );
082:
083: break;
084: }
085: }
086:
087: if (sn == null)
088: ; //System.out.println("Yikes : failed to find ast for '" + node.getName() );
089:
090: /*
091: * if we found something, render
092: */
093:
094: if (sn != null) {
095: InternalContextAdapterImpl ica = new InternalContextAdapterImpl(
096: context);
097:
098: ica.pushCurrentTemplateName(node.name());
099:
100: try {
101: sn.render(ica, writer);
102: } finally {
103: ica.popCurrentTemplateName();
104: }
105:
106: return true;
107: }
108:
109: return false;
110: }
111:
112: }
|