1 package com.bonevich.erj.xml;
2
3 import com.bonevich.erj.app.Project;
4 import com.bonevich.erj.app.ProjectComponent;
5 import com.bonevich.erj.app.ProjectProperties;
6 import com.bonevich.util.ResourceUtilities;
7
8 import org.apache.commons.digester.AbstractObjectCreationFactory;
9 import org.apache.commons.digester.Digester;
10 import org.xml.sax.Attributes;
11 import org.xml.sax.InputSource;
12
13 import java.io.IOException;
14
15 public class ProjectPropertiesDigester
16 {
17 //////////////////////////////////////////////////////////
18 // Constants
19 private static final String NAME_STR = "name";
20 private static final String AUTHOR_STR = "author";
21 private static final String VERSION_STR = "version";
22 private static final String DESC_STR = "description";
23
24 private static final String DB_TARGET_STR = "target-database";
25 private static final String DB_NAME_STR = "database-name";
26 private static final String DB_TYPE_STR = "database-type";
27 private static final String DB_DRIVER_STR = "driver";
28 private static final String DB_JAR_STR = "jar";
29 private static final String DB_URL_STR = "url";
30 private static final String LOGIN_PROPS_STR = "login-fields";
31 private static final String LOGIN_PROP_STR = "login-field";
32 private static final String LOGIN_PROP_NAME_STR = "name";
33 private static final String LOGIN_PROP_VALUE_STR = "value";
34
35 private static final String CMP_STR = "component";
36 private static final String TYPE_STR = "type";
37 private static final String FILENAME_STR = "filename";
38
39 private static final String SYSTEM_ID = "com/bonevich/erj/xml/dtd/erj-project.dtd";
40 private static final String PUBLIC_ID = "-//Jeffrey Bonevich//DTD ERJ Project Properties 1.0//EN";
41
42 //////////////////////////////////////////////////////////
43 // Attributes
44 private Project _project;
45 private Digester _digester;
46 private String _dtdFile = ResourceUtilities.getResourceAsURL(SYSTEM_ID).toString();
47
48 //////////////////////////////////////////////////////////
49 // Constructors
50 public ProjectPropertiesDigester(Project project)
51 {
52 _project = project;
53 init();
54 }
55
56 private void init()
57 {
58 _digester = new Digester();
59 _digester.register(PUBLIC_ID, _dtdFile);
60 _digester.setValidating(false);
61 _digester.push(_project);
62
63 _digester.addCallMethod("erj-project/project-properties/name", "setName", 0);
64 _digester.addCallMethod("erj-project/project-properties/author", "setAuthor", 0);
65 _digester.addCallMethod("erj-project/project-properties/version", "setVersion", 0);
66 _digester.addCallMethod("erj-project/project-properties/description", "setDescription", 0);
67
68 _digester.addFactoryCreate(
69 "erj-project/target-database",
70 new AbstractObjectCreationFactory()
71 {
72 public Object createObject(Attributes attributes)
73 {
74 return _project.createDatabaseLogin(attributes.getValue(TYPE_STR));
75 }
76 }
77 );
78 _digester.addCallMethod("erj-project/target-database/name", "setDatabaseName", 0);
79 _digester.addCallMethod("erj-project/target-database/driver", "setDriver", 0);
80 _digester.addCallMethod("erj-project/target-database/jar", "setJarPath", 0);
81 _digester.addCallMethod("erj-project/target-database/url", "setJdbcUrl", 0);
82
83 _digester.addCallMethod("erj-project/target-database/login-fields/login-field", "setLoginProperty", 2);
84 _digester.addCallParam("erj-project/target-database/login-fields/login-field", 0, "name");
85 _digester.addCallParam("erj-project/target-database/login-fields/login-field", 1, "value");
86
87 _digester.addFactoryCreate(
88 "erj-project/component",
89 new AbstractObjectCreationFactory()
90 {
91 public Object createObject(Attributes attributes)
92 {
93 String type = attributes.getValue(TYPE_STR);
94 ProjectComponent cmp = null;
95 // we need to skip the properties entry itself
96 if (!ProjectComponent.ERJ.equals(type))
97 {
98 cmp = _project.createComponent(type);
99 cmp.setEntryFilename(attributes.getValue(FILENAME_STR));
100 }
101 else
102 {
103 // this one actually gets tossed
104 cmp = new ProjectProperties(_project);
105 }
106 return cmp;
107 }
108 }
109 );
110 }
111
112 public void digest(InputSource src) throws IOException
113 {
114 System.out.println("======================================================");
115 System.out.println("== READING PROJECT PROPERTIES: " + src.getSystemId());
116 System.out.println();
117
118 try
119 {
120 _digester.parse(src);
121 }
122 catch (Exception e)
123 {
124 e.printStackTrace();
125 throw new IOException("Failed to load project properties file: " + e.getMessage());
126 }
127 }
128
129 } /* end class ProjectProperitesDigester */
This page was automatically generated by Maven