001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/tools/wms/SLDHarmonizer.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005     Department of Geography, University of Bonn
006     and
007     lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035     ----------------------------------------------------------------------------*/
036    
037    package org.deegree.tools.wms;
038    
039    import static org.deegree.framework.log.LoggerFactory.getLogger;
040    import static org.deegree.framework.xml.XMLTools.getElements;
041    import static org.deegree.framework.xml.XMLTools.getRequiredNodeAsString;
042    import static org.deegree.ogcbase.CommonNamespaces.SLDNS;
043    import static org.deegree.ogcbase.CommonNamespaces.getNamespaceContext;
044    
045    import java.io.File;
046    import java.io.FileNotFoundException;
047    import java.io.FileOutputStream;
048    import java.io.IOException;
049    import java.io.OutputStreamWriter;
050    import java.io.UnsupportedEncodingException;
051    import java.net.MalformedURLException;
052    
053    import javax.xml.transform.TransformerException;
054    
055    import org.deegree.framework.log.ILogger;
056    import org.deegree.framework.xml.NamespaceContext;
057    import org.deegree.framework.xml.XMLFragment;
058    import org.deegree.framework.xml.XMLParsingException;
059    import org.w3c.dom.Element;
060    import org.xml.sax.SAXException;
061    
062    /**
063     * <code>SLDHarmonizer</code>
064     * 
065     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
066     * @author last edited by: $Author: aschmitz $
067     * 
068     * @version $Revision: 18275 $, $Date: 2009-06-29 10:05:47 +0200 (Mo, 29. Jun 2009) $
069     */
070    public class SLDHarmonizer {
071    
072        private static final ILogger LOG = getLogger( SLDHarmonizer.class );
073    
074        private static final NamespaceContext nsContext = getNamespaceContext();
075    
076        private File sldFile;
077    
078        private XMLFragment doc;
079    
080        private SLDHarmonizer( String file ) throws MalformedURLException, IOException {
081            sldFile = new File( file );
082    
083            if ( !sldFile.exists() ) {
084                LOG.logInfo( "SLD file does not exist: " + sldFile );
085                return;
086            }
087    
088            try {
089                doc = new XMLFragment( sldFile );
090            } catch ( SAXException e ) {
091                LOG.logDebug( "Stack trace: ", e );
092                LOG.logInfo( "SLD file cannot be parsed: " + sldFile );
093            }
094        }
095    
096        private void harmonize()
097                                throws UnsupportedEncodingException, FileNotFoundException, TransformerException,
098                                XMLParsingException {
099            if ( doc == null ) {
100                return;
101            }
102    
103            Element root = doc.getRootElement();
104            root.setAttribute( "xmlns:app", "http://www.deegree.org/app" );
105    
106            for ( Element e : getElements( root, "//sld:NamedLayer/sld:UserStyle[count(sld:Name) = 0]", nsContext ) ) {
107                String name = "default:" + getRequiredNodeAsString( e, "../sld:Name", nsContext );
108                Element elem = root.getOwnerDocument().createElementNS( SLDNS.toASCIIString(), "Name" );
109                elem.setTextContent( name );
110                e.insertBefore( elem, e.getChildNodes().item( 0 ) );
111            }
112    
113            for ( Element e : getElements( root, "//ogc:PropertyName", nsContext ) ) {
114                String name = e.getTextContent().toLowerCase();
115                name = name.startsWith( "app:" ) ? name : "app:" + name;
116                e.setTextContent( name );
117            }
118    
119            for ( Element e : getElements( root, "//sld:CssParameter[@name = 'stroke-width' and text() = '-1']", nsContext ) ) {
120                e.setTextContent( "1" );
121            }
122    
123            doc.prettyPrint( new OutputStreamWriter( new FileOutputStream( sldFile ), "UTF-8" ) );
124        }
125    
126        /**
127         * @param args
128         */
129        public static void main( String[] args ) {
130            if ( args.length == 0 ) {
131                LOG.logInfo( "Usage:" );
132                LOG.logInfo( "java -cp deegree2.jar org.deegree.tools.wms.SLDHarmonizer <sldfile> [<sldfiles>]" );
133                return;
134            }
135    
136            for ( String file : args ) {
137                try {
138                    new SLDHarmonizer( file ).harmonize();
139                } catch ( UnsupportedEncodingException e ) {
140                    LOG.logError( "Unknown error", e );
141                } catch ( FileNotFoundException e ) {
142                    LOG.logError( "Unknown error", e );
143                } catch ( MalformedURLException e ) {
144                    LOG.logError( "Unknown error", e );
145                } catch ( TransformerException e ) {
146                    LOG.logError( "Unknown error", e );
147                } catch ( XMLParsingException e ) {
148                    LOG.logError( "Unknown error", e );
149                } catch ( IOException e ) {
150                    LOG.logError( "Unknown error", e );
151                }
152            }
153        }
154    
155    }