/** * Retrieves the image located at the given URI. It's assumed the URI does point to an image--the * URI will be accessed (using java.io or java.net), opened, read and then passed into the JDK * image-parsing routines. The result is packed up into an ImageResource for later consumption. * * @param uri Location of the image source. * @return An ImageResource containing the image. */ public ImageResource getImageResource(String uri) { ImageResource ir; uri = resolveURI(uri); ir = (ImageResource) _imageCache.get(uri); // TODO: check that cached image is still valid if (ir == null) { InputStream is = resolveAndOpenStream(uri); if (is != null) { try { BufferedImage img = ImageIO.read(is); if (img == null) { throw new IOException("ImageIO.read() returned null"); } ir = createImageResource(uri, img); _imageCache.put(uri, ir); } catch (FileNotFoundException e) { XRLog.exception("Can't read image file; image at URI '" + uri + "' not found"); } catch (IOException e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uri + "'", e); } finally { try { is.close(); } catch (IOException e) { // ignore } } } } if (ir == null) { ir = createImageResource(uri, null); } return ir; }
private void parseHtmlTags(Document doc) { XRLog.render(Level.FINEST, "parsing (X)HTML tags ..."); parseHtmlTitleTag(doc); parseHtmlMetaTags(doc); if (XRLog.isLoggingEnabled()) { XRLog.render(Level.FINEST, "PDF info map = " + pdfInfoValues); } }
public synchronized ImageResource get(final String uri, final int width, final int height) { if (ImageUtil.isEmbeddedBase64Image(uri)) { ImageResource resource = loadEmbeddedBase64ImageResource(uri); BufferedImage newImg = ((AWTFSImage) resource.getImage()).getImage(); return new ImageResource( resource.getImageUri(), AWTFSImage.createImage(ImageUtils.scaleImage(newImg, width, height))); } else { CacheKey key = new CacheKey(uri, width, height); ImageResource ir = (ImageResource) _imageCache.get(key); if (ir == null) { // not loaded, or not loaded at target size // loaded a base size? ir = (ImageResource) _imageCache.get(new CacheKey(uri, -1, -1)); // no: loaded if (ir == null) { if (isImmediateLoadUri(uri)) { XRLog.load(Level.FINE, "Load immediate: " + uri); ir = loadImageResourceFromUri(uri); FSImage awtfsImage = ir.getImage(); BufferedImage newImg = ((AWTFSImage) awtfsImage).getImage(); loaded(ir, -1, -1); if (width > -1 && height > -1) { XRLog.load(Level.FINE, this + ", scaling " + uri + " to " + width + ", " + height); newImg = ImageUtils.scaleImage(newImg, width, height); ir = new ImageResource(ir.getImageUri(), AWTFSImage.createImage(newImg)); loaded(ir, width, height); } } else { XRLog.load(Level.FINE, "Image cache miss, URI not yet loaded, queueing: " + uri); MutableFSImage mfsi = new MutableFSImage(_repaintListener); ir = new ImageResource(uri, mfsi); _loadQueue.addToQueue(this, uri, mfsi, width, height); } _imageCache.put(key, ir); } else { // loaded at base size, need to scale XRLog.load(Level.FINE, this + ", scaling " + uri + " to " + width + ", " + height); FSImage awtfsImage = ir.getImage(); BufferedImage newImg = ((AWTFSImage) awtfsImage).getImage(); newImg = ImageUtils.scaleImage(newImg, width, height); ir = new ImageResource(ir.getImageUri(), AWTFSImage.createImage(newImg)); loaded(ir, width, height); } } return ir; } }
// TOdO:implement this with nio. protected InputStream resolveAndOpenStream(String uri) { java.io.InputStream is = null; uri = resolveURI(uri); try { is = new URL(uri).openStream(); } catch (java.net.MalformedURLException e) { XRLog.exception("bad URL given: " + uri, e); } catch (java.io.FileNotFoundException e) { XRLog.exception("item at URI " + uri + " not found"); } catch (java.io.IOException e) { XRLog.exception("IO problem for " + uri, e); } return is; }
/** Add PDF meta values to the target PDF document. */ private void addPdfMetaValuesToPdfDocument(ITextRenderer renderer) { Iterator pdfNameIter = this.pdfInfoValues.keySet().iterator(); while (pdfNameIter.hasNext()) { PdfName pdfName = (PdfName) pdfNameIter.next(); PdfString pdfString = (PdfString) pdfInfoValues.get(pdfName); XRLog.render(Level.FINEST, "pdfName=" + pdfName + ", pdfString=" + pdfString); renderer.getOutputDevice().getWriter().getInfo().put(pdfName, pdfString); } if (XRLog.isLoggingEnabled()) { XRLog.render( Level.FINEST, "added " + renderer.getOutputDevice().getWriter().getInfo().getKeys()); } }
private void parseHtmlMetaTags(Document doc) { NodeList headNodeList = doc.getDocumentElement().getElementsByTagName(HTML_TAG_HEAD); XRLog.render(Level.FINEST, "headNodeList=" + headNodeList); Element rootHeadNodeElement = (Element) headNodeList.item(0); NodeList metaNodeList = rootHeadNodeElement.getElementsByTagName(HTML_TAG_META); XRLog.render(Level.FINEST, "metaNodeList=" + metaNodeList); for (int inode = 0; inode < metaNodeList.getLength(); ++inode) { XRLog.render(Level.FINEST, "node " + inode + " = " + metaNodeList.item(inode).getNodeName()); Element thisNode = (Element) metaNodeList.item(inode); XRLog.render(Level.FINEST, "node " + thisNode); String metaName = thisNode.getAttribute(HTML_META_ATTR_NAME); String metaContent = thisNode.getAttribute(HTML_META_ATTR_CONTENT); XRLog.render(Level.FINEST, "metaName=" + metaName + ", metaContent=" + metaContent); if (metaName.length() != 0 && metaContent.length() != 0) { PdfName pdfName = null; PdfString pdfString = null; if (HTML_META_KEY_TITLE.equalsIgnoreCase(metaName) || HTML_META_KEY_DC_TITLE.equalsIgnoreCase(metaName)) { pdfName = PdfName.TITLE; pdfString = new PdfString(metaContent, PdfObject.TEXT_UNICODE); this.pdfInfoValues.put(pdfName, pdfString); } else if (HTML_META_KEY_CREATOR.equalsIgnoreCase(metaName) || HTML_META_KEY_DC_CREATOR.equalsIgnoreCase(metaName)) { pdfName = PdfName.AUTHOR; pdfString = new PdfString(metaContent, PdfObject.TEXT_UNICODE); this.pdfInfoValues.put(pdfName, pdfString); } else if (HTML_META_KEY_SUBJECT.equalsIgnoreCase(metaName) || HTML_META_KEY_DC_SUBJECT.equalsIgnoreCase(metaName)) { pdfName = PdfName.SUBJECT; pdfString = new PdfString(metaContent, PdfObject.TEXT_UNICODE); this.pdfInfoValues.put(pdfName, pdfString); } else if (HTML_META_KEY_KEYWORDS.equalsIgnoreCase(metaName)) { pdfName = PdfName.KEYWORDS; pdfString = new PdfString(metaContent, PdfObject.TEXT_UNICODE); this.pdfInfoValues.put(pdfName, pdfString); } } } }
/* * public boolean isDynamic() { * return (_pc != 0); * } */ public void setPseudoElement(String pseudoElement) { if (_pe != null) { addUnsupportedCondition(); XRLog.match(Level.WARNING, "Trying to set more than one pseudo-element"); } else { _specificityD++; _pe = pseudoElement; } }
/** * Adds a feature to the Condition attribute of the Selector object * * @param c The feature to be added to the Condition attribute */ private void addCondition(Condition c) { if (conditions == null) { conditions = new java.util.ArrayList(); } if (_pe != null) { conditions.add(Condition.createUnsupportedCondition()); XRLog.match(Level.WARNING, "Trying to append conditions to pseudoElement " + _pe); } conditions.add(c); }
public ImageResource getImageResource(String uri) { ImageResource resource = null; uri = resolveURI(uri); resource = (ImageResource) _imageCache.get(uri); if (resource == null) { InputStream is = resolveAndOpenStream(uri); if (is != null) { try { URL url = new URL(uri); if (url.getPath() != null && url.getPath().toLowerCase().endsWith(".pdf")) { PdfReader reader = _outputDevice.getReader(url); PDFAsImage image = new PDFAsImage(url); Rectangle rect = reader.getPageSizeWithRotation(1); image.setInitialWidth(rect.getWidth() * _outputDevice.getDotsPerPoint()); image.setInitialHeight(rect.getHeight() * _outputDevice.getDotsPerPoint()); resource = new ImageResource(uri, image); } else { Image image = Image.getInstance(url); scaleToOutputResolution(image); resource = new ImageResource(uri, new ITextFSImage(image)); } _imageCache.put(uri, resource); } catch (IOException e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uri + "'", e); } catch (BadElementException e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uri + "'", e); } catch (URISyntaxException e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uri + "'", e); } finally { try { is.close(); } catch (IOException e) { // ignore } } } } if (resource == null) { resource = new ImageResource(uri, null); } return resource; }
/** * Gets the appropriateSibling attribute of the Selector object * * @param e PARAM * @param treeRes * @return The appropriateSibling value */ Object getAppropriateSibling(Object e, TreeResolver treeRes) { Object sibling = null; switch (_axis) { case IMMEDIATE_SIBLING_AXIS: sibling = treeRes.getPreviousSiblingElement(e); break; default: XRLog.exception("Bad sibling axis"); } return sibling; }
/** * The image we're replacing. * * @return see desc */ public Image getImage() { if (!_loaded && _imageResource.isLoaded()) { Image image = ((AWTFSImage) _imageResource.getImage()).getImage(); if (_doScaleImage && (_targetWidth > 0 || _targetHeight > 0)) { int w = image.getWidth(null); int h = image.getHeight(null); int newW = _targetWidth; int newH = _targetHeight; if (newW == -1) { newW = (int) (w * ((double) newH / h)); } if (newH == -1) { newH = (int) (h * ((double) newW / w)); } if (w != newW || h != newH) { if (image instanceof BufferedImage) { image = ImageUtil.getScaledInstance((BufferedImage) image, newW, newH); } else { if (true) { throw new RuntimeException( "image is not a buffered image! " + _imageResource.getImageUri()); } String scalingType = Configuration.valueFor("xr.image.scale", "HIGH").trim(); if (scalingType.equalsIgnoreCase("HIGH") || scalingType.equalsIgnoreCase("MID")) { image = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); } else { image = image.getScaledInstance(newW, newH, Image.SCALE_FAST); } } } _image = image; } else { _image = image; } _loaded = true; XRLog.load( Level.FINE, "Icon: replaced image " + _imageResource.getImageUri() + ", repaint requested"); SwingUtilities.invokeLater( new Runnable() { public void run() { repaintListener.repaintRequested(_doScaleImage); } }); } return _image; }
/** * Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the * baseUrl for the agent. * * @param uri A URI, possibly relative. * @return A URI as String, resolved, or null if there was an exception (for example if the URI is * malformed). */ public String resolveURI(String uri) { if (uri == null) return null; String ret = null; if (_baseURL == null) { // first try to set a base URL try { URL result = new URL(uri); setBaseURL(result.toExternalForm()); } catch (MalformedURLException e) { try { setBaseURL(new File(".").toURI().toURL().toExternalForm()); } catch (Exception e1) { XRLog.exception( "The default NaiveUserAgent doesn't know how to resolve the base URL for " + uri); return null; } } } // test if the URI is valid; if not, try to assign the base url as its parent try { return new URL(uri).toString(); } catch (MalformedURLException e) { XRLog.load( "Could not read " + uri + " as a URL; may be relative. Testing using parent URL " + _baseURL); try { URL result = new URL(new URL(_baseURL), uri); ret = result.toString(); } catch (MalformedURLException e1) { XRLog.exception( "The default NaiveUserAgent cannot resolve the URL " + uri + " with base URL " + _baseURL); } } return ret; }
public static ImageResource loadImageResourceFromUri(final String uri) { if (ImageUtil.isEmbeddedBase64Image(uri)) { return loadEmbeddedBase64ImageResource(uri); } else { StreamResource sr = new StreamResource(uri); InputStream is; ImageResource ir = null; try { sr.connect(); is = sr.bufferedStream(); try { BufferedImage img = ImageIO.read(is); if (img == null) { throw new IOException("ImageIO.read() returned null"); } ir = createImageResource(uri, img); } catch (FileNotFoundException e) { XRLog.exception("Can't read image file; image at URI '" + uri + "' not found"); } catch (IOException e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uri + "'", e); } finally { sr.close(); } } catch (IOException e) { // couldnt open stream at URI... XRLog.exception("Can't open stream for URI '" + uri + "': " + e.getMessage()); } // When we do this we return null which will raise an exception on layout/render, which // will allow us to use the fallback discover page in cases when the network/site are // unavailable // if (ir == null) { // ir = createImageResource(uri, null); // } return ir; } }
private void parseHtmlTitleTag(Document doc) { NodeList headNodeList = doc.getDocumentElement().getElementsByTagName(HTML_TAG_HEAD); XRLog.render(Level.FINEST, "headNodeList=" + headNodeList); Element rootHeadNodeElement = (Element) headNodeList.item(0); NodeList titleNodeList = rootHeadNodeElement.getElementsByTagName(HTML_TAG_TITLE); XRLog.render(Level.FINEST, "titleNodeList=" + titleNodeList); Element titleElement = (Element) titleNodeList.item(0); if (titleElement != null) { XRLog.render(Level.FINEST, "titleElement=" + titleElement); XRLog.render(Level.FINEST, "titleElement.name=" + titleElement.getTagName()); XRLog.render(Level.FINEST, "titleElement.value=" + titleElement.getNodeValue()); XRLog.render(Level.FINEST, "titleElement.content=" + titleElement.getTextContent()); String titleContent = titleElement.getTextContent(); PdfName pdfName = PdfName.TITLE; PdfString pdfString = new PdfString(titleContent); this.pdfInfoValues.put(pdfName, pdfString); } }
/** * PDFCreationListener onClose event handler. * * @see PDFCreationListener */ public void onClose(ITextRenderer renderer) { XRLog.render(Level.FINEST, "handling onClose event ..."); addPdfMetaValuesToPdfDocument(renderer); }
public void repaintRequested(boolean doLayout) { XRLog.general(Level.FINE, "No-op repaint requested"); }
public void stopLoading() { if (_loadQueue != null) { XRLog.load("By request, clearing pending items from load queue: " + _loadQueue.size()); _loadQueue.reset(); } }