3333import java .util .Map ;
3434import java .util .Map .Entry ;
3535import java .util .Set ;
36+ import java .util .logging .Level ;
37+ import java .util .logging .Logger ;
3638
3739import org .apache .clerezza .commons .rdf .Graph ;
3840import org .apache .clerezza .commons .rdf .IRI ;
4143import org .apache .clerezza .rdf .core .serializedform .Parser ;
4244import org .apache .clerezza .rdf .core .serializedform .Serializer ;
4345import org .apache .clerezza .rdf .ontologies .DCTERMS ;
44- import org .json .simple .JSONArray ;
4546import org .json .simple .JSONObject ;
4647import org .json .simple .parser .JSONParser ;
4748import org .json .simple .parser .ParseException ;
@@ -53,8 +54,6 @@ public class RepositoryProcessor {
5354 private final Parser parser = Parser .getInstance ();
5455
5556 private boolean supressFileExtension ;
56-
57- private final URL apiBaseURI ;
5857 private final Set <BranchProcessor > branchProcessors = new HashSet <>();
5958 private final Repository repository ;
6059
@@ -65,7 +64,7 @@ class BranchProcessor {
6564 private FileStorage fileStorage = new FileStorage ();
6665 private final String branch ;
6766
68- private BranchProcessor (String branch ) throws IOException , ParseException {
67+ private BranchProcessor (String branch ) throws IOException {
6968 this .branch = branch ;
7069 processBranch ();
7170 }
@@ -96,7 +95,7 @@ protected void loadStuffToGraph(byte[] content, String path, String rdfType) thr
9695 }
9796 }
9897
99- private void processFile (String path ) throws IOException , ParseException {
98+ private void processFile (String path ) throws IOException {
10099 String rdfType = getRdfFormat (path );
101100 byte [] content = repository .getContent (path );
102101 if (content == null ) {
@@ -110,13 +109,13 @@ private void processFile(String path) throws IOException, ParseException {
110109 }
111110 }
112111
113- private void processBranch () throws IOException , ParseException {
112+ private void processBranch () throws IOException {
114113 synchronized (repository ) {
115114 repository .useBranch (branch );
116115 baseIRI = getBaseIRI (repository .getContent ("BASEURI" ));
117116 for (String path : repository .getPaths ()) {
118117 processFile (path );
119- };
118+ }
120119 Graph repoGraph = new SimpleGraph ();
121120 repoGraph .add (new TripleImpl (getBranchIRI (), Ontology .latestCommit , new IRI (repository .getCommitURL ())));
122121 repoGraph .add (new TripleImpl (getBranchIRI (), Ontology .repository , getRepoIRI ()));
@@ -137,13 +136,18 @@ IRI constructFileBaseIRI(String path) {
137136 return new IRI (baseIRI .getUnicodeString () + path );
138137 }
139138
140- private IRI getBaseIRI (byte [] contentBytes ) throws IOException , ParseException {
139+ private IRI getBaseIRI (byte [] contentBytes ) throws IOException {
141140 if (contentBytes != null ) {
142141 String content = new String (contentBytes , "UTF-8" );
143142 if (content .trim ().charAt (0 ) == '{' ) {
144- JSONParser jsonParser = new JSONParser ();
145- JSONObject contentObject = (JSONObject ) jsonParser .parse (content );
146- return new IRI ((String ) contentObject .get (branch ));
143+ try {
144+ JSONParser jsonParser = new JSONParser ();
145+ JSONObject contentObject = (JSONObject ) jsonParser .parse (content );
146+ return new IRI ((String ) contentObject .get (branch ));
147+ } catch (ParseException ex ) {
148+ Logger .getLogger (RepositoryProcessor .class .getName ()).log (Level .SEVERE , "Couln'd parse BASEURI, usind default base IRI" , ex );
149+ return getDefaultBaseIRI ();
150+ }
147151 } else {
148152 return new IRI (content .trim ());
149153 }
@@ -155,16 +159,10 @@ private IRI getBaseIRI(byte[] contentBytes) throws IOException, ParseException {
155159 }
156160
157161 RepositoryProcessor (String repository , String token , boolean supressFileExtension ) throws IOException {
158- String apiBaseURIString = "https://api.github.com/repos/" + repository + "/" ;
159- this .apiBaseURI = new URL (apiBaseURIString );
160162 this .repositoryName = repository ;
161163 this .repository = new Repository (repository , token );
162164 this .supressFileExtension = supressFileExtension ;
163- try {
164- processRepository ();
165- } catch (ParseException ex ) {
166- throw new IOException (ex );
167- }
165+ processRepository ();
168166 }
169167
170168 public static void main (String [] args ) throws Exception {
@@ -196,9 +194,9 @@ public Map<IRI, Graph> getGraphs() {
196194 return result ;
197195 }
198196
199- private void processRepository () throws IOException , ParseException {
197+ private void processRepository () throws IOException {
200198 System .out .println ("Loading RDF data from " + repositoryName );
201- String [] branches = getBranches ();
199+ String [] branches = repository . getBranches ();
202200 for (String branch : branches ) {
203201 BranchProcessor branchProcessor = new BranchProcessor (branch );
204202 branchProcessors .add (branchProcessor );
@@ -245,13 +243,5 @@ private String getRdfFormat(String path) {
245243 return null ;
246244 }
247245
248- private String [] getBranches () throws IOException {
249- JSONArray jsonArray = repository .getJsonArray (new URL (apiBaseURI , "branches" ));
250- Set <String > resultSet = new HashSet <>();
251- jsonArray .forEach ((obj ) -> {
252- resultSet .add ((String ) ((JSONObject ) obj ).get ("name" ));
253- });
254- return resultSet .toArray (new String [resultSet .size ()]);
255- }
256246
257247}
0 commit comments