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