/* * Geotools2 - OpenSource mapping toolkit * http://geotools.org * (C) 2002, Geotools Project Managment Committee (PMC) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * */ package org.geotools.styling; import java.io.IOException; import javax.xml.transform.TransformerException; import org.geotools.filter.FilterTransformer; /** * Exports a style as a OGC SLD document. This class is does not generate * namespace compliant xml, even though it does print gml prefixes. It was * also written before the 1.0 filter spec, so some of it may be not up to * date. * * @author Ian Turton, ccg * * @deprecated Use org.geotools.styling.SLDTransformer * @task HACK: Logging errors, very bad! We need a style visitor exception, or * have visit methods throw illegal filter exceptions, or io exceptions. * @task TODO: Support full header information for new XML file * @task REVISIT: make filter utils class so that other encoders (like sql). It * could also be nice to refactor common code from gml producer, as * there is basically a GeometryProducer there. * @task REVISIT: make namespace aware. * @source $URL$ */ public class XMLEncoder implements org.geotools.styling.StyleVisitor { /** The logger for the filter module. */ private static final java.util.logging.Logger LOGGER = java.util.logging.Logger .getLogger("org.geotools.style"); /** To write the xml representations of filters to */ private java.io.Writer out; /** * Constructor with writer to write filters to. * * @param out where to write the xml representation of filters. */ public XMLEncoder(java.io.Writer out) { this.out = out; LOGGER.fine("creating a Style encoder"); } /** * Creates a new instance of XMLEncoder * * @param out The writer to write to. * @param style the style to encode. * * @throws IOException - if a problem occurs with the writer */ public XMLEncoder(java.io.Writer out, Style style) throws IOException { this.out = out; encode(style); } /** * Encodes the filter to the current writer. * * @param style the filter to encode. * * @throws IOException if there are problems writing to out. */ public void encode(Style style) throws IOException { out.write("\n"); style.accept(this); out.write("\n"); } /** * Encodes the expression to the current writer. * * @param expression the expression to encode. * @throws IOException */ private void encode(org.geotools.filter.Expression expression) throws IOException { if (expression != null) { try { out.write((new FilterTransformer()).transform(expression)); } catch (TransformerException e1) { LOGGER.warning(e1.toString()); } } } public void encodeCssParam(String name, org.geotools.filter.Expression expression) { try { out.write("\n"); encode(expression); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Style style) { try { out.write("\n"); String title = style.getTitle(); String abs = style.getAbstract(); String name = style.getName(); out.write("" + title + "\n"); out.write("" + name + "\n"); out.write("" + abs + "\n"); out.write("\n"); FeatureTypeStyle[] fts = style.getFeatureTypeStyles(); for (int i = 0; i < fts.length; i++) { visit(fts[i]); } out.write("\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(FeatureTypeStyle fts) { try { out.write("\n"); out.write(""); out.write(fts.getFeatureTypeName()); out.write("\n"); Rule[] rules = fts.getRules(); for (int i = 0; i < rules.length; i++) { rules[i].accept(this); } out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Rule rule) { try { out.write("\n"); out.write("" + rule.getName() + "\n"); out.write("" + rule.getAbstract() + "\n"); out.write("" + rule.getTitle() + "\n"); if (rule.getMaxScaleDenominator() != Double.POSITIVE_INFINITY) { out.write("" + rule.getMaxScaleDenominator() + ""); } if (rule.getMinScaleDenominator() != 0.0) { out.write("" + rule.getMinScaleDenominator() + ""); } org.geotools.filter.Filter filter = rule.getFilter(); if (filter != null) { out.write(""); try { out.write((new FilterTransformer()).transform(filter)); } catch (TransformerException e1) { LOGGER.warning(e1.toString()); } out.write(""); } if (rule.hasElseFilter()) { out.write(""); } Graphic[] gr = rule.getLegendGraphic(); for (int i = 0; i < gr.length; i++) { gr[i].accept(this); } Symbolizer[] sym = rule.getSymbolizers(); for (int i = 0; i < sym.length; i++) { sym[i].accept(this); } out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Graphic gr) { try { out.write("\n"); String geom = gr.getGeometryPropertyName(); if ((geom != null) && !geom.trim().equals("")) { out.write("" + geom + "\n"); } out.write("\n"); encode(gr.getSize()); out.write("\n"); out.write("\n"); encode(gr.getOpacity()); out.write("\n"); out.write("\n"); encode(gr.getRotation()); out.write("\n"); Symbol[] symbols = gr.getSymbols(); for (int i = 0; i < symbols.length; i++) { symbols[i].accept(this); } out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(PointSymbolizer sym) { try { out.write("\n"); if (sym.getGeometryPropertyName() != null) { out.write("\n\t" + sym.getGeometryPropertyName() + "\n\n"); } sym.getGraphic().accept(this); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(LineSymbolizer sym) { try { out.write("\n"); if (sym.getGeometryPropertyName() != null) { out.write("\n\t" + sym.getGeometryPropertyName() + "\n\n"); } sym.getStroke().accept(this); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Symbolizer sym) { throw new IllegalStateException("visiting an unknown symbolizer"); } public void visit(Fill fill) { try { out.write("\n"); if (fill.getGraphicFill() != null) { fill.getGraphicFill().accept(this); } encodeCssParam("fill", fill.getColor()); encodeCssParam("fill-opacity", fill.getOpacity()); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Stroke stroke) { try { out.write("\n"); if (stroke.getGraphicFill() != null) { stroke.getGraphicFill().accept(this); } if (stroke.getGraphicStroke() != null) { stroke.getGraphicStroke().accept(this); } encodeCssParam("stroke", stroke.getColor()); encodeCssParam("stroke-linecap", stroke.getLineCap()); encodeCssParam("stroke-linejoin", stroke.getLineJoin()); encodeCssParam("stroke-opacity", stroke.getOpacity()); encodeCssParam("stroke-width", stroke.getWidth()); encodeCssParam("stroke-dashoffset", stroke.getDashOffset()); float[] dash = stroke.getDashArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < dash.length; i++) { sb.append(dash[i] + " "); } out.write("" + sb.toString() + "\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(TextSymbolizer sym) { try { out.write("\n"); if (sym.getGeometryPropertyName() != null) { out.write("\n\t" + sym.getGeometryPropertyName() + "\n\n"); } out.write("\n"); out.write("\n"); Font[] fonts = sym.getFonts(); for (int i = 0; i < fonts.length; i++) { encodeCssParam("font-family", fonts[i].getFontFamily()); } encodeCssParam("font-size", fonts[0].getFontSize()); encodeCssParam("font-style", fonts[0].getFontStyle()); encodeCssParam("font-weight", fonts[0].getFontWeight()); out.write("\n"); out.write("\n"); sym.getHalo().accept(this); sym.getFill().accept(this); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(PolygonSymbolizer sym) { try { out.write("\n"); out.write("\n\t" + sym.getGeometryPropertyName() + "\n\n"); if (sym.getFill() != null) { sym.getFill().accept(this); } if (sym.getStroke() != null) { sym.getStroke().accept(this); } out.write(""); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Mark mark) { try { out.write("\n"); out.write("\n"); try { out.write((new FilterTransformer()).transform(mark.getWellKnownName())); } catch (TransformerException e1) { LOGGER.warning(e1.toString()); } out.write("\n"); if (mark.getFill() != null) { mark.getFill().accept(this); } if (mark.getStroke() != null) { mark.getStroke().accept(this); } out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(ExternalGraphic exgr) { try { out.write("\n"); out.write("" + exgr.getFormat() + "\n"); out.write( ""); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(PointPlacement pp) { try { out.write("\n"); out.write("\n"); pp.getAnchorPoint().accept(this); pp.getDisplacement().accept(this); out.write("\n"); encode(pp.getRotation()); out.write("\n"); out.write("\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(AnchorPoint ap) { try { out.write("\n"); out.write("\n"); encode(ap.getAnchorPointX()); out.write("\n"); out.write("\n"); encode(ap.getAnchorPointY()); out.write("\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Displacement dis) { try { out.write("\n"); out.write("\n"); encode(dis.getDisplacementX()); out.write("\n"); out.write("\n"); encode(dis.getDisplacementY()); out.write("\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(LinePlacement lp) { try { out.write("\n"); out.write("\n"); out.write("\n"); encode(lp.getPerpendicularOffset()); out.write("\n"); out.write("\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(Halo halo) { try { out.write("\n"); halo.getFill().accept(this); out.write("\n"); encode(halo.getRadius()); out.write("\n"); out.write("\n"); } catch (IOException e) { throw new RuntimeException(e); } } public void visit(RasterSymbolizer raster) { throw new UnsupportedOperationException(); } public void visit(StyledLayerDescriptor arg0) { throw new UnsupportedOperationException(); } public void visit(NamedLayer arg0) { throw new UnsupportedOperationException(); } public void visit(UserLayer arg0) { throw new UnsupportedOperationException(); } public void visit(FeatureTypeConstraint arg0) { throw new UnsupportedOperationException(); } }