/**
  * Loads the named objects from a zip file, using the prepared map from names to zip entries.
  * Returns a map from names to {@link AspectGraph}s.
  *
  * @param file the file to read from
  * @param graphs the mapping from object names to zip entries
  */
 private Map<String, AspectGraph> loadObjects(
     ResourceKind kind, ZipFile file, Map<String, ZipEntry> graphs) throws IOException {
   FileType filter = kind.getFileType();
   Map<String, AspectGraph> result = new HashMap<String, AspectGraph>();
   for (Map.Entry<String, ZipEntry> graphEntry : graphs.entrySet()) {
     String graphName = filter.stripExtension(graphEntry.getKey());
     InputStream in = file.getInputStream(graphEntry.getValue());
     try {
       AttrGraph xmlGraph = GxlIO.instance().loadGraph(in);
       /*
        * For backward compatibility, we set the role and name of the
        * graph.
        */
       xmlGraph.setRole(kind.getGraphRole());
       xmlGraph.setName(createQualName(graphName).toString());
       addLayout(file, graphEntry.getKey(), xmlGraph);
       AspectGraph graph = xmlGraph.toAspectGraph();
       /* Store the graph */
       result.put(graphName, graph);
     } catch (FormatException exc) {
       throw new IOException(
           String.format("Format error while loading '%s':\n%s", graphName, exc.getMessage()),
           exc);
     } catch (IOException exc) {
       throw new IOException(
           String.format("Error while loading '%s':\n%s", graphName, exc.getMessage()), exc);
     }
   }
   return result;
 }
 /**
  * Extracts the entry name for the grammar from a given JAR or ZIP URL. The name is either given
  * in the JAR entry part of the URL, or it is taken to be the name of the archive file.
  *
  * @param url the URL to be parsed; guaranteed to be a JAR or ZIP
  * @return the name; non-null
  * @throws IllegalArgumentException if no name can be found according to the above rules
  * @throws IOException if the URL cannot be opened
  */
 private String extractEntryName(URL url) throws IOException {
   String result;
   JarURLConnection connection = (JarURLConnection) url.openConnection();
   result = connection.getEntryName();
   if (result == null) {
     result = FileType.getPureName(new File(connection.getJarFileURL().getPath()));
   }
   return result;
 }
Example #3
0
 @Override
 public Set<Resource> doImport(File file, FileType fileType, GrammarModel grammar)
     throws PortException {
   Set<Resource> resources;
   try {
     FileInputStream stream = new FileInputStream(file);
     resources = doImport(fileType.stripExtension(file.getName()), stream, fileType, grammar);
     stream.close();
   } catch (IOException e) {
     throw new PortException(e);
   }
   return resources;
 }
Example #4
0
 /**
  * Exports a given state using a filename derived from a state pattern.
  *
  * @param state the state to be exported
  * @param pattern the filename pattern
  * @throws IOException if anything went wrong during export
  */
 public static File exportState(GraphState state, String pattern) throws IOException {
   String stateFilename = pattern.replace(PLACEHOLDER, "" + state.getNumber());
   File stateFile = new File(stateFilename);
   Pair<FileType, Exporter> stateFormat = Exporters.getAcceptingFormat(stateFilename);
   if (stateFormat != null) {
     try {
       stateFormat.two().doExport(new Exportable(state.getGraph()), stateFile, stateFormat.one());
     } catch (PortException e1) {
       throw new IOException(e1);
     }
   } else {
     if (!FileType.hasAnyExtension(stateFile)) {
       stateFile = FileType.STATE.addExtension(stateFile);
     }
     Groove.saveGraph(GraphConverter.toAspect(state.getGraph()), stateFile);
   }
   return stateFile;
 }