4ecaad4a35d633679620f791426242f868784b35
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / SingleRouterConfig.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
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.wamblee.xmlrouter.impl;
17
18 import org.wamblee.xmlrouter.common.Id;
19 import org.wamblee.xmlrouter.config.Config;
20 import org.wamblee.xmlrouter.config.DocumentType;
21 import org.wamblee.xmlrouter.config.Filter;
22 import org.wamblee.xmlrouter.config.RouterConfig;
23 import org.wamblee.xmlrouter.config.Transformation;
24
25 // TODO implement copying of routerconfig. 
26
27 /**
28  * Represents a single configuration set of a single configuration client of the
29  * XML router.
30  * 
31  * @author Erik Brakkee
32  */
33 public class SingleRouterConfig implements ExtendedRouterConfig {
34
35     public static final class DocumentConfig extends ConfigImpl<DocumentType> {
36         public DocumentConfig(Id<Config> aId) {
37             super(DocumentType.class, aId);
38         }
39
40         public DocumentConfig(DocumentConfig aConfig) {
41             super(aConfig);
42         }
43
44         @Override
45         public DocumentType wrap(String aPrefix, DocumentType aT) {
46             return new RobustDocumentType("", aT);
47         }
48     }
49
50     public static final class TransformationConfig extends
51         ConfigImpl<Transformation> {
52         public TransformationConfig(Id<Config> aId) {
53             super(Transformation.class, aId);
54         }
55
56         public TransformationConfig(TransformationConfig aConfig) {
57             super(aConfig);
58         }
59
60         @Override
61         public Transformation wrap(String aPrefix,
62             Transformation aTransformation) {
63             return new RobustTransformation("", aTransformation);
64         }
65     }
66
67     public static final class FilterConfig extends ConfigImpl<Filter> {
68         public FilterConfig(Id<Config> aId) {
69             super(Filter.class, aId);
70         }
71
72         public FilterConfig(FilterConfig aConfig) {
73             super(aConfig);
74         }
75
76         @Override
77         public Filter wrap(String aPrefix, Filter aFilter) {
78             return new RobustFilter("", aFilter);
79         }
80     }
81
82     private Id<RouterConfig> id;
83
84     private DocumentConfig documentTypes;
85     private TransformationConfig transformations;
86     private FilterConfig filters;
87
88     /**
89      * Constructs a router configuration.
90      * 
91      * @param aId
92      *            Unique id for this configuration.
93      */
94     public SingleRouterConfig(Id<RouterConfig> aId) {
95         id = aId;
96         documentTypes = new DocumentConfig(new Id<Config>(aId.getId() +
97             ".documenttypes"));
98         transformations = new TransformationConfig(new Id<Config>(aId.getId() +
99             ".transformations"));
100         filters = new FilterConfig(new Id<Config>(aId.getId() + ".filters"));
101     }
102
103     public SingleRouterConfig(SingleRouterConfig aConfig) {
104         id = aConfig.id;
105         documentTypes = new DocumentConfig(aConfig.documentTypes);
106         transformations = new TransformationConfig(aConfig.transformations);
107         filters = new FilterConfig(aConfig.filters);
108     }
109
110     @Override
111     public Id<RouterConfig> getId() {
112         return id;
113     }
114
115     @Override
116     public Config<DocumentType> documentTypeConfig() {
117         return documentTypes;
118     }
119
120     @Override
121     public Config<Transformation> transformationConfig() {
122         return transformations;
123     }
124
125     @Override
126     public Config<Filter> filterConfig() {
127         return filters;
128     }
129
130     @Override
131     public boolean equals(Object aObj) {
132         if (aObj == null) {
133             return false;
134         }
135         if (!(aObj instanceof SingleRouterConfig)) {
136             return false;
137         }
138         SingleRouterConfig obj = (SingleRouterConfig) aObj;
139
140         return documentTypes.equals(obj.documentTypes) &&
141             transformations.equals(obj.transformations) &&
142             filters.equals(obj.filters);
143     }
144
145     @Override
146     public int hashCode() {
147         return documentTypes.hashCode() + transformations.hashCode() +
148             filters.hashCode();
149     }
150 }