Skip to main content
summaryrefslogtreecommitdiffstats
blob: fe7e8859995e00d03df308e96f7deb06212cb02a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/******************************************************************************
 * Copyright (c) 2005 BEA Systems, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Konstantin Komissarchik - initial API and implementation
 ******************************************************************************/

package org.eclipse.jst.common.jdt.internal.classpath;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jst.common.frameworks.CommonFrameworksPlugin;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author <a href="mailto:kosta@bea.com">Konstantin Komissarchik</a>
 */

public final class ClasspathDecorationsManager
{
    private final File f;
    private final HashMap decorations;
    
    public ClasspathDecorationsManager( final String plugin )
    {
        final IWorkspace ws = ResourcesPlugin.getWorkspace();
        final File wsdir = ws.getRoot().getLocation().toFile();
        final File wsmdroot = new File( wsdir, ".metadata/.plugins" ); //$NON-NLS-1$
        final File pmdroot = new File( wsmdroot, plugin );
    
        this.f = new File( pmdroot, "classpath.decorations.xml" ); //$NON-NLS-1$
        this.decorations = read();
    }
    
    public ClasspathDecorations getDecorations( final String container,
                                                final String entry )
    {
        final HashMap submap = (HashMap) this.decorations.get( container );
        
        if( submap == null )
        {
            return null;
        }
        
        return (ClasspathDecorations) submap.get( entry );
    }

    public void setDecorations( final String container,
                                final String entry,
                                final ClasspathDecorations dec )
    {
        HashMap submap = (HashMap) this.decorations.get( container );
        
        if( submap == null )
        {
            submap = new HashMap();
            this.decorations.put( container, submap );
        }
        
        submap.put( entry, dec );
    }
    
    public void clearAllDecorations( final String container )
    {
        this.decorations.remove( container );
    }

    public void save()
    {
        final File folder = this.f.getParentFile();
        
        if( ! folder.exists() && ! folder.mkdirs() )
        {
            return;
        }
        
        PrintWriter w = null;
        
        try
        {
            w = new PrintWriter( new BufferedWriter( new FileWriter( this.f ) ) );
            
            w.println( "<classpath>" ); //$NON-NLS-1$
            
            for( Iterator itr1 = decorations.entrySet().iterator(); 
                 itr1.hasNext(); )
            {
                final Map.Entry entry1 = (Map.Entry) itr1.next();
                final Map submap = (Map) entry1.getValue();
                
                w.print( "  <container id=\"" ); //$NON-NLS-1$
                w.print( (String) entry1.getKey() );
                w.println( "\">" ); //$NON-NLS-1$
                
                for( Iterator itr2 = submap.entrySet().iterator(); 
                     itr2.hasNext(); )
                {
                    final Map.Entry entry2 = (Map.Entry) itr2.next();
                    
                    final ClasspathDecorations dec 
                        = (ClasspathDecorations) entry2.getValue();
                    
                    w.print( "    <entry id=\"" ); //$NON-NLS-1$
                    w.print( (String) entry2.getKey() );
                    w.println( "\">" ); //$NON-NLS-1$
                    
                    if( dec.getSourceAttachmentPath() != null )
                    {
                        w.print( "      <source-attachment-path>" ); //$NON-NLS-1$
                        w.print( dec.getSourceAttachmentPath().toString() );
                        w.println( "</source-attachment-path>" ); //$NON-NLS-1$
                    }

                    if( dec.getSourceAttachmentRootPath() != null )
                    {
                        w.print( "      <source-attachment-root-path>" ); //$NON-NLS-1$
                        w.print( dec.getSourceAttachmentRootPath().toString() );
                        w.println( "</source-attachment-root-path>" ); //$NON-NLS-1$
                    }
                    
                    final IClasspathAttribute[] attrs 
                        = dec.getExtraAttributes();
                    
                    for( int i = 0; i < attrs.length; i++ )
                    {
                        final IClasspathAttribute attr = attrs[ i ];
                        
                        w.print( "      <attribute name=\"" ); //$NON-NLS-1$
                        w.print( attr.getName() );
                        w.print( "\">" ); //$NON-NLS-1$
                        w.print( attr.getValue() );
                        w.println( "</attribute>" ); //$NON-NLS-1$
                    }
                    
                    w.println( "    </entry>" ); //$NON-NLS-1$
                }
                
                w.println( "  </container>" ); //$NON-NLS-1$
            }
            
            w.println( "</classpath>" ); //$NON-NLS-1$
        }
        catch( IOException e )
        {
            CommonFrameworksPlugin.log( e );
        }
        finally
        {
            w.close();
        }
    }
    
