View Javadoc

1   package migratool.definition.parser;
2   
3   import java.io.IOException;
4   import java.io.Reader;
5   
6   import migratool.model.BDdescription;
7   import migratool.model.BDdescriptionFactoryIF;
8   import migratool.model.SuperBeanFactory;
9   
10  import org.jdom.Attribute;
11  import org.jdom.Document;
12  import org.jdom.Element;
13  import org.jdom.JDOMException;
14  import org.jdom.input.SAXBuilder;
15  
16  
17  /**
18   * 
19   * MIGRATOOL Program to migrate spatial databsets.
20   * Copyright (C) 2007 Fábio Luiz Leite Júnior
21   * Universidade Federal de Campina Grande
22   * contact: fabioleite@gmail.com
23   *
24   * This program is free software; you can redistribute it and/or
25   * modify it under the terms of the GNU General Public License
26   * as published by the Free Software Foundation; either version 2
27   * of the License, or (at your option) any later version.
28   *
29   * This program is distributed in the hope that it will be useful,
30   * but WITHOUT ANY WARRANTY; without even the implied warranty of
31   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32   * GNU General Public License for more details.
33   *
34   * You should have received a copy of the GNU General Public License
35   * along with this program; if not, write to the Free Software
36   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
37   *
38   * @author fabio
39   *
40   * Classe que encapsula as várias definições de fonte de dados origem e destino
41   */
42   
43  public class MigParser {
44  	
45  	/**
46  	 * L� as defini��es de fontes de dados e instancia as defini��es(beans).
47  	 * @param reader Reader utilizado para ler o arquivo da defini��o de mapa
48  	 * @return defini��o de mapa contida no arquivo
49  	 */	
50  	public MigBean getMigDefinition(Reader reader) throws ParserException{
51  		//Criando os elementos XML
52  		SAXBuilder builder = new SAXBuilder();
53  		Document docInput = null;
54  		try {
55  			try {
56  				docInput = builder.build(reader);
57  			} catch (IOException e) {
58  				// TODO Auto-generated catch block
59  				e.printStackTrace();
60  			}
61  		} catch (JDOMException e) {
62  
63  			throw new ParserException("Problemas com o parser: ",
64  										e.getCause());
65  		}
66  	
67  		return createMigBean(docInput.getRootElement());
68  	}
69  
70  	/**
71  	 * @param element
72  	 */
73  	private MigBean createMigBean(Element root) {
74  		
75  		Element migEl = root;
76  		MigBean mig = new MigBean();
77  		
78  		mig.setDataSourceFactoryType((migEl.getAttribute("dataSourceFactoryType")).getValue());
79  		
80  		Element BDdefinitionSourceEl =  migEl.getChild("fromDataSource");
81  		mig.setSource(createBDdefinitionSource(BDdefinitionSourceEl));
82  		
83  		Element BDdefinitionDestinationEl =  migEl.getChild("destinationDataSource");
84  		mig.setDestination(createBDdefinitionDestination(BDdefinitionDestinationEl));
85  		
86  		return mig;
87  		
88  	}
89  	
90  	/**
91  	 * Cria uma defini��o de banco de dados destino a partir dos dados do xml
92  	 * @param BDdefinitionDestinationEl
93  	 * @return
94  	 */
95  	private BDdescription createBDdefinitionDestination(Element BDdefinitionDestinationEl) {
96  		//	pensar nessa cria��o de objetos se vale apena as factories, pois as descri��es mudam
97  		BDdescriptionFactoryIF beanFactory = 
98  			SuperBeanFactory.getInstance(
99  					BDdefinitionDestinationEl.getAttribute("beanFactory").getValue());
100 		BDdescription bDescription = beanFactory.getBDdescription();
101 		
102 		bDescription.setName(BDdefinitionDestinationEl.getAttribute("name").getValue());
103 		bDescription.setBeanFactory(BDdefinitionDestinationEl.getAttribute("beanFactory").getValue());
104 					
105 		Element BDdefinitionSourceLoactionEl = BDdefinitionDestinationEl.getChild("location");
106 		
107 		Attribute user = BDdefinitionSourceLoactionEl.getAttribute("user");
108 		if (user != null){
109 			bDescription.setUser(user.getValue());
110 		}
111 		
112 		Attribute password = BDdefinitionSourceLoactionEl.getAttribute("password");
113 		if (password != null){
114 			bDescription.setPassword(password.getValue());
115 		}
116 		
117 		Attribute url = BDdefinitionSourceLoactionEl.getAttribute("url");
118 		if (url != null){
119 			bDescription.setUrl(url.getValue());
120 		}
121 		
122 		Attribute driver = BDdefinitionSourceLoactionEl.getAttribute("driver");
123 		if (driver!= null){
124 			bDescription.setDriver(driver.getValue());
125 		}
126 
127 		bDescription.setTableName(BDdefinitionSourceLoactionEl.getAttribute("tableName").getValue());
128 		
129 		Attribute positionGeometryFieldAT = BDdefinitionSourceLoactionEl.getAttribute("positionGeometryField");
130 		if(positionGeometryFieldAT != null){
131 			String positionGeometryField = positionGeometryFieldAT.getValue();
132 			if(positionGeometryField != null){
133 				bDescription.setPositionGeometryField(positionGeometryField);
134 			}
135 		}	
136 		return bDescription;
137 	}
138 
139 	/**
140 	 * Cria uma defini��o de banco de dados fonte a partir dos dados do xml
141 	 * @param BDdefinitionSourceEl
142 	 * @return
143 	 */
144 	private BDdescription createBDdefinitionSource(Element BDdefinitionSourceEl){
145 		//	pensar nessa cria��o de objetos se vale apena as factories, pois as descri��es mudam
146 		BDdescriptionFactoryIF beanFactory = SuperBeanFactory.getInstance(BDdefinitionSourceEl.getAttribute("beanFactory").getValue());
147 		BDdescription bDescription = beanFactory.getBDdescription();
148 		
149 		bDescription.setName(BDdefinitionSourceEl.getAttribute("name").getValue());
150 		bDescription.setBeanFactory(BDdefinitionSourceEl.getAttribute("beanFactory").getValue());
151 					
152 		Element BDdefinitionSourceLoactionEl = BDdefinitionSourceEl.getChild("location");
153 		bDescription.setUser(BDdefinitionSourceLoactionEl.getAttribute("user").getValue());
154 		bDescription.setPassword(BDdefinitionSourceLoactionEl.getAttribute("password").getValue());	
155 		bDescription.setUrl(BDdefinitionSourceLoactionEl.getAttribute("url").getValue());
156 		bDescription.setDriver(BDdefinitionSourceLoactionEl.getAttribute("driver").getValue());
157 		bDescription.setTableName(BDdefinitionSourceLoactionEl.getAttribute("tableName").getValue());
158 				
159 		return bDescription;
160 	}
161 
162 
163 }