View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ******************************************************************************/
17  package net.sourceforge.jarduino.message;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.nio.charset.StandardCharsets;
24  
25  import net.sourceforge.jarduino.util.ArduinoLogManager;
26  import net.sourceforge.jarduino.util.ArduinoLogger;
27  
28  /**
29   * Library resources.
30   */
31  public final class ArduinoLibrary {
32      /**
33       * The LOGGER.
34       */
35      private static final ArduinoLogger LOGGER = ArduinoLogManager.getLogger(ArduinoLibrary.class);
36  
37      /**
38       * The library source.
39       */
40      public static final String CODE = "jarduino.cpp";
41  
42      /**
43       * The library header.
44       */
45      public static final String HEADER = "jarduino.h";
46  
47      /**
48       * The library template.
49       */
50      public static final String TEMPLATE = "template.";
51  
52      /**
53       * Private Constructor.
54       */
55      private ArduinoLibrary() {
56      }
57  
58      /**
59       * Load code to String.
60       * @return the loaded code
61       */
62      public static String loadLibraryCode() {
63          return loadResourceToString(CODE);
64      }
65  
66      /**
67       * Load header to String.
68       * @return the loaded header
69       */
70      public static String loadLibraryHeader() {
71          return loadResourceToString(HEADER);
72      }
73  
74      /**
75       * Load template to String.
76       * @param pSuffix the template suffix
77       * @return the loaded template
78       */
79      public static String loadLibraryTemplate(final String pSuffix) {
80          return loadResourceToString(TEMPLATE + pSuffix);
81      }
82  
83      /**
84       * Load code to String.
85       * @param pFile the file to load
86       * @return the loaded code
87       */
88      private static String loadResourceToString(final String pFile) {
89          /* Protect against exceptions */
90          try (InputStream myStream = ArduinoLibrary.class.getResourceAsStream(pFile);
91               InputStreamReader myInputReader = new InputStreamReader(myStream, StandardCharsets.UTF_8);
92               BufferedReader myReader = new BufferedReader(myInputReader)) {
93  
94              /* Reset the builder */
95              final StringBuilder myBuilder = new StringBuilder();
96  
97              /* Read the header entry */
98              boolean bXtra = false;
99              for (;;) {
100                 /* Read next line */
101                 final String myLine = myReader.readLine();
102                 if (myLine == null) {
103                     break;
104                 }
105 
106                 /* Add to the string buffer */
107                 if (bXtra) {
108                     myBuilder.append(ArduinoChar.LF);
109                 }
110                 myBuilder.append(myLine);
111                 bXtra = true;
112             }
113 
114             /* Build the string */
115             return myBuilder.toString();
116 
117             /* Catch exceptions */
118         } catch (IOException e) {
119             /* Throw an exception */
120             LOGGER.fatal("Failed to load resource: " + pFile, e);
121             return "<FAIL>";
122         }
123     }
124 }