private void copyToDest(String base, String folder, String dest, List<String> res) throws IOException { for (File f : new File(folder).listFiles()) { if (f.isDirectory()) { copyToDest(base, f.getAbsolutePath(), Utilities.path(dest, f.getName()), res); } else { String dst = Utilities.path(dest, f.getName()); FileUtils.copyFile(f, new File(dst), true); res.add(f.getAbsolutePath().substring(base.length()+1)); } } }
public void addFolder(String actualDir, String statedDir, boolean omitIfExists) throws IOException { File fd = new CSFile(actualDir); String files[] = fd.list(); for (String f : files) { if (new CSFile(Utilities.path(actualDir, f)).isDirectory()) addFolder(Utilities.path(actualDir, f), Utilities.pathURL(statedDir, f), omitIfExists); else addFileName(Utilities.pathURL(statedDir, f), Utilities.path(actualDir, f), omitIfExists); } }
private void loadTransforms(String xsltDir) throws FileNotFoundException, IOException { Map<String, byte[]> res = new HashMap<String, byte[]>(); for (String s : new File(xsltDir).list()) { if (s.endsWith(".xslt")) res.put(s, TextFile.fileToBytes(Utilities.path(xsltDir, s))); } this.transforms = res; }
public void addFolder(String actualDir, String statedDir, boolean omitIfExists, String noExt) throws IOException { File fd = new CSFile(actualDir); String files[] = fd.list(); for (String f : files) { if (new CSFile(Utilities.path(actualDir, f)).isDirectory()) addFolder(Utilities.path(actualDir, f), Utilities.pathURL(statedDir, f), omitIfExists, noExt); else if (noExt == null || !f.endsWith(noExt)) addFileName(Utilities.pathURL(statedDir, f), Utilities.path(actualDir, f), omitIfExists); } }
public NpmPackage(String path) throws IOException { this.path = path; if (path != null) { for (String f : sorted(new File(path).list())) { if (new File(Utilities.path(path, f)).isDirectory()) { folders.add(f); } } npm = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(Utilities.path(path, "package", "package.json"))); cache = new IniFile(Utilities.path(path, "cache.ini")); } }
private void loadSchemas(String dir, String[] names) throws FileNotFoundException, IOException { Map<String, byte[]> res = new HashMap<String, byte[]>(); for (String s : new File(dir).list()) { if (s.endsWith(".sch")) res.put(s, TextFile.fileToBytes(Utilities.path(dir, s))); boolean ok = false; for (String b : names) ok = ok || b.equals(s); if (ok) res.put(s, TextFile.fileToBytes(Utilities.path(dir, s))); } this.schemas = res; }
public static void copyFileToDirectory(File source, File destDir) throws IOException { copyFile(source, new File(path(destDir.getAbsolutePath(), source.getName()))); }
private String srcFor(String corePrefix, String filename) throws IOException { if (inLineGraphics) { if (files.containsKey(filename)) return files.get(filename); StringBuilder b = new StringBuilder(); b.append("data: image/png;base64,"); byte[] bytes; File file = new File(Utilities.path(dest, filename)); if (!file.exists()) // because sometime this is called real early before the files exist. it will be built again later because of this bytes = new byte[0]; else bytes = FileUtils.readFileToByteArray(file); b.append(new String(Base64.encodeBase64(bytes))); // files.put(filename, b.toString()); return b.toString(); } else return corePrefix+filename; }
/** * Accessing the contents of the package - get a list of files in a subfolder of the package * * @param folder * @return * @throws IOException */ public List<String> list(String folder) throws IOException { List<String> res = new ArrayList<String>(); if (path != null) { File f = new File(Utilities.path(path, folder)); if (f.exists() && f.isDirectory()) for (String s : f.list()) res.add(s); } else { for (String s : content.keySet()) { if (s.startsWith(folder+"/") && !s.substring(folder.length()+2).contains("/")) res.add(s.substring(folder.length()+1)); } } return res; }
private static void LoadDirectory(String server, String file, IParser p, int size) throws IOException, Exception { // LoadZipFile(server, Utilities.path(file, "Patient.json.zip"), p, size, 1000, -1); // LoadZipFile(server, Utilities.path(file, "Binary.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "DocumentReference.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Encounter.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Organization.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Procedure.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "AllergyIntolerance.json.zip"), p, size, 1500, -1); // LoadZipFile(server, Utilities.path(file, "Condition.json.zip"), p, size, 0, -1); LoadZipFile(server, Utilities.path(file, "Immunization.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "MedicationStatement.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Observation-res.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Observation-sh.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Observation-vs.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "Observation-gen.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "List.json.zip"), p, size, 6500, -1); // LoadZipFile(server, Utilities.path(file, "List-res.json.zip"), p, size, 0, -1); // LoadZipFile(server, Utilities.path(file, "List-vs.json.zip"), p, size, 0, -1); }
public static void deleteTempFiles() throws IOException { File file = createTempFile("test", "test"); String folder = getDirectoryForFile(file.getAbsolutePath()); String[] list = new File(folder).list(new FilenameFilter() { public boolean accept(File dir, String name) { return name.startsWith("ohfu-"); } }); if (list != null) { for (String n : list) { new File(path(folder, n)).delete(); } } }
private String cachedFetch(String id, String source) throws IOException { String tmpDir = System.getProperty("java.io.tmpdir"); String local = Utilities.path(tmpDir, id); File f = new File(local); if (f.exists()) return TextFile.fileToString(f); URL url = new URL(source); URLConnection c = url.openConnection(); String result = TextFile.streamToString(c.getInputStream()); TextFile.stringToFile(result, f); return result; }
/** * get a stream that contains the contents of one of the files in a folder * * @param folder * @param file * @return * @throws IOException */ public InputStream load(String folder, String file) throws IOException { if (content.containsKey(folder+"/"+file)) return new ByteArrayInputStream(content.get(folder+"/"+file)); else { File f = new File(Utilities.path(path, folder, file)); if (f.exists()) return new FileInputStream(f); throw new IOException("Unable to find the file "+folder+"/"+file+" in the package "+name()); } }
public void close() throws IOException { ZipGenerator zip = new ZipGenerator(filename); for (String rn : sorted(outputs.keySet())) { ResourceInfo ri = outputs.get(rn); ri.writer.flush(); ri.writer.close(); ri.stream.close(); zip.addStream(rn+".ndjson", new FileInputStream(Utilities.path(scratch, rn+".ndjson")), false); } zip.close(); }
public String generate(StructureDefinition sd) throws FHIRException, IOException { String name = sd.hasName() ? Utilities.titleize(sd.getName().replace(".", "").replace("-", "").replace("\"", "")).replace(" ", "") : ""; if (!Utilities.nmtokenize(name).equals(name)) { System.out.println("Cannot generate Java code for profile "+sd.getUrl()+" because the name \""+name+"\" is not a valid Java class name"); return null; } File destFile = new File(Utilities.path(folder, name+".java")); OutputStreamWriter dest = new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8"); dest.write("package "+packageName+";\r\n"); dest.write("\r\n"); dest.write("import org.hl7.fhir.r4.model.ProfilingWrapper;\r\n"); dest.write("\r\n"); dest.write("public class "+name+" {\r\n"); dest.write("\r\n"); dest.write("}\r\n"); dest.flush(); dest.close(); return destFile.getAbsolutePath(); }
@Override public Source resolve(String href, String base) throws TransformerException { try { if (href.startsWith("http://") || href.startsWith("https://")) { if (alt != null) { Source s = alt.resolve(href, base); if (s != null) return s; } return TransformerFactory.newInstance().getURIResolver().resolve(href, base); } else return new StreamSource(new FileInputStream(href.contains(File.separator) ? href : Utilities.path(path, href))); } catch (FileNotFoundException e) { throw new TransformerException(e); } catch (IOException e) { throw new TransformerException(e); } }
public void addFilesFiltered(String actualDir, String ext, String[] noExt) throws IOException { File f = new CSFile(actualDir); String files[] = f.list(); for (int i = 0; i < files.length; i++) { if ( new CSFile(actualDir + files[i]).isFile() && ((ext == null || files[i].endsWith(ext)))) { boolean ok = true; for (String n : noExt) { ok = ok && !files[i].endsWith(n); } if (ok) { addFile(Utilities.path(actualDir, files[i])); } } } }
public void storeResource(MetadataResource md) throws IOException { synchronized (lock) { canonicals.put(md.getUrl(), md); if (md.hasVersion()) canonicals.put(md.getUrl()+"|"+md.getVersion(), md); } if (cacheFolder != null) { FileOutputStream s = new FileOutputStream(Utilities.path(cacheFolder, makeFileName(md.getUrl()+"|"+md.getVersion()))); context.newXmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, md); s.close(); } }
@Override public ValueSetExpansionOutcome expand(ValueSet source, Parameters expParams) throws ETooCostly, IOException { String cacheKey = makeCacheKey(source, expParams); if (expansions.containsKey(cacheKey)) return expansions.get(cacheKey); ValueSetExpander vse = new ValueSetExpanderSimple(context); ValueSetExpansionOutcome vso = vse.expand(source, expParams); if (vso.getError() != null) { // well, we'll see if the designated server can expand it, and if it can, we'll cache it locally vso = context.expandVS(source, false, expParams == null || !expParams.getParameterBool("excludeNested")); if (cacheFolder != null) { FileOutputStream s = new FileOutputStream(Utilities.path(cacheFolder, makeFileName(source.getUrl()))); context.newXmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, vso.getValueset()); s.close(); } } if (vso.getValueset() != null) expansions.put(cacheKey, vso); return vso; }
@Override public ValueSetExpansionOutcome expand(ValueSet source) throws Exception { if (expansions.containsKey(source.getUrl())) return expansions.get(source.getUrl()); ValueSetExpander vse = new ValueSetExpanderSimple(context, ValueSetExpansionCache.this); ValueSetExpansionOutcome vso = vse.expand(source); if (vso.getError() != null) { // well, we'll see if the designated server can expand it, and if it can, we'll cache it locally vso = context.expandVS(source, false); FileOutputStream s = new FileOutputStream(Utilities.path(cacheFolder, makeFile(source.getUrl()))); context.newXmlParser().setOutputStyle(IParser.OutputStyle.PRETTY).compose(s, vso.getValueset()); s.close(); } expansions.put(source.getUrl(), vso); return vso; }