]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java
SCL-compiler should activate installed bundles
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / BundleDocumentationSource.java
1 package org.simantics.scl.osgi.internal;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.nio.charset.Charset;
7 import java.util.Arrays;
8
9 import org.osgi.framework.Bundle;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 public class BundleDocumentationSource {
14
15     private static final Logger LOGGER = LoggerFactory.getLogger(BundleDocumentationSource.class);
16
17     public static final Charset UTF8 = Charset.forName("UTF-8");  
18     
19     public final String documentationName;
20     public final Bundle bundle;
21     public final URL url;
22     
23     public BundleDocumentationSource(String documentationName, Bundle bundle,
24             URL url) {
25         this.documentationName = documentationName;
26         this.bundle = bundle;
27         this.url = url;
28     }
29
30     public String getText() {
31         try {
32             InputStream stream = url.openStream();
33             try {
34                 byte[] buffer = new byte[1024];
35                 int pos = 0;
36                 while(true) {
37                     int count = stream.read(buffer, pos, buffer.length-pos);
38                     if(count <= 0)
39                         break;
40                     pos += count;
41                     if(pos == buffer.length)
42                         buffer = Arrays.copyOf(buffer, buffer.length*2);
43                 }
44                 return new String(buffer, 0, pos, UTF8);
45             } finally {
46                 stream.close();
47             }
48         } catch(IOException e) {
49             LOGGER.error("Could not get text for {} at {}", documentationName, url);
50             return null;
51         }
52     }
53 }