KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > attribute > PropertyFileAttributeLoader


1 package com.tirsen.nanning.attribute;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 public class PropertyFileAttributeLoader implements AttributesLoader {
9     public static final String JavaDoc ATTRIBUTE_FILE_SUFFIX = ".attributes";
10
11     public void load(ClassAttributes classAttributes) {
12         ClassPropertiesHelper classPropertiesHelper = new ClassPropertiesHelper(classAttributes);
13         classPropertiesHelper.loadAttributes(getProperties(classAttributes.getAttributeClass()));
14     }
15
16     Properties JavaDoc getProperties(Class JavaDoc klass) {
17         Properties JavaDoc properties = new Properties JavaDoc();
18         InputStream JavaDoc inputStream = null;
19         try {
20             String JavaDoc className = klass.getName();
21
22             // load the JavaDoc-tags
23
inputStream = Attributes.findFile(klass, ClassPropertiesHelper.className(klass) + ATTRIBUTE_FILE_SUFFIX);
24             if (inputStream == null) {
25                 inputStream = Attributes.findFile(klass, className.replace('.', '/') + ATTRIBUTE_FILE_SUFFIX);
26             }
27
28             if (inputStream != null) {
29                 properties.load(inputStream);
30             }
31
32         } catch (MalformedURLException JavaDoc e) {
33             throw new AttributeException("Error fetching properties for " + klass, e);
34         } catch (IOException JavaDoc e) {
35             throw new AttributeException("Error fetching properties for " + klass, e);
36         } catch (AttributeException e) {
37             throw e;
38         } catch (Exception JavaDoc e) {
39             throw new AttributeException("Error fetching properties for " + klass, e);
40         } finally {
41             if (inputStream != null) {
42                 try {
43                     inputStream.close();
44                 } catch (IOException JavaDoc e) {
45                     throw new RuntimeException JavaDoc(e);
46                 }
47             }
48         }
49         return properties;
50     }
51
52 }
53
Popular Tags