Actually, it tries to do a deep copy. If you look at the source 
(http://svn.geotools.org/trunk/modules/library/main/src/main/java/org/geotools/styling/StyleImpl.java),
 the intention is to create a deep copy of the style. In fact, it does it fine 
except for the symbolizers. 

Code snippet:

    /**
     * Clones the Style.  Creates deep copy clone of the style.
     *
     * @return the Clone of the style.
     *
     * @throws RuntimeException DOCUMENT ME!
     *
     * @see org.geotools.styling.Style#clone()
     */
    public Object clone() {
        Style clone;

        try {
            clone = (Style) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e); // this should never happen since we 
implement Cloneable
        }

        final List<FeatureTypeStyle> ftsCopy = new 
ArrayList<FeatureTypeStyle>();
        
        
        for(FeatureTypeStyle fts : featureTypeStyles){
            ftsCopy.add( (FeatureTypeStyle) ((Cloneable) fts).clone() );
        }
        
        clone.featureTypeStyles().clear();
        ((List<FeatureTypeStyle>)clone.featureTypeStyles()).addAll(ftsCopy);

        return clone;
    }


On 2010-05-29, at 3:51 AM, Stefan Tzeggai (geb. Krüger) wrote:

> Hi
> 
> On Friday 28 May 2010 19:25:12 Enamul wrote:
>> I might have found a bug with Cloneable implementation of Style. The issue
>> seems to be with the cloning of rules. Here is a very simple test case that
>> fails with the trunk code:
>> 
>> Style clonedStyle = (Style) ((Cloneable) style).clone();
> 
> I guess that Style.clone does only a "shallow copy"; e.g. it created only a 
> new Style object, but all children are the same objects as in the original. 
> That would explain your described behaviour:
> 
>> // get the first rule of the original style
>> Rule rule = style.featureTypeStyles().get(0).rules().get(0);
>> 
>> // get the first rule of the cloned style
>> Rule clonedRule = clonedStyle.featureTypeStyles().get(0).rules().get(0);
>> 
>> // let's remove all the symbolizers from the cloned rule
>> clonedRule.symbolizers().clear();
>> 
>> System.out.println(rule.symbolizers().size());
>> 
>> The output of this test case would be 0, which is not the expected
>> behavior. The issue seems to be in RuleImpl.clone():
> 
> In GeoTools you should use the DuplicatingStyleVisitor class, and not the 
> clone() method:
> 
> http://docs.codehaus.org/display/GEOTDOC/08+StyleVisitor+use+with+DuplicatingStyleVisitor+and+RescaleStyleVisitor
> 
> It can not only be used to duplicate a Style, but also duplicates Rules, 
> Symbolizers etc..
> 
> DuplicatingStyleVisitor xerox = new DuplicatingStyleVisitor();
> style.accepts( xerox ); 
> Style copy = (Style) xerox.getCopy();
> 
> Greetings,
> Steve
> -- 
> Geotools-based SLD editor: 
> http://en.geopblushing.org/AtlasStyler
> Stefan Tzeggai (geb. Krüger)
> 
> reclaim your net - http://tor.eff.org
> enforce privacy - http://www.pgpi.org
> pgp key id: 51B576FD - http://pgp.mit.edu
> 
> Please note that according to the German law on data retention,
> information on every electronic information exchange with me is
> retained for a period of six months.
> 
> 
> ------------------------------------------------------------------------------
> 
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

------------------------------------------------------------------------------

_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to