KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > IncludeEl


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.configuration;
17
18 import scriptella.spi.Resource;
19 import scriptella.util.IOUtils;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.nio.charset.Charset JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29
30 /**
31  * TODO: Add documentation
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 public class IncludeEl extends XmlConfigurableBase implements Resource {
37     private URL JavaDoc url;
38     private String JavaDoc href;
39     private String JavaDoc charset;
40     private static final Logger JavaDoc LOG = Logger.getLogger(IncludeEl.class.getName());
41     private FallbackEl fallbackEl;
42
43     public IncludeEl(XmlElement element) {
44         configure(element);
45     }
46
47     public String JavaDoc getHref() {
48         return href;
49     }
50
51     public void setHref(final String JavaDoc href) {
52         this.href = href;
53     }
54
55     public String JavaDoc getCharset() {
56         return charset;
57     }
58
59     public void setCharset(final String JavaDoc charset) {
60         this.charset = charset;
61     }
62
63     public FallbackEl getFallbackEl() {
64         return fallbackEl;
65     }
66
67     public void setFallbackEl(FallbackEl fallbackEl) {
68         this.fallbackEl = fallbackEl;
69     }
70
71     public void configure(final XmlElement element) {
72         url = element.getDocumentUrl();
73         setRequiredProperty(element, "href");
74
75         String JavaDoc enc = element.getAttribute("encoding");
76         if (enc != null && !Charset.isSupported(enc)) {
77             throw new ConfigurationException("Encoding " + enc + " is not supported", element);
78         }
79         charset = enc;
80         final XmlElement fallbackElement = element.getChild("fallback");
81         if (fallbackElement != null) {
82             fallbackEl = new FallbackEl(fallbackElement);
83         }
84     }
85
86     public Reader JavaDoc open() throws IOException JavaDoc {
87         try {
88             URL JavaDoc u = new URL JavaDoc(url, href);
89             return IOUtils.getReader(u.openStream(), charset);
90         } catch (MalformedURLException JavaDoc e) {
91             throw (IOException JavaDoc) new IOException JavaDoc("Malformed include url: " + href).initCause(e);
92         } catch (IOException JavaDoc e) {
93             if (fallbackEl != null) {
94                 LOG.log(Level.FINE, e.getMessage());
95                 return fallbackEl.open();
96             } else {
97                 throw e;
98             }
99         }
100     }
101 }
102
Popular Tags