Added support for parsing submodules & added dependency utility parser
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / repo / FilesystemSchemaCachingProvider.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.model.util.repo;
9
10 import java.io.File;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStreamWriter;
17 import java.io.StringBufferInputStream;
18
19 import com.google.common.base.Function;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Preconditions;
22
23 public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
24
25     private final File storageDirectory;
26     private final Function<I, String> transformationFunction;
27
28     public FilesystemSchemaCachingProvider(AdvancedSchemaSourceProvider<I> delegate, File directory,
29             Function<I, String> transformationFunction) {
30         super(delegate);
31         this.storageDirectory = directory;
32         this.transformationFunction = transformationFunction;
33     }
34
35     @Override
36     protected synchronized Optional<InputStream> cacheSchemaSource(SourceIdentifier identifier, Optional<I> source) {
37         File schemaFile = toFile(identifier);
38         try {
39             if (source.isPresent() && schemaFile.createNewFile()) {
40                 try (FileOutputStream outStream = new FileOutputStream(schemaFile);
41                         OutputStreamWriter writer = new OutputStreamWriter(outStream);) {
42                     writer.write(transformToString(source.get()));
43                     writer.flush();
44                 } catch (IOException e) {
45
46                 }
47             }
48         } catch (IOException e) {
49
50         }
51         return transformToStream(source);
52     }
53
54     @SuppressWarnings("deprecation")
55     private Optional<InputStream> transformToStream(Optional<I> source) {
56         if (source.isPresent()) {
57             return Optional.<InputStream> of(new StringBufferInputStream(transformToString(source.get())));
58         }
59         return Optional.absent();
60     }
61
62     private String transformToString(I input) {
63         return transformationFunction.apply(input);
64     }
65
66     @Override
67     protected Optional<InputStream> getCachedSchemaSource(SourceIdentifier identifier) {
68         File inputFile = toFile(identifier);
69         try {
70             if (inputFile.exists() && inputFile.canRead()) {
71                 InputStream stream = new FileInputStream(inputFile);
72                 return Optional.of(stream);
73             }
74         } catch (FileNotFoundException e) {
75             return Optional.absent();
76         }
77         return Optional.absent();
78     }
79
80     private File toFile(SourceIdentifier identifier) {
81         return new File(storageDirectory, identifier.toYangFilename());
82     }
83
84     private static final Function<String, String> NOOP_TRANSFORMATION = new Function<String, String>() {
85         @Override
86         public String apply(String input) {
87             return input;
88         }
89     };
90
91     public static FilesystemSchemaCachingProvider<String> createFromStringSourceProvider(
92             SchemaSourceProvider<String> liveProvider, File directory) {
93         Preconditions.checkNotNull(liveProvider);
94         Preconditions.checkNotNull(directory);
95         directory.mkdirs();
96         return new FilesystemSchemaCachingProvider<String>(
97                 SchemaSourceProviders.toAdvancedSchemaSourceProvider(liveProvider),//
98                 directory, // 
99                 NOOP_TRANSFORMATION);
100     }
101 }