/** * Resolves dependencies (using {@code #include } in the source code) * and delegates the combined source code to * {@link #createProgramFromSourceCode(java.lang.String) }. * Important: only absolute paths are allowed. * @param sourceCode the original source code * @param assetManager the asset manager to load the files * @return the created program object * @throws AssetNotFoundException if a dependency could not be loaded */ public Program createProgramFromSourceCodeWithDependencies(String sourceCode, AssetManager assetManager) { StringBuilder builder = new StringBuilder(sourceCode.length()); BufferedReader reader = new BufferedReader(new StringReader(sourceCode)); try { buildSourcesRec(reader, builder, assetManager); } catch (IOException ex) { throw new AssetNotFoundException("Unable to read a dependency file", ex); } return createProgramFromSourceCode(builder.toString()); } private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException {
private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException { String ln; while ((ln = reader.readLine()) != null) { if (ln.trim().startsWith("#import ")) { ln = ln.trim().substring(8).trim(); if (ln.startsWith("\"")) { ln = ln.substring(1); } if (ln.endsWith("\"")) { ln = ln.substring(0, ln.length()-1); } AssetInfo info = assetManager.locateAsset(new AssetKey<String>(ln)); if (info == null) { throw new AssetNotFoundException("Unable to load source file \""+ln+"\""); } try (BufferedReader r = new BufferedReader(new InputStreamReader(info.openStream()))) { builder.append("//-- begin import ").append(ln).append(" --\n"); buildSourcesRec(r, builder, assetManager); builder.append("//-- end import ").append(ln).append(" --\n"); } } else { builder.append(ln).append('\n'); } } }
/** * Resolves dependencies (using {@code #include } in the source code) * and delegates the combined source code to * {@link #createProgramFromSourceCode(java.lang.String) }. * Important: only absolute paths are allowed. * @param sourceCode the original source code * @param assetManager the asset manager to load the files * @return the created program object * @throws AssetNotFoundException if a dependency could not be loaded */ public Program createProgramFromSourceCodeWithDependencies(String sourceCode, AssetManager assetManager) { StringBuilder builder = new StringBuilder(sourceCode.length()); BufferedReader reader = new BufferedReader(new StringReader(sourceCode)); try { buildSourcesRec(reader, builder, assetManager); } catch (IOException ex) { throw new AssetNotFoundException("Unable to read a dependency file", ex); } return createProgramFromSourceCode(builder.toString()); } private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException {
private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException { String ln; while ((ln = reader.readLine()) != null) { if (ln.trim().startsWith("#import ")) { ln = ln.trim().substring(8).trim(); if (ln.startsWith("\"")) { ln = ln.substring(1); } if (ln.endsWith("\"")) { ln = ln.substring(0, ln.length()-1); } AssetInfo info = assetManager.locateAsset(new AssetKey<String>(ln)); if (info == null) { throw new AssetNotFoundException("Unable to load source file \""+ln+"\""); } try (BufferedReader r = new BufferedReader(new InputStreamReader(info.openStream()))) { builder.append("//-- begin import ").append(ln).append(" --\n"); buildSourcesRec(r, builder, assetManager); builder.append("//-- end import ").append(ln).append(" --\n"); } } else { builder.append(ln).append('\n'); } } }