Home | History | Annotate | Download | only in impl
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package signature.io.impl;
     18 
     19 import signature.converter.Visibility;
     20 import signature.io.IApiExternalizer;
     21 import signature.io.IApiLoader;
     22 import signature.model.IApi;
     23 import java.io.File;
     24 import java.io.FileInputStream;
     25 import java.io.FileOutputStream;
     26 import java.io.IOException;
     27 import java.io.ObjectInputStream;
     28 import java.io.ObjectOutputStream;
     29 import java.util.Set;
     30 
     31 public class BinaryApi implements IApiExternalizer, IApiLoader {
     32 
     33     public void externalizeApi(String fileName, IApi api) throws IOException {
     34 
     35         File directory = new File(fileName);
     36         if (!directory.exists()) {
     37             directory.mkdirs();
     38         }
     39 
     40         File file = new File(directory, getFileName(api));
     41         file.createNewFile();
     42 
     43         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
     44                 file));
     45         oos.writeObject(api);
     46 
     47         oos.flush();
     48         oos.close();
     49     }
     50 
     51     private String getFileName(IApi api) {
     52         return api.getName().replaceAll(" ", "_").concat(".sig");
     53     }
     54 
     55     public IApi loadApi(String name, Visibility visibility,
     56             Set<String> fileNames, Set<String> packageNames) throws
     57             IOException {
     58         System.err
     59                 .println("Binary signature loader ignores visibility and " +
     60                         "package names.");
     61         if (fileNames.size() != 1) {
     62             throw new IllegalArgumentException(
     63                     "Only one file can be processed by the binary signature " +
     64                     "loader.");
     65         }
     66         String fileName = fileNames.iterator().next();
     67         File file = new File(fileName);
     68         ObjectInputStream ois = new ObjectInputStream(
     69                 new FileInputStream(file));
     70         IApi sig = null;
     71         try {
     72             sig = (IApi) ois.readObject();
     73         } catch (ClassNotFoundException e) {
     74             throw new IllegalArgumentException(e);
     75         }
     76         if (name != null) {
     77             sig.setName(name);
     78         }
     79 
     80         ois.close();
     81         return sig;
     82     }
     83 }
     84