File: GraphBundleLoader.java

package info (click to toggle)
libgrinvin-core-java 1.2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 3,904 kB
  • ctags: 5,009
  • sloc: java: 23,494; xml: 423; makefile: 15
file content (330 lines) | stat: -rw-r--r-- 11,246 bytes parent folder | download
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
/* GraphBundleLoader.java
 * =========================================================================
 * This file is part of the GrInvIn project - http://www.grinvin.org
 * 
 * Copyright (C) 2005-2008 Universiteit Gent
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program 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
 * General Public License for more details.
 * 
 * A copy of the GNU General Public License can be found in the file
 * LICENSE.txt provided with the source distribution of this program (see
 * the META-INF directory in the source jar). This license can also be
 * found on the GNU website at http://www.gnu.org/licenses/gpl.html.
 * 
 * If you did not receive a copy of the GNU General Public License along
 * with this program, contact the lead developer, or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.grinvin.io.graphs;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.grinvin.graphs.Annotation;
import org.grinvin.graphs.Embedding;
import org.grinvin.graphs.Graph;
import org.grinvin.graphs.GraphBundle;
import org.grinvin.gui.icons.DefaultGraphIconFactory;
import org.grinvin.gui.icons.GraphIconFactory;
import org.grinvin.io.DirectorySectionLoader;
import org.grinvin.io.IOFormatException;
import org.grinvin.io.InvariantValuesLoader;
import org.grinvin.io.SectionLoader;
import org.grinvin.io.ZipFileSectionLoader;
import org.grinvin.io.ZipInputStreamSectionLoader;
import org.grinvin.io.compat.GraphLoader_1_0;
import org.grinvin.util.InternationalizedProperties;

/**
 * Loads a {@link GraphBundle} from a zip file.
 */
public class GraphBundleLoader {
    
    //
    private static final Logger LOGGER = Logger.getLogger("org.grinvin.io");
    
    //
    private final GraphBundle bundle;
    
    //
    private final SectionLoader sloader;
    
    //
    private final String path;
    
    // meta-information stored in the file
    private Properties meta;
    
    /**
     * Default constructor.
     */
    private GraphBundleLoader(GraphBundle bundle, SectionLoader sloader, String path) {
        this.bundle = bundle;
        this.sloader = sloader;
        this.path = path;
    }
    
    private GraphBundleLoader(GraphBundle bundle, SectionLoader sloader) {
        this(bundle, sloader, null);
    }
    
    /**
     * Get the entry with the given name.
     */
    private InputStream openEntry(String name) throws IOException {
        InputStream in;
        if (path != null) {
            in = sloader.openSection(path + "/" + name);
        } else {
            in = sloader.openSection(name);
        }
        if (in == null)
            throw new IOFormatException("Expected section: " + path + "/" + name);
        return in;
    }
    
    /**
     * Load the meta-information for the graph.
     */
    private void loadMeta() throws IOException {
        meta = new Properties();
        InputStream in = openEntry("meta-info.xml");
        meta.loadFromXML(in);
        in.close();
    }
    
    /**
     * Load the graph properties.
     */
    private void loadProperties() throws IOException {
        InputStream in = openEntry("resources.xml");
        InternationalizedProperties props = new InternationalizedProperties();
        props.load(in);
        bundle.setProperties(props);
        in.close();
    }

    /**
     * Load the graph properties without validating the XML schema.
     */
    private void loadPropertiesNonValidating() throws IOException {
        InputStream in = openEntry("resources.xml");
        InternationalizedProperties props = new InternationalizedProperties();
        props.loadNonValidating(in);
        bundle.setProperties(props);
        in.close();
    }

    
    /**
     * Load the abstract graph.
     */
    private void loadGraph() throws IOException {
        InputStream in = openEntry("graph.xml");
        Graph graph = bundle.createGraph();
        GraphLoader.load(graph, in);
        in.close();
    }
    
    /**
     * Load the abstract graph (version 1.0).
     */
    private void loadGraph_1_0() throws IOException {
        InputStream in = openEntry("graph.xml");
        Graph graph = bundle.createGraph();
        Annotation annotation = bundle.createAnnotation();
        GraphLoader_1_0.load(graph, annotation, in);
        in.close();
    }
    
    private void loadEmbeddings(boolean validate) throws IOException {
        try {
            int embeddingsCount = Integer.parseInt(meta.getProperty("nrOfEmbeddings", "2"));
            for (int i = 0; i < embeddingsCount; i++) {
                if (validate) {
                    addEmbedding(i);
                } else {
                    addEmbeddingNonValidating(i);
                }
            }
        } catch (NumberFormatException nfex) {
            throw new IOFormatException("number of embeddings is not an integer", nfex);
        }
    }
            
    /**
     * Add the embedding with the given index as the next embedding in the bundle.
     */
    private void addEmbedding(int index) throws IOException {
        InputStream in = openEntry("embedding_"+index+".xml");
        Embedding embedding = bundle.createEmbedding();
        EmbeddingLoader.load(embedding, in);
        in.close();
    }

