Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / TopologyRuleFactory.java @ 18995

History | View | Annotate | Download (4.31 KB)

1
/*
2
 * Created on 10-abr-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: 
47
 * $Log: 
48
 */
49
package org.gvsig.topology;
50

    
51
import java.util.Map;
52

    
53
import org.apache.log4j.Logger;
54

    
55
import com.iver.cit.gvsig.fmap.Messages;
56
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
57
import com.iver.utiles.NotExistInXMLEntity;
58
import com.iver.utiles.XMLEntity;
59

    
60
/**
61
 * Factory to create ITopologyRule implementations.
62
 * 
63
 * @author Alvaro Zabala
64
 * 
65
 */
66
public class TopologyRuleFactory {
67

    
68
        private static Logger logger = Logger.getLogger(TopologyRuleFactory.class
69
                        .getName());
70

    
71
        public static ITopologyRule createFromXML(Topology ownerOfRule,
72
                        XMLEntity xml) {
73
                String className = null;
74
                try {
75
                        className = xml.getStringProperty("className");
76
                } catch (NotExistInXMLEntity e) {
77
                        logger.error("Class name not set.\n"
78
                                        + " Maybe you forgot to add the"
79
                                        + " putProperty(\"className\", yourClassName)"
80
                                        + " call in the getXMLEntity method of your class", e);
81
                        return null;
82
                }
83
                Class clazz = null;
84
                IOneLyrRule obj = null;
85
                String s = className;
86
                try {
87
                        clazz = Class.forName(className);
88
                        if (xml.contains("desc")) {
89
                                s += " \"" + xml.getStringProperty("desc") + "\"";
90
                        }
91
                        obj = (IOneLyrRule) clazz.newInstance();
92
                        logger.info(Messages.getString("creating") + "....... " + s);
93
                        try {
94
                                obj.setTopology(ownerOfRule);
95
                                obj.setXMLEntity(xml);
96
                                
97
                                if(obj.getOriginLyr() == null)
98
                                        throw new TopologyRuleDefinitionException("Regla topologica sin capa de origen");
99
                                
100
                                if(obj instanceof ITwoLyrRule){
101
                                        if(((ITwoLyrRule)obj).getDestinationLyr() == null){
102
                                                throw new TopologyRuleDefinitionException("Regla que aplica a dos capas no ha especificado la segunda capa");
103
                                        }
104
                                }
105
                                
106
                        } catch (NotExistInXMLEntity neiXML) {
107
                                logger.error(Messages.getString("failed_creating_object")
108
                                                + ": " + s);
109
                                throw neiXML;
110
                        }
111
                } catch (Exception e) {
112
                        logger.error(e);
113
                }
114
                return (ITopologyRule) obj;
115
        }
116

    
117
        public static ITopologyRule createRule(Class<?> ruleClass, 
118
                                                                                Map<String, Object> params, 
119
                                                                        Topology ruleOwner) throws TopologyRuleDefinitionException{
120
                
121
                try{        
122
                        ITopologyRule rule = (ITopologyRule) ruleClass.newInstance();
123
                        FLyrVect originLyr = (FLyrVect) params.get("originLyr");
124
                        if(originLyr == null)
125
                                throw new TopologyRuleDefinitionException("Regla topologica sin capa de origen");
126
                        ((IOneLyrRule)rule).setOriginLyr(originLyr);
127
                        if (ruleClass.isAssignableFrom(ITwoLyrRule.class)){
128
                                FLyrVect destinationLyr = (FLyrVect) params.get("destinationLyr");
129
                                if(destinationLyr == null)
130
                                        throw new TopologyRuleDefinitionException("Regla que aplica a dos capas no ha especificado la segunda capa");
131
                                ((ITwoLyrRule)rule).setDestinationLyr(destinationLyr);
132
                        }        
133
                        return rule;
134
                } catch (InstantiationException e) {
135
                        throw new TopologyRuleDefinitionException(
136
                                        "Error construyendo la regla topologica", e);
137
                } catch (IllegalAccessException e) {
138
                        throw new TopologyRuleDefinitionException(
139
                                        "Error construyendo la regla topologica", e);
140
                }//catch
141
        }
142
}