001    package org.deegree.ogcwebservices.csw.iso_profile;
002    
003    import java.io.BufferedReader;
004    import java.io.IOException;
005    import java.io.InputStream;
006    import java.io.InputStreamReader;
007    import java.util.Properties;
008    
009    import org.deegree.framework.util.IDGenerator;
010    import org.deegree.framework.util.StringTools;
011    import org.deegree.framework.xml.NamespaceContext;
012    import org.deegree.framework.xml.XMLParsingException;
013    import org.deegree.framework.xml.XMLTools;
014    import org.deegree.ogcbase.CommonNamespaces;
015    import org.w3c.dom.Node;
016    
017    /**
018     * 
019     * 
020     * @version $Revision: 1.9 $
021     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
022     * @author last edited by: $Author: buesching $
023     * 
024     * @version 1.0. $Revision: 1.9 $, $Date: 2008-01-25 14:36:09 $
025     * 
026     * @since 2.0
027     */
028    public class Mapping2_0_2 {
029    
030        private static IDGenerator idgen = IDGenerator.getInstance();
031        
032        private static Properties mapping = null;
033    
034        private Properties nsp = null;
035    
036        private static Properties namespaces = new Properties();
037    
038        private static Properties namespacesInverse = new Properties();
039        
040        static {
041            mapping = new Properties();
042            try {
043                InputStream is = Mapping2_0_2.class.getResourceAsStream( "mapping2_0_2.properties" );
044                InputStreamReader isr = new InputStreamReader( is );
045                BufferedReader br = new BufferedReader( isr );
046                String line = null;
047                while ( ( line = br.readLine() ) != null ) {
048                    if ( !line.trim().startsWith( "#" ) ) {
049                        String[] tmp = StringTools.toArray( line.trim(), "=", false );
050                        if ( tmp[0].startsWith( "$namespace." ) ) {
051                            String pre = tmp[0].substring( tmp[0].indexOf( '.' ) + 1, tmp[0].length() );
052                            namespaces.put( pre, tmp[1] );
053                            namespacesInverse.put( tmp[1], pre );
054                        } else {
055                            mapping.put( tmp[0], tmp[1] );
056                        }
057                    }
058                }
059            } catch ( IOException e ) {
060                e.printStackTrace();
061            }
062        }
063    
064        private static NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
065    
066        /**
067         * maps a property name of GetRecords, Delete and Update request from the catalogue schema to
068         * the underlying WFS schema
069         * 
070         * @param node
071         * @return
072         * @throws XMLParsingException
073         */
074        public String mapPropertyValue( Node node, String nspDec )
075                                throws XMLParsingException {
076            if ( nsp == null ) {
077                createNsp( nspDec );
078            }
079    
080            String s = XMLTools.getNodeAsString( node, ".", nsc, null );
081            String prefix = s.substring( 0, s.indexOf( ':' ) );
082            String localPre = namespacesInverse.getProperty( nsp.getProperty( prefix ) );
083    
084            String propertyValue = "./" + localPre + s.substring( s.indexOf( ':' ) );
085            propertyValue = mapping.getProperty( propertyValue );
086    
087            return propertyValue;
088    
089        }
090    
091        private void createNsp( String nspDec ) {
092            nsp = new Properties();
093            String[] tmp = StringTools.toArray( nspDec, ";", false );
094            for ( int i = 0; i < tmp.length; i++ ) {
095                int p = tmp[i].indexOf( ':' );
096                String pre = tmp[i].substring( 0, p );
097                String val = tmp[i].substring( p + 1 );
098                nsp.put( pre, val );
099            }
100    
101        }
102    
103        /**
104         * maps property names for sorting from the catalogue schema to the underlying WFS schema
105         * 
106         * @param node
107         * @return
108         * @throws XMLParsingException
109         */
110        public String mapSortProperty( Node node, String nspDec )
111                                throws XMLParsingException {
112            if ( nsp == null ) {
113                createNsp( nspDec );
114            }
115    
116            String s = XMLTools.getNodeAsString( node, ".", nsc, null );
117            String prefix = s.substring( 0, s.indexOf( ':' ) );
118            String localPre = namespacesInverse.getProperty( nsp.getProperty( prefix ) );
119    
120            String propertyValue = "./sortby/" + localPre + s.substring( s.indexOf( ':' ) );
121            propertyValue = mapping.getProperty( propertyValue );
122    
123            return propertyValue;
124        }
125    
126        public String getLiteralValueIsLike( Node node, String propName, String wildCard )
127                                throws XMLParsingException {
128    
129            String literal = XMLTools.getNodeAsString( node, ".", nsc, null );
130            String propertyName = propName.substring( propName.indexOf( ':' ) + 1 );
131    
132            String newLiteral = literal;
133            if ( propertyName.equals( "subject" ) || propertyName.equals( "AlternateTitle" )
134                 || propertyName.equals( "ResourceIdentifier" ) || propertyName.equals( "ResourceLanguage" )
135                 || propertyName.equals( "AlternateTitle" ) || propertyName.equals( "ResourceIdentifier" )
136                 || propertyName.equals( "GeographicDescripionCode" ) || propertyName.equals( "TopicCategory" ) ) {
137                newLiteral = StringTools.concat( 500, "%|", wildCard, literal, wildCard, "|%" );
138            }
139    
140            return newLiteral;
141        }
142    
143        public String getLiteralValueIsEqualTo( Node node )
144                                throws XMLParsingException {
145            String literal = XMLTools.getNodeAsString( node, ".", nsc, null );
146            return StringTools.concat( 100, "%|", literal, "|%" );
147        }
148        
149        public static String getId(){
150            return "unknown" + Long.toString( idgen.generateUniqueID() );
151        }
152    
153    }