View Javadoc

1   package org.apache.dvsl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  import java.util.List;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.io.Writer;
27  
28  import org.apache.velocity.context.InternalContextAdapterImpl;
29  import org.apache.velocity.runtime.parser.node.SimpleNode;
30  import org.apache.velocity.context.Context;
31  
32  import org.dom4j.DocumentHelper;
33  import org.dom4j.Document;
34  import org.dom4j.XPath;
35  import org.dom4j.Node;
36  import org.dom4j.rule.Pattern;
37  import org.dom4j.rule.Rule;
38  
39  /**
40   *  Currently provides the match rule accumulation
41   *  as well as the AST storage and rendering
42   *
43   *  Rule stuff might be replaced with the dom4j RuleManager
44   *
45   *  @author <a href="mailto:geirm@apache.org?">Geir Magnusson Jr.</a>
46   */
47  public class TemplateHandler
48  {
49      private Map astmap = new HashMap();
50      private List xpathList = new ArrayList();
51  
52      public void registerMatch( String xpath, SimpleNode node )
53      {
54          //System.out.println("registering : " + xpath );
55  
56          Pattern pattern = DocumentHelper.createPattern( xpath );
57          Rule rule = new Rule( pattern );
58          Map foo = new HashMap();
59  
60          foo.put("rule", rule );
61          foo.put("xpath", xpath );
62          foo.put("ast", node );
63          xpathList.add( foo );
64      }
65  
66  
67      boolean render( DVSLNode node, Context context, Writer writer )
68          throws Exception
69      {
70          /*
71           *  find if we have an AST where the xpath expression mathes
72           *  for this node
73           */
74  
75          Node dom4jnode = (Node) node.getNodeImpl();
76          SimpleNode sn = null;
77  
78          for( int i = 0; i < xpathList.size(); i++ )
79          {
80              Map m = (Map) xpathList.get(i);
81  
82              Rule xpathrule = (Rule) m.get("rule");
83  
84              if( xpathrule.matches( dom4jnode ) )
85              {
86                  sn = (SimpleNode) m.get( "ast" );
87                  //System.out.println("using : " + (String) m.get("xpath") );
88  
89                  break;
90              }
91          }
92  
93          if( sn == null)
94              ; //System.out.println("Yikes : failed to find ast for '" + node.getName() );
95  
96          /*
97           *  if we found something, render
98           */
99  
100         if( sn != null)
101         {
102             InternalContextAdapterImpl ica =
103                 new InternalContextAdapterImpl( context );
104 
105             ica.pushCurrentTemplateName( node.name() );
106 
107             try
108             {
109                 sn.render( ica, writer );
110             }
111             finally
112             {
113                 ica.popCurrentTemplateName();
114             }
115 
116             return true;
117         }
118 
119         return false;
120     }
121 
122 }