KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > tree > builder > SourceTreeModelDefinitionBuilder


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel.tree.builder;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.service.ServiceException;
20 import org.apache.avalon.framework.service.ServiceManager;
21 import org.apache.avalon.framework.service.Serviceable;
22 import org.apache.cocoon.forms.FormsConstants;
23 import org.apache.cocoon.forms.formmodel.tree.SourceTreeModelDefinition;
24 import org.apache.cocoon.forms.formmodel.tree.TreeModelDefinition;
25 import org.apache.cocoon.forms.util.DomHelper;
26 import org.apache.cocoon.matching.helpers.WildcardHelper;
27 import org.apache.excalibur.source.SourceResolver;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Builds a {@link org.apache.cocoon.forms.formmodel.tree.SourceTreeModel}.
32  *
33  * @version $Id: SourceTreeModelDefinitionBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
34  */

35 public class SourceTreeModelDefinitionBuilder extends AbstractLogEnabled
36     implements Serviceable, TreeModelDefinitionBuilder {
37
38     private ServiceManager manager;
39
40     public void service(ServiceManager manager) throws ServiceException {
41         this.manager = manager;
42     }
43
44     public TreeModelDefinition build(Element JavaDoc modelElt) throws Exception JavaDoc {
45         
46         SourceTreeModelDefinition definition = new SourceTreeModelDefinition();
47         
48         definition.setURL(DomHelper.getAttribute(modelElt, "src"));
49         
50         Element JavaDoc fileSet = DomHelper.getChildElement(modelElt, FormsConstants.DEFINITION_NS, "fileset");
51         if (fileSet != null) {
52             definition.setFilePatterns(getPatterns(fileSet, "include"),
53                     getPatterns(fileSet, "exclude"));
54         }
55         
56         Element JavaDoc dirSet = DomHelper.getChildElement(modelElt, FormsConstants.DEFINITION_NS, "dirset");
57         if (dirSet != null) {
58             definition.setDirectoryPatterns(getPatterns(dirSet, "include"),
59                     getPatterns(dirSet, "exclude"));
60         }
61         
62         definition.setSourceResolver((SourceResolver)this.manager.lookup(SourceResolver.ROLE));
63         
64         return definition;
65     }
66     
67     int[][] getPatterns(Element JavaDoc parent, String JavaDoc name) throws Exception JavaDoc {
68         Element JavaDoc[] children = DomHelper.getChildElements(parent, FormsConstants.DEFINITION_NS, name);
69
70         if (children.length == 0) {
71             return null;
72         }
73
74         int[][] result = new int[children.length][];
75         for (int i = 0; i < children.length; i++) {
76             String JavaDoc pattern = DomHelper.getAttribute(children[i], "pattern");
77             result[i] = WildcardHelper.compilePattern(pattern);
78         }
79         return result;
80     }
81
82 }
83
Popular Tags