001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.styling.visitor;
017:
018: import java.util.Stack;
019:
020: import org.geotools.event.GTCloneUtil;
021: import org.geotools.filter.visitor.DuplicatingFilterVisitor;
022: import org.geotools.styling.AnchorPoint;
023: import org.geotools.styling.ColorMap;
024: import org.geotools.styling.ColorMapEntry;
025: import org.geotools.styling.Displacement;
026: import org.geotools.styling.ExternalGraphic;
027: import org.geotools.styling.FeatureTypeConstraint;
028: import org.geotools.styling.FeatureTypeStyle;
029: import org.geotools.styling.Fill;
030: import org.geotools.styling.Graphic;
031: import org.geotools.styling.Halo;
032: import org.geotools.styling.LinePlacement;
033: import org.geotools.styling.LineSymbolizer;
034: import org.geotools.styling.Mark;
035: import org.geotools.styling.NamedLayer;
036: import org.geotools.styling.PointPlacement;
037: import org.geotools.styling.PointSymbolizer;
038: import org.geotools.styling.PolygonSymbolizer;
039: import org.geotools.styling.RasterSymbolizer;
040: import org.geotools.styling.Rule;
041: import org.geotools.styling.Stroke;
042: import org.geotools.styling.Style;
043: import org.geotools.styling.StyleFactory;
044: import org.geotools.styling.StyleVisitor;
045: import org.geotools.styling.StyledLayer;
046: import org.geotools.styling.StyledLayerDescriptor;
047: import org.geotools.styling.Symbol;
048: import org.geotools.styling.Symbolizer;
049: import org.geotools.styling.TextSymbolizer;
050: import org.geotools.styling.UserLayer;
051: import org.opengis.filter.Filter;
052: import org.opengis.filter.FilterFactory2;
053: import org.opengis.filter.expression.Expression;
054:
055: /**
056: * Used to duplicate a Style object (anything in the SLD hierarchy).
057: * <p>
058: * Some methods in this visitor use a clone method to duplicate, rather than
059: * constructing a new object. This should be made consistent with the other
060: * methods, and tested.
061: * </p>
062: *
063: * @author Cory Horner, Refractions Research Inc.
064: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/styling/visitor/DuplicatorStyleVisitor.java $
065: * @deprecated use {@link DuplicatingStyleVisitor}
066: */
067: public class DuplicatorStyleVisitor implements StyleVisitor {
068: Stack pages = new Stack(); // need a Stack as Filter structure is recursive
069:
070: //Stack pages; // need a Stack as Filter structure is recursive
071: StyleFactory sf;
072:
073: /** TODO: support duplication of pure open gis filters */
074: public DuplicatingFilterVisitor copyFilter;
075:
076: public DuplicatorStyleVisitor(StyleFactory sf, FilterFactory2 ff) { // FilterFactory factory
077: this .sf = sf;
078: this .copyFilter = new DuplicatingFilterVisitor(ff);
079: }
080:
081: /** Duplicate an expression using copyFilter */
082: public Expression copy(Expression expression) {
083: return (Expression) expression.accept(copyFilter, null);
084: //return (Expression) copyFilter.getPages().pop();
085: }
086:
087: /** Duplicate an expression using copyFilter */
088: public Filter copy(Filter filter) {
089: return (Filter) filter.accept(copyFilter, null);
090: // return (Filter) copyFilter.getPages().pop();
091: }
092:
093: public void setStyleFactory(StyleFactory styleFactory) {
094: this .sf = styleFactory;
095: }
096:
097: public Stack getPages() {
098: return pages;
099: }
100:
101: public Object getCopy() {
102: return pages.firstElement();
103: }
104:
105: public void visit(StyledLayerDescriptor sld) {
106: StyledLayerDescriptor copy = null;
107:
108: StyledLayer[] layers = sld.getStyledLayers();
109: StyledLayer[] layersCopy = new StyledLayer[layers.length];
110: final int length = layers.length;
111: for (int i = 0; i < length; i++) {
112: if (layers[i] instanceof UserLayer) {
113: ((UserLayer) layers[i]).accept(this );
114: layersCopy[i] = (UserLayer) getPages().pop();
115: } else if (layers[i] instanceof NamedLayer) {
116: ((NamedLayer) layers[i]).accept(this );
117: layersCopy[i] = (NamedLayer) getPages().pop();
118: }
119: }
120:
121: copy = sf.createStyledLayerDescriptor();
122: copy.setAbstract(sld.getAbstract());
123: copy.setName(sld.getName());
124: copy.setTitle(sld.getTitle());
125: copy.setStyledLayers(layersCopy);
126:
127: getPages().push(copy);
128: }
129:
130: public void visit(NamedLayer layer) {
131: NamedLayer copy = null;
132:
133: Style[] style = layer.getStyles();
134: Style[] styleCopy = new Style[style.length];
135: int length = style.length;
136: for (int i = 0; i < length; i++) {
137: if (style[i] != null) {
138: style[i].accept(this );
139: styleCopy[i] = (Style) getPages().pop();
140: }
141: }
142:
143: FeatureTypeConstraint[] lfc = layer
144: .getLayerFeatureConstraints();
145: FeatureTypeConstraint[] lfcCopy = new FeatureTypeConstraint[lfc.length];
146:
147: length = lfc.length;
148: for (int i = 0; i < length; i++) {
149: if (lfc[i] != null) {
150: lfc[i].accept(this );
151: lfcCopy[i] = (FeatureTypeConstraint) getPages().pop();
152: }
153: }
154:
155: copy = sf.createNamedLayer();
156: copy.setName(layer.getName());
157: length = styleCopy.length;
158: for (int i = 0; i < length; i++) {
159: copy.addStyle(styleCopy[i]);
160: }
161:
162: copy.setLayerFeatureConstraints(lfcCopy);
163: getPages().push(copy);
164: }
165:
166: public void visit(UserLayer layer) {
167: UserLayer copy = null;
168:
169: Style[] style = layer.getUserStyles();
170: int length = style.length;
171: Style[] styleCopy = new Style[length];
172: for (int i = 0; i < length; i++) {
173: if (style[i] != null) {
174: style[i].accept(this );
175: styleCopy[i] = (Style) getPages().pop();
176: }
177: }
178:
179: FeatureTypeConstraint[] lfc = layer
180: .getLayerFeatureConstraints();
181: FeatureTypeConstraint[] lfcCopy = new FeatureTypeConstraint[lfc.length];
182:
183: length = lfc.length;
184: for (int i = 0; i < length; i++) {
185: if (lfc[i] != null) {
186: lfc[i].accept(this );
187: lfcCopy[i] = (FeatureTypeConstraint) getPages().pop();
188: }
189: }
190:
191: copy = sf.createUserLayer();
192: copy.setName(layer.getName());
193: copy.setUserStyles(styleCopy);
194: copy.setLayerFeatureConstraints(lfcCopy);
195:
196: getPages().push(copy);
197: }
198:
199: public void visit(Style style) {
200: Style copy = null;
201:
202: FeatureTypeStyle[] fts = style.getFeatureTypeStyles();
203: final int length = fts.length;
204: FeatureTypeStyle[] ftsCopy = new FeatureTypeStyle[length];
205: for (int i = 0; i < length; i++) {
206: if (fts[i] != null) {
207: fts[i].accept(this );
208: ftsCopy[i] = (FeatureTypeStyle) getPages().pop();
209: }
210: }
211:
212: copy = sf.createStyle();
213: copy.setAbstract(style.getAbstract());
214: copy.setName(style.getName());
215: copy.setTitle(style.getTitle());
216: copy.setFeatureTypeStyles(ftsCopy);
217:
218: getPages().push(copy);
219: }
220:
221: public void visit(Rule rule) {
222: Rule copy = null;
223:
224: Filter filterCopy = null;
225:
226: if (rule.getFilter() != null) {
227: Filter filter = rule.getFilter();
228: filterCopy = copy(filter);
229: }
230:
231: Graphic[] legendGraphic = rule.getLegendGraphic();
232: Graphic[] legendGraphicCopy = new Graphic[legendGraphic.length];
233:
234: int length = legendGraphic.length;
235: for (int i = 0; i < length; i++) {
236: if (legendGraphic[i] != null) {
237: legendGraphic[i].accept(this );
238: legendGraphicCopy[i] = (Graphic) getPages().pop();
239: }
240: }
241:
242: Symbolizer[] symbolizer = rule.getSymbolizers();
243: Symbolizer[] symbolizerCopy = new Symbolizer[symbolizer.length];
244:
245: length = symbolizer.length;
246: for (int i = 0; i < length; i++) {
247: if (symbolizer[i] != null) {
248: symbolizer[i].accept(this );
249: symbolizerCopy[i] = (Symbolizer) getPages().pop();
250: }
251: }
252:
253: copy = sf.createRule();
254: copy.setAbstract(rule.getAbstract());
255: copy.setFilter(filterCopy);
256: copy.setIsElseFilter(rule.hasElseFilter());
257: copy.setLegendGraphic(legendGraphicCopy);
258: copy.setMinScaleDenominator(rule.getMinScaleDenominator());
259: copy.setMaxScaleDenominator(rule.getMaxScaleDenominator());
260: copy.setName(rule.getName());
261: copy.setTitle(rule.getTitle());
262: copy.setSymbolizers(symbolizerCopy);
263:
264: getPages().push(copy);
265: }
266:
267: public void visit(FeatureTypeStyle fts) {
268: FeatureTypeStyle copy = null;
269:
270: Rule[] rules = fts.getRules();
271: int length = rules.length;
272: Rule[] rulesCopy = new Rule[length];
273: for (int i = 0; i < length; i++) {
274: if (rules[i] != null) {
275: rules[i].accept(this );
276: rulesCopy[i] = (Rule) getPages().pop();
277: }
278: }
279:
280: copy = sf.createFeatureTypeStyle();
281: copy.setName(fts.getName());
282: copy.setTitle(fts.getTitle());
283: copy.setAbstract(fts.getAbstract());
284: copy.setFeatureTypeName(fts.getFeatureTypeName());
285: copy.setRules(rulesCopy);
286: copy.setSemanticTypeIdentifiers((String[]) fts
287: .getSemanticTypeIdentifiers().clone());
288:
289: getPages().push(copy);
290: }
291:
292: public void visit(Fill fill) {
293: Fill copy = null;
294:
295: try {
296: copy = (Fill) GTCloneUtil.clone(fill); //TODO: remove temporary hack
297: } catch (CloneNotSupportedException erp) {
298: throw new RuntimeException(erp);
299: }
300:
301: getPages().push(copy);
302: }
303:
304: public void visit(Stroke stroke) {
305: Stroke copy = null;
306:
307: try {
308: copy = (Stroke) GTCloneUtil.clone(stroke); //TODO: remove temporary hack
309: } catch (CloneNotSupportedException erp) {
310: throw new RuntimeException(erp);
311: }
312:
313: getPages().push(copy);
314: }
315:
316: public void visit(Symbolizer sym) {
317: // Should not happen?
318: throw new RuntimeException("visit(Symbolizer) unsupported");
319: }
320:
321: public void visit(PointSymbolizer ps) {
322: PointSymbolizer copy = null;
323:
324: try {
325: copy = (PointSymbolizer) GTCloneUtil.clone(ps); //TODO: remove temporary hack
326: } catch (CloneNotSupportedException erp) {
327: throw new RuntimeException(erp);
328: }
329:
330: getPages().push(copy);
331: }
332:
333: public void visit(LineSymbolizer line) {
334: LineSymbolizer copy = null;
335:
336: try {
337: copy = (LineSymbolizer) GTCloneUtil.clone(line); //TODO: remove temporary hack
338: } catch (CloneNotSupportedException erp) {
339: throw new RuntimeException(erp);
340: }
341:
342: getPages().push(copy);
343: }
344:
345: public void visit(PolygonSymbolizer poly) {
346: PolygonSymbolizer copy = null;
347:
348: try {
349: copy = (PolygonSymbolizer) GTCloneUtil.clone(poly); //TODO: remove temporary hack
350: } catch (CloneNotSupportedException erp) {
351: throw new RuntimeException(erp);
352: }
353:
354: getPages().push(copy);
355: }
356:
357: public void visit(TextSymbolizer text) {
358: TextSymbolizer copy = null;
359:
360: try {
361: copy = (TextSymbolizer) GTCloneUtil.clone(text); //TODO: remove temporary hack
362: } catch (CloneNotSupportedException erp) {
363: throw new RuntimeException(erp);
364: }
365:
366: getPages().push(copy);
367: }
368:
369: public void visit(RasterSymbolizer raster) {
370: RasterSymbolizer copy = null;
371:
372: try {
373: copy = (RasterSymbolizer) GTCloneUtil.clone(raster); //TODO: remove temporary hack
374: } catch (CloneNotSupportedException erp) {
375: throw new RuntimeException(erp);
376: }
377:
378: getPages().push(copy);
379: }
380:
381: public void visit(Graphic gr) {
382: Graphic copy = null;
383:
384: Displacement displacementCopy = null;
385:
386: if (gr.getDisplacement() != null) {
387: gr.getDisplacement().accept(this );
388: displacementCopy = (Displacement) getPages().pop();
389: }
390:
391: ExternalGraphic[] externalGraphics = gr.getExternalGraphics();
392: ExternalGraphic[] externalGraphicsCopy = new ExternalGraphic[externalGraphics.length];
393:
394: int length = externalGraphics.length;
395: for (int i = 0; i < length; i++) {
396: if (externalGraphics[i] != null) {
397: externalGraphics[i].accept(this );
398: externalGraphicsCopy[i] = (ExternalGraphic) getPages()
399: .pop();
400: }
401: }
402:
403: Mark[] marks = gr.getMarks();
404: Mark[] marksCopy = new Mark[marks.length];
405: length = marks.length;
406: for (int i = 0; i < length; i++) {
407: if (marks[i] != null) {
408: marks[i].accept(this );
409: marksCopy[i] = (Mark) getPages().pop();
410: }
411: }
412:
413: Expression opacityCopy = null;
414:
415: if (gr.getOpacity() != null) {
416: opacityCopy = (Expression) gr.getOpacity().accept(
417: copyFilter, null);
418: }
419:
420: Expression rotationCopy = null;
421:
422: if (gr.getRotation() != null) {
423: rotationCopy = (Expression) gr.getRotation().accept(
424: copyFilter, null);
425: }
426:
427: Expression sizeCopy = null;
428:
429: if (gr.getSize() != null) {
430: sizeCopy = (Expression) gr.getSize().accept(copyFilter,
431: null);
432: }
433:
434: Symbol[] symbols = gr.getSymbols();
435: length = symbols.length;
436: Symbol[] symbolCopys = new Symbol[length];
437:
438: for (int i = 0; i < length; i++) {
439: if (symbols[i] != null) {
440: symbols[i].accept(this );
441: symbolCopys[i] = (Symbol) getPages().pop();
442: }
443: }
444:
445: copy = sf.createDefaultGraphic();
446: copy.setGeometryPropertyName(gr.getGeometryPropertyName());
447: copy.setDisplacement(displacementCopy);
448: copy.setExternalGraphics(externalGraphicsCopy);
449: copy.setMarks(marksCopy);
450: copy.setOpacity(opacityCopy);
451: copy.setRotation(rotationCopy);
452: copy.setSize(sizeCopy);
453: copy.setSymbols(symbolCopys);
454:
455: getPages().push(copy);
456: }
457:
458: public void visit(Mark mark) {
459: Mark copy = null;
460:
461: Fill fillCopy = null;
462:
463: if (mark.getFill() != null) {
464: mark.accept(this );
465: fillCopy = (Fill) getPages().pop();
466: }
467:
468: Expression rotationCopy = null;
469:
470: if (mark.getRotation() != null) {
471: rotationCopy = (Expression) mark.getRotation().accept(
472: copyFilter, null);
473: }
474:
475: Expression sizeCopy = null;
476:
477: if (mark.getSize() != null) {
478: sizeCopy = (Expression) mark.getSize().accept(copyFilter,
479: null);
480: }
481:
482: Stroke strokeCopy = null;
483:
484: if (mark.getStroke() != null) {
485: mark.getStroke().accept(this );
486: strokeCopy = (Stroke) getPages().pop();
487: }
488:
489: Expression wellKnownNameCopy = null;
490:
491: if (mark.getWellKnownName() != null) {
492: wellKnownNameCopy = (Expression) mark.getWellKnownName()
493: .accept(copyFilter, null);
494: }
495:
496: copy = sf.createMark();
497: copy.setFill(fillCopy);
498: copy.setRotation(rotationCopy);
499: copy.setSize(sizeCopy);
500: copy.setStroke(strokeCopy);
501: copy.setWellKnownName(wellKnownNameCopy);
502:
503: getPages().push(copy);
504: }
505:
506: public void visit(ExternalGraphic exgr) {
507: ExternalGraphic copy = null;
508:
509: try {
510: copy = (ExternalGraphic) GTCloneUtil.clone(exgr); //TODO: remove temporary hack
511: } catch (CloneNotSupportedException erp) {
512: throw new RuntimeException(erp);
513: }
514:
515: getPages().push(copy);
516: }
517:
518: public void visit(PointPlacement pp) {
519: PointPlacement copy = null;
520:
521: try {
522: copy = (PointPlacement) GTCloneUtil.clone(pp); //TODO: remove temporary hack
523: } catch (CloneNotSupportedException erp) {
524: throw new RuntimeException(erp);
525: }
526:
527: getPages().push(copy);
528: }
529:
530: public void visit(AnchorPoint ap) {
531: AnchorPoint copy = null;
532:
533: try {
534: copy = (AnchorPoint) GTCloneUtil.clone(ap); //TODO: remove temporary hack
535: } catch (CloneNotSupportedException erp) {
536: throw new RuntimeException(erp);
537: }
538:
539: getPages().push(copy);
540: }
541:
542: public void visit(Displacement dis) {
543: Displacement copy = null;
544:
545: try {
546: copy = (Displacement) GTCloneUtil.clone(dis); //TODO: remove temporary hack
547: } catch (CloneNotSupportedException erp) {
548: throw new RuntimeException(erp);
549: }
550:
551: getPages().push(copy);
552: }
553:
554: public void visit(LinePlacement lp) {
555: LinePlacement copy = null;
556:
557: try {
558: copy = (LinePlacement) GTCloneUtil.clone(lp); //TODO: remove temporary hack
559: } catch (CloneNotSupportedException erp) {
560: throw new RuntimeException(erp);
561: }
562:
563: getPages().push(copy);
564: }
565:
566: public void visit(Halo halo) {
567: Halo copy = null;
568:
569: try {
570: copy = (Halo) GTCloneUtil.clone(halo); //TODO: remove temporary hack
571: } catch (CloneNotSupportedException erp) {
572: throw new RuntimeException(erp);
573: }
574:
575: getPages().push(copy);
576: }
577:
578: public void visit(FeatureTypeConstraint ftc) {
579: FeatureTypeConstraint copy = null;
580:
581: try {
582: copy = (FeatureTypeConstraint) GTCloneUtil.clone(ftc); //TODO: remove temporary hack
583: } catch (CloneNotSupportedException erp) {
584: throw new RuntimeException(erp);
585: }
586:
587: getPages().push(copy);
588: }
589:
590: public void visit(ColorMap arg0) {
591: // TODO Auto-generated method stub
592: }
593:
594: public void visit(ColorMapEntry arg0) {
595: // TODO Auto-generated method stub
596: }
597: }
|