2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: BuilderFactory.java 464373 2006-10-16 04:21:54Z rahul $
20   */
21  package org.apache.shale.clay.parser.builder;
22  
23  import java.net.URL;
24  
25  import javax.faces.context.FacesContext;
26  
27  import org.apache.commons.chain.Catalog;
28  import org.apache.commons.chain.CatalogFactory;
29  import org.apache.commons.chain.Command;
30  import org.apache.commons.chain.config.ConfigParser;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.shale.clay.config.Globals;
34  import org.apache.shale.clay.parser.Node;
35  import org.apache.shale.clay.parser.builder.chain.BuilderRuleContext;
36  
37  /***
38   * <p>
39   * This is an abstract factory that returns a {@link Builder} mapped to a
40   * {@link Node}. The mapping is performed by executing <code>Chain</code>
41   * <code>Globals.FIND_BUILDER_COMMAND_NAME</code>
42   * in <code>Globals.BUILDER_CATALOG_NAME</code> defined in
43   * <code>Globals.BUILDER_RESOURCE_NAME</code>.
44   * </p>
45   */
46  public final class BuilderFactory {
47  
48      /***
49       * <p>
50       * Common Logger utility.
51       * </p>
52       */
53      private static Log log;
54      static {
55          log = LogFactory.getLog(BuilderFactory.class);
56      }
57  
58      /***
59       * <p>Hidden constructor for utility class.</p>
60       */
61      private BuilderFactory() {
62         super();
63      }
64  
65      /***
66       * <p>
67       * Returns catalog <code>Globals.BUILDER_CATALOG_NAME</code> from file
68       * <code>Globals.BUILDER_RESOURCE_NAME</code>.
69       * </p>
70       *
71       * @return commons chains catalog
72       * @exception Exception finding catalog
73       */
74      protected static Catalog getCatalog() throws Exception {
75  
76          Catalog catalog = CatalogFactory.getInstance().getCatalog(Globals.BUILDER_CATALOG_NAME);
77          if (catalog == null) {
78  
79              ConfigParser parser = new ConfigParser();
80              URL url = parser.getClass().getClassLoader().getResource(Globals.BUILDER_RESOURCE_NAME);
81              if (url == null) {
82                  throw new IllegalArgumentException(Globals.BUILDER_RESOURCE_NAME);
83              }
84              parser.parse(url);
85  
86              catalog = CatalogFactory.getInstance().getCatalog(Globals.BUILDER_CATALOG_NAME);
87  
88          }
89  
90          return catalog;
91  
92      }
93  
94      /***
95       * <p>
96       * Returns a {@link Builder} mapped to a {@link Node} by executing chain
97       * command <code>Globals.FIND_BUILDER_COMMAND_NAME</code> The
98       * {@link BuilderRuleContext} is passed to the chain.
99       * </p>
100      *
101      * @param node the markup node
102      * @return builder that handles the node
103      */
104     public static Builder getRenderer(Node node) {
105 
106         BuilderRuleContext context = new BuilderRuleContext();
107         context.setNode(node);
108         try {
109             Catalog catalog = getCatalog();
110 
111             Command command = null;
112             if (node.getQname() == null) {
113                 command = catalog.getCommand(Globals.FIND_DEFAULT_BUILDER_COMMAND_NAME);
114             } else {
115                 String prefix = node.getQname();
116                 String uri = node.getNamespaceURI(prefix);
117                 if (uri != null) {
118                     command = catalog.getCommand(uri);
119                     if (command == null) {
120                         FacesContext facesContext = FacesContext.getCurrentInstance();
121                         if (facesContext != null) {
122                             facesContext.getExternalContext().getRequestMap()
123                             .put(Globals.CLAY_CUSTOM_BUILDER_XMLNS, uri);
124                         }
125 
126                         command = catalog.getCommand(Globals.FIND_UNKNOWN_BUILDER_COMMAND_NAME);
127                     }
128                 } else {
129                     command = catalog.getCommand(Globals.FIND_UNKNOWN_BUILDER_COMMAND_NAME);
130                 }
131 
132             }
133 
134             command.execute(context);
135 
136         } catch (Exception e) {
137             log.error(e);
138             throw new RuntimeException(e);
139         }
140 
141         return context.getBuilder();
142     }
143 }