KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > sourcetype > SourceType


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 package org.apache.forrest.sourcetype;
18
19 import org.apache.avalon.framework.configuration.*;
20
21 import java.util.*;
22
23 /**
24  * Represents a sourcetype. A sourcetype has a name and a number of rules
25  * which are used to determine if a certain document is of this sourcetype.
26  */

27 public class SourceType implements Configurable
28 {
29     protected List rules = new ArrayList();
30     protected String JavaDoc name;
31
32     public void configure(Configuration configuration) throws ConfigurationException
33     {
34         name = configuration.getAttribute("name");
35
36         Configuration[] ruleConfs = configuration.getChildren();
37         for (int i = 0; i < ruleConfs.length; i++)
38         {
39             SourceTypeRule rule;
40             if (ruleConfs[i].getName().equals("document-declaration"))
41                 rule = new DocDeclRule();
42             else if (ruleConfs[i].getName().equals("processing-instruction"))
43                 rule = new ProcessingInstructionRule();
44             else if (ruleConfs[i].getName().equals("w3c-xml-schema"))
45                 rule = new ProcessingInstructionRule();
46             else if (ruleConfs[i].getName().equals("document-element"))
47                 rule = new DocumentElementRule();
48             else
49                 throw new ConfigurationException("Unsupported element " + ruleConfs[i].getName() + " at "
50                         + ruleConfs[i].getLocation());
51
52             rule.configure(ruleConfs[i]);
53             rules.add(rule);
54         }
55     }
56
57     public boolean matches(SourceInfo sourceInfo)
58     {
59         Iterator rulesIt = rules.iterator();
60         boolean matches = true;
61         while (rulesIt.hasNext())
62         {
63             SourceTypeRule rule = (SourceTypeRule)rulesIt.next();
64             matches = matches && rule.matches(sourceInfo);
65             if (!matches)
66                 return false;
67         }
68         return matches;
69     }
70
71     public String JavaDoc getName()
72     {
73         return name;
74     }
75 }
76
Popular Tags