    /**
     * Add the embedding with the given index as the next embedding in the bundle
     * without validating the XML schema.
     */
    private void addEmbeddingNonValidating(int index) throws IOException {
        InputStream in = openEntry("embedding_"+index+".xml");
        Embedding embedding = bundle.createEmbedding();
        EmbeddingLoader.loadNonValidating(embedding, in);
        in.close();
    }

    private void loadAnnotations() throws IOException {
        try {
            int count = Integer.parseInt(meta.getProperty("nrOfAnnotations"));
            for (int i = 0; i < count; i++) {
                addAnnotation(i);
            }
        } catch (NumberFormatException nfex) {
            throw new IOFormatException("number of annotations is not an integer", nfex);
        }
    }

    
    /**
     * Add the embedding with the given index as the next embedding in the bundle.
     */
    private void addAnnotation(int index) throws IOException {
        InputStream in = openEntry("annotation_"+index+".xml");
        Annotation annotation = bundle.createAnnotation();
        AnnotationLoader.load(annotation, in);
        in.close();
    }
    
    //
    private void loadInvariantValues() throws IOException {
        InputStream in;
        try {
            in = openEntry("invariantvalues.xml");
        } catch (IOFormatException ex) {
            // the given bundle does not contain invariant values
            //TODO: log failure somewhere
            return;
        }
        InvariantValuesLoader.load(bundle, in);
        in.close();
    }

    //
    private void loadInvariantValues_1_0() throws IOException {
        InputStream in;
        try {
            in = openEntry("invariantvalues.xml");
        } catch (IOFormatException ex) {
            // the given bundle does not contain invariant values
            //TODO: log failure somewhere
            return;
        }
        InvariantValuesLoader.load_1_0(bundle, in);
        in.close();
    }

    
    /**
     * Load the bundle and close the inputstream.
     */
    private void load() throws IOException {
        loadMeta();
        String versionStr = meta.getProperty("version");
        if (versionStr.equals("1.0")) {
            loadPropertiesNonValidating();
            loadGraph_1_0();
            loadEmbeddings(false);
            loadInvariantValues_1_0();
        } else {
            loadProperties();
            loadGraph();
            loadEmbeddings(true);
            loadAnnotations();
            loadInvariantValues();
        }
        initGraphIconFactory();
    }
    
    /**
     * Initialize the graph icon factory.
     */
    private void initGraphIconFactory() {
        String name = meta.getProperty("graphIconFactory");
        if (name == null) {
            bundle.setGraphIconFactory(DefaultGraphIconFactory.getInstance());
        } else {
            try {
                Class<?> clazz = Class.forName(name);
                Method method = clazz.getMethod("getInstance");
                GraphIconFactory gif = (GraphIconFactory)method.invoke(null); // static method
                bundle.setGraphIconFactory(gif);
            } catch (Exception ex) {
                LOGGER.log(Level.WARNING, "unable to instantiate graph icon factory", ex);
            }
        }
    }
    
    /**
     * Load the bundle from the given file.
     * @param bundle Empty bundle which will hold the result of the load operation.
     * @param file File which contains the graph bundle in Zip-format
     */
    public static void loadFromZip(GraphBundle bundle, File file) throws IOException {
        ZipFile zip = new ZipFile(file);
        GraphBundleLoader loader = new GraphBundleLoader(bundle, new ZipFileSectionLoader(zip));
        loader.load();
        zip.close();
    }
    
    /**
     * Load the bundle from the given input stream.
     * @param bundle Empty bundle which will hold the result of the load operation.
     * @param in Input stream which contains the graph bundle in Zip-format
     */
    public static void loadFromZip(GraphBundle bundle, InputStream in) throws IOException {
        ZipInputStream zip = new ZipInputStream(in);
        GraphBundleLoader loader = new GraphBundleLoader(bundle, new ZipInputStreamSectionLoader(zip));
        loader.load();
        in.close();
    }
    
    /**
     * Load the bundle from the given directory. The directory should
     * contain the uncompressed contents of a ZIP-archive.<p>
     * <b>Note:</b> The preferred representation of graph bundles on disk
     * is by means of compressed ZIP-archives. This method is provided
     * primarily for debugging purposes.
     *
     * @param bundle Empty bundle which will hold the result of the load operation.
     * @param dir Directory which contains the graph bundle as
     * uncompressed contents of a ZIP-archive.
     */
    public static void loadFromDirectory(GraphBundle bundle, File dir) throws IOException {
        GraphBundleLoader loader = new GraphBundleLoader(bundle, new DirectorySectionLoader(dir));
        loader.load();
    }
    
    //
    public static void load(GraphBundle bundle, SectionLoader sloader, String path) throws IOException {
        GraphBundleLoader loader = new GraphBundleLoader(bundle, sloader, path);
        loader.load();
    }

}