001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/crs/CRSFactory.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.model.crs;
044    
045    import java.net.URI;
046    
047    import org.deegree.i18n.Messages;
048    import org.deegree.model.csct.cs.ConvenienceCSFactory;
049    
050    /**
051     * 
052     * 
053     *
054     * @version $Revision: 6259 $
055     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
056     * @author last edited by: $Author: bezema $
057     *
058     * @version 1.0. $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
059     *
060     * @since 2.0
061     */
062    public class CRSFactory {
063    
064        /**
065         * 
066         * @param prefix must be not null
067         * @param localName
068         * @param namespace
069         * @return a CoordinateSystem corresponding to the given name, prefix and namespace
070         * @throws UnknownCRSException 
071         */
072        public static CoordinateSystem create( String prefix, String localName, URI namespace )
073                                throws UnknownCRSException {
074            if ( ConvenienceCSFactory.getInstance().getCSByName( prefix + ':' + localName ) == null ) {
075                throw new UnknownCRSException( Messages.getMessage( "CRS_UNKNOWNCRS" ) );
076            }
077            return new CoordinateSystem( prefix, localName, namespace );
078        }
079    
080        /**
081         * 
082         * @param name
083         * @param namespace
084         * @return a CoordinateSystem corresponding to the given name and namespace
085         * @throws UnknownCRSException 
086         */
087        public static CoordinateSystem create( String name, URI namespace )
088                                throws UnknownCRSException {
089            name = normalizeName( name );
090            if ( ConvenienceCSFactory.getInstance().getCSByName( name ) == null ) {
091                throw new UnknownCRSException( Messages.getMessage( "CRS_UNKNOWNCRS" ) );
092            }
093            return new CoordinateSystem( name, namespace );
094        }
095    
096        /**
097         * e.g. epgs:4326
098         * @param name
099         * @return a CoordinateSystem corresponding to the given name 
100         * @throws UnknownCRSException if the crs-name is not known
101         */
102        public static CoordinateSystem create( String name )
103                                throws UnknownCRSException {
104            name = normalizeName( name );
105            if ( ConvenienceCSFactory.getInstance().getCSByName( name ) == null ) {
106                throw new UnknownCRSException( Messages.getMessage( "CRS_UNKNOWNCRS", name ) );
107            }
108            return new CoordinateSystem( name );
109        }
110        
111        private static String normalizeName(String name ) {
112            if ( name.startsWith( "http://www.opengis.net/gml/srs/" ) ) {
113                // as declared in the GML 2.1.1 specification
114                // http://www.opengis.net/gml/srs/epsg.xml#4326
115                int p = name.lastIndexOf( "/" );
116    
117                if ( p >= 0 ) {
118                    name = name.substring( p, name.length() );
119                    p = name.indexOf( "." );
120    
121                    String s1 = name.substring( 1, p ).toUpperCase();
122                    p = name.indexOf( "#" );
123    
124                    String s2 = name.substring( p + 1, name.length() );
125                    if ( s2.toUpperCase().startsWith( "EPSG" ) ) {
126                        name = s2;
127                    } else {
128                        name = s1 + ":" + s2;
129                    }
130                }
131            }
132            return name;
133        }
134    
135    }
136    /* ********************************************************************
137     Changes to this class. What the people have been up to:
138     $Log$
139     Revision 1.4  2006/11/27 15:36:08  bezema
140     fixed javadocs
141    
142     Revision 1.3  2006/11/27 09:07:51  poth
143     JNI integration of proj4 has been removed. The CRS functionality now will be done by native deegree code.
144    
145     Revision 1.2  2006/07/12 14:46:14  poth
146     comment footer added
147    
148     ********************************************************************** */