public static NestedIOException wrap(IOException e){ return new NestedIOException(e); }
private File getDir(Resource resource) { File file = null; try { file = resource.getFile(); } catch (NestedIOException e) { if (!(e.getIOException() instanceof FileNotFoundException)) { throw e; } } return file; }
public IOException getIOException(){ return (IOException)getCause(); }
public static void wrapAndThrow(IOException e){ throw new NestedIOException(e); }
@Override public File getFile() throws IOException { try{ File file = wrapped.getFile(); if(null == file) { throw new FileNotFoundException("Resource '" + getDescription() + "' is not a file"); } return file; }catch (NestedIOException e) { throw e.getIOException(); } }
public static NestedIOException wrap(String message,IOException e){ return new NestedIOException(message,e); }
private void addWatchDir(List<File> dirs, Resource resource) { if(null == resource || !resource.exists()) { return; } File file = null; try { file = resource.getFile(); } catch (NestedIOException e) { if (!(e.getIOException() instanceof FileNotFoundException)) { throw e; } } if(null != file && file.isDirectory()) { dirs.add(file); } }
public static void wrapAndThrow(String message,IOException e){ throw new NestedIOException(message,e); }
public static XmlReader createReader(File file) { try{ return createStaxReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8),file.getAbsolutePath()); }catch(IOException e){ throw new NestedIOException(e); } }
@Override public void run() { try { scan(result,rootResourceToScan,rootDirPath,subPattern); } catch (IOException e) { throw new NestedIOException(e); } } }));
public static XmlDocument load(File file) { InputStreamReader reader = null; try{ reader = new InputStreamReader(new FileInputStream(file),Charsets.UTF_8); Document doc = parseDocument(reader); return new DomDocument(doc,file.getAbsolutePath()); }catch(IOException e){ throw new NestedIOException(e.getMessage(),e); }finally{ IO.close(reader); } }
public static XmlReader createReader(Resource resource) { try{ String resourceLocation = resource.toString(); return createStaxReader(new InputStreamReader(resource.getInputStream(), Charsets.UTF_8),resourceLocation); }catch(IOException e){ throw new NestedIOException(e); } }
@Override public Resource[] searchUrls(String... locationPatterns) { Args.notNull(locationPatterns,"location patterns"); Set<Resource> resources = new LinkedHashSet<Resource>(); for(String locationPattern : locationPatterns){ try { for(int i=0;i<values.length;i++){ Resource resource = values[i]; if(Resources.matcher.match(locationPattern,resource.getURL().toExternalForm())){ resources.add(resource); } } } catch (IOException e) { throw new NestedIOException(e); } } return resources.toArray(new Resource[resources.size()]); }
@Override public void run() { try { final DefaultResourceScanner resourceScanner = new DefaultResourceScanner(loader); resourceScanner.setExecutorService(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())); Collections2.addAll(result, resourceScanner.scan(locationPattern)); resourceScanner.getExecutorService().shutdownNow(); } catch (IOException e) { throw new NestedIOException(e); } } }));
/** * scan all the resources match the given location pattern. * * @see ResourceScanner#scan(String) * * @throws NestedIOException if an {@link IOException} thrown by underlying scanner. */ public static ResourceSet scan(String locationPattern) throws NestedIOException { try { return new SimpleResourceSet(scanner.scan(locationPattern)); } catch (IOException e) { throw new NestedIOException(e); } }
private static Properties readProperties(Resource resource) { InputStream is = null; try{ is = resource.getInputStream(); Properties properties = new Properties(); if(Strings.endsWithIgnoreCase(resource.getFilename(),".xml")){ properties.loadFromXML(is); }else{ properties.load(is); } return properties; }catch(IOException e){ throw new NestedIOException(e); }finally{ IO.close(is); } }
public static Resource getResource(Resource current,String path) throws NestedIOException{ Args.notNull(current,"current resource"); Args.notNull(path,"resource path"); try { if(path.indexOf(':') > 0 || path.startsWith("/")){ return Resources.getResource(path); }else{ return current.createRelative(path); } } catch (IOException e) { throw new NestedIOException(e); } }
/** * Saving the given {@link Properties} to the given file use default charset {@link Charsets#defaultCharset()}. * * @throws NestedIOException if an I/O error occurs. */ public static void save(Properties props, File file) throws NestedIOException { Args.notNull(props,"properties"); Args.notNull(file,"file"); try{ if(!file.exists()){ file.createNewFile(); } if(Strings.endsWithIgnoreCase(file.getName(), ".xml")){ try(OutputStream out = new FileOutputStream(file)){ props.storeToXML(out, ""); } }else{ try(Writer writer = IO.createWriter(file)){ props.store(writer, ""); } } }catch(IOException e){ throw new NestedIOException("Error saving properties file '" + file.getAbsolutePath() + "', " + e.getMessage(), e); } }
public static XmlDocument load(Resource resource) throws NestedIOException { InputStreamReader reader = null; try{ reader = resource.getInputStreamReader(); Document doc = parseDocument(reader); return new DomDocument(doc,null == resource.getURL() ? null : resource.getURL().toString()); }catch(IOException e){ throw new NestedIOException(e.getMessage(),e); }finally{ IO.close(reader); } }
/** * Reads the given properties file. * * @throws NestedIOException if an I/O error occurs. */ public static ExProperties load(File file,Properties defaults) throws NestedIOException { Args.notNull(file,"file"); try { ExProperties props = null == defaults ? new ExProperties(file) : new ExProperties(file,defaults); if(Strings.endsWithIgnoreCase(file.getName(), ".xml")){ try(InputStream in = new FileInputStream(file)){ props.loadFromXML(in); } }else{ try(Reader reader = IO.createReader(file,Charset.defaultCharset())){ props.load(reader); } } return props; } catch (IOException e) { throw new NestedIOException("Error loading properties file '" + file.getAbsolutePath() + "', " + e.getMessage(), e); } }