    private HashMap read()
    {
        final HashMap map = new HashMap();
        if( ! this.f.exists() ) return map;

        InputStream in = null;
        Element root = null;

        try
        {
            final DocumentBuilderFactory factory 
                = DocumentBuilderFactory.newInstance();
            
            final DocumentBuilder docbuilder = factory.newDocumentBuilder();
            
            in = new BufferedInputStream( new FileInputStream( f ) );
            root = docbuilder.parse( in ).getDocumentElement();
        }
        catch( Exception e )
        {
            CommonFrameworksPlugin.log( e );
            return map;
        }
        finally
        {
            if( in != null )
            {
                try
                {
                    in.close();
                }
                catch( IOException e ) {}
            }
        }
        
        for( Iterator itr1 = elements( root, "container" ); itr1.hasNext(); ) //$NON-NLS-1$
        {
            final Element e1 = (Element) itr1.next();
            final String cid = e1.getAttribute( "id" ); //$NON-NLS-1$
            
            final HashMap submap = new HashMap();
            map.put( cid, submap );
            
            for( Iterator itr2 = elements( e1, "entry" ); itr2.hasNext(); ) //$NON-NLS-1$
            {
                final Element e2 = (Element) itr2.next();
                final String eid = e2.getAttribute( "id" ); //$NON-NLS-1$
                final ClasspathDecorations dec = new ClasspathDecorations();
                
                submap.put( eid, dec );
                
                for( Iterator itr3 = elements( e2 ); itr3.hasNext(); )
                {
                    final Element e3 = (Element) itr3.next();
                    final String n = e3.getNodeName();
                    
                    if( n.equals( "source-attachment-path" ) ) //$NON-NLS-1$
                    {
                        dec.setSourceAttachmentPath( new Path( text( e3 ) ) );
                    }
                    else if( n.equals( "source-attachment-root-path" ) ) //$NON-NLS-1$
                    {
                        dec.setSourceAttachmentRootPath( new Path( text( e3 ) ) );
                    }
                    else if( n.equals( "attribute" ) ) //$NON-NLS-1$
                    {
                        final String name = e3.getAttribute( "name" ); //$NON-NLS-1$
                        dec.addExtraAttribute( name, text( e3 ) );
                    }
                }
            }
        }
        
        return map;
    }
    
    private static String text( final Element el )
    {
        final NodeList nodes = el.getChildNodes();

        String str = null;
        StringBuffer buf = null;
        
        for( int i = 0, n = nodes.getLength(); i < n; i++ )
        {
            final Node node = nodes.item( i );
            
            if( node.getNodeType() == Node.TEXT_NODE )
            {
                final String val = node.getNodeValue();
                
                if( buf != null )
                {
                    buf.append( val );
                }
                else if( str != null )
                {
                    buf = new StringBuffer();
                    buf.append( str );
                    buf.append( val );
                    
                    str = null;
                }
                else
                {
                    str = val;
                }
            }
        }
        
        if( buf != null )
        {
            return buf.toString();
        }
        return str;
    }
    
    private static Iterator elements( final Element el,
                                     final String name )
    {
        return new ElementsIterator( el, name );
    }
    
    private static Iterator elements( final Element el )
    {
        return new ElementsIterator( el, null );
    }

    private static final class ElementsIterator
    
        implements Iterator
        
    {
        private final NodeList nodes;
        private final int length;
        private final String name;
        private int position;
        private Element element;

        public ElementsIterator( final Element parent, 
                                 final String name )
        {
            this.nodes = parent.getChildNodes();
            this.length = nodes.getLength();
            this.position = -1;
            this.name = name;

            advance();
        }

        private void advance()
        {
            this.element = null;
            this.position++;

            for( ; this.position < this.length && this.element == null; 
                 this.position++ )
            {
                final Node node = this.nodes.item( this.position );

                if( node.getNodeType() == Node.ELEMENT_NODE &&
                    ( this.name == null || 
                      node.getNodeName().equals( this.name ) ) ) 
                {
                    this.element = (Element) node;
                }
            }
        }

        public boolean hasNext() 
        {
            return ( this.element != null );
        }

        public Object next() 
        {
            final Element el = this.element;

            if( el == null ) 
            {
                throw new NoSuchElementException();
            }

            advance();

            return el;
        }

        public void remove() 
        {
            throw new UnsupportedOperationException();
        }
    }
    
}

Back to the top