KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > generator > jdbc > JdbcSchemaSG


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
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  
17 package org.apache.ws.jaxme.pm.generator.jdbc;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.ws.jaxme.generator.sg.Context;
23 import org.apache.ws.jaxme.generator.sg.ObjectSG;
24 import org.apache.ws.jaxme.generator.sg.SchemaSG;
25 import org.apache.ws.jaxme.generator.sg.SchemaSGChain;
26 import org.apache.ws.jaxme.generator.sg.TypeSG;
27 import org.apache.ws.jaxme.generator.sg.impl.SchemaSGChainImpl;
28 import org.apache.ws.jaxme.impl.JAXBContextImpl;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
36  */

37 public class JdbcSchemaSG extends SchemaSGChainImpl {
38   final JaxMeJdbcSG jdbcSG;
39
40   /** <p>Creates a new instance of JdbcSchemaSG.</p>
41    */

42   protected JdbcSchemaSG(JaxMeJdbcSG pJdbcSG, SchemaSGChain pBackingObject) {
43     super(pBackingObject);
44     jdbcSG = pJdbcSG;
45   }
46
47   private Element JavaDoc createProperty(Element JavaDoc pParent, String JavaDoc pName, String JavaDoc pValue) {
48     Element JavaDoc element = pParent.getOwnerDocument().createElementNS(JAXBContextImpl.CONFIGURATION_URI, "Property");
49     pParent.appendChild(element);
50     element.setAttributeNS(null, "name", pName);
51     element.setAttributeNS(null, "value", pValue);
52     return element;
53   }
54
55   public Document JavaDoc getConfigFile(SchemaSG pController, String JavaDoc pPackageName, List JavaDoc pContextList)
56       throws SAXException JavaDoc {
57     final String JavaDoc uri = JAXBContextImpl.CONFIGURATION_URI;
58     final Document JavaDoc doc = super.getConfigFile(pController, pPackageName, pContextList);
59     final Element JavaDoc root = doc.getDocumentElement();
60     final Iterator JavaDoc iter = pContextList.iterator();
61     for (Node JavaDoc node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
62       if (node.getNodeType() == Node.ELEMENT_NODE
63           && JAXBContextImpl.CONFIGURATION_URI.equals(node.getNamespaceURI())
64           && "Manager".equals(node.getLocalName())) {
65         final ObjectSG objectSG = (ObjectSG) iter.next();
66         final TypeSG typeSG = objectSG.getTypeSG();
67         if (typeSG.isComplex()) {
68           final CustomTableData customTableData = (CustomTableData) typeSG.getProperty(jdbcSG.getKey());
69           if (customTableData != null) {
70             Element JavaDoc manager = (Element JavaDoc) node;
71             final Context ctx = typeSG.getComplexTypeSG().getClassContext();
72             manager.setAttributeNS(uri, "pmClass", ctx.getPMName().toString());
73
74             final TableDetails tableDetails = customTableData.getTableDetails();
75             final String JavaDoc driver = tableDetails.getDriver();
76             if (driver != null) {
77               createProperty(manager, "jdbc.driver", driver);
78             }
79             final String JavaDoc url = tableDetails.getUrl();
80             if (url != null) {
81               createProperty(manager, "jdbc.url", url);
82             }
83             final String JavaDoc user = tableDetails.getUser();
84             if (user != null) {
85               createProperty(manager, "jdbc.user", user);
86             }
87             final String JavaDoc password = tableDetails.getPassword();
88             if (password != null) {
89               createProperty(manager, "jdbc.password", password);
90             }
91           }
92         }
93       }
94     }
95     if (iter.hasNext()) {
96       throw new IllegalStateException JavaDoc("More managers expected than found");
97     }
98     return doc;
99   }
100 }
101
Popular Tags