/** * ProcessFromHref first tries to create an {@link URL} from the given <code>href</code>, * if that throws a {@link MalformedURLException}, it will prepend the given * root URLs to <code>href</code> until a valid URL is found.<br />If by then there is * no valid url found, this method will see if the given <code>href</code> is a valid file * and can read it.<br />If it's not a valid file or a file that can't be read, * the given root directories will be set as root path with the given <code>href</code> as * file path until a valid file has been found. */ virtual public void ProcessFromHref(String href, IReadingProcessor processor) { if (LOGGER.IsLogging(Level.DEBUG)) { LOGGER.Debug(String.Format(LocaleMessages.GetInstance().GetMessage("retrieve.file.from"), href)); } Uri url = null; bool isfile = false; string f = href; try { url = new Uri(href); } catch (UriFormatException) { try { url = DetectWithRootUrls(href); } catch (UriFormatException) { // its probably a file, try to detect it. isfile = true; if (!(File.Exists(href))) { isfile = false; foreach (string root in rootdirs) { f = Path.Combine(root, href); if (File.Exists(f)) { isfile = true; break; } } } } } Stream inp = null; if (null != url) { WebRequest w = WebRequest.Create(url); try { inp = w.GetResponse().GetResponseStream(); } catch (WebException) { throw new IOException(LocaleMessages.GetInstance().GetMessage("retrieve.file.from.nothing")); } } else if (isfile) { inp = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.Read); } else { throw new IOException(LocaleMessages.GetInstance().GetMessage("retrieve.file.from.nothing")); } Read(processor, inp); }
/** * @param processor * @param in * @throws IOException */ private void Read(IReadingProcessor processor, Stream inp) { try { int inbit = -1; while ((inbit = inp.ReadByte()) != -1) { processor.Process(inbit); } } catch (IOException e) { throw e; } finally { try { if (null != inp) { inp.Close(); } } catch (IOException e) { throw new RuntimeWorkerException(e); } } }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.net.FileRetrieve#processFromStream(java.io.Stream, com.itextpdf.tool.xml.net.ReadingProcessor) */ public void ProcessFromStream(Stream inp, IReadingProcessor processor) { Read(processor, inp); }
/** * ProcessFromHref first tries to create an {@link URL} from the given <code>href</code>, * if that throws a {@link MalformedURLException}, it will prepend the given * root URLs to <code>href</code> until a valid URL is found.<br />If by then there is * no valid url found, this method will see if the given <code>href</code> is a valid file * and can read it.<br />If it's not a valid file or a file that can't be read, * the given root directories will be set as root path with the given <code>href</code> as * file path until a valid file has been found. */ public void ProcessFromHref(String href, IReadingProcessor processor) { if (LOGGER.IsLogging(Level.DEBUG)) { LOGGER.Debug(String.Format(LocaleMessages.GetInstance().GetMessage("retrieve.file.from"), href)); } Uri url = null; bool isfile = false; string f = href; try { url = new Uri(href); } catch (UriFormatException) { try { url = DetectWithRootUrls(href); } catch (UriFormatException) { // its probably a file, try to detect it. isfile = true; if (!(File.Exists(href))) { isfile = false; foreach (string root in rootdirs) { f = Path.Combine(root, href); if (File.Exists(f)) { isfile = true; break; } } } } } Stream inp = null; if (null != url) { WebRequest w = WebRequest.Create(url); try { inp = w.GetResponse().GetResponseStream(); } catch (WebException) { throw new IOException(LocaleMessages.GetInstance().GetMessage("retrieve.file.from.nothing")); } } else if (isfile) { inp = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.Read); } else { throw new IOException(LocaleMessages.GetInstance().GetMessage("retrieve.file.from.nothing")); } Read(processor, inp); }