/** Configure this transformer, possibly passing to it a jtidy configuration file location. */ public void configure(Configuration config) throws ConfigurationException { super.configure(config); String configUrl = config.getChild("jtidy-config").getValue(null); if (configUrl != null) { org.apache.excalibur.source.SourceResolver resolver = null; Source configSource = null; try { resolver = (org.apache.excalibur.source.SourceResolver) this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); configSource = resolver.resolveURI(configUrl); if (getLogger().isDebugEnabled()) { getLogger().debug("Loading configuration from " + configSource.getURI()); } this.properties = new Properties(); this.properties.load(configSource.getInputStream()); } catch (Exception e) { getLogger().warn("Cannot load configuration from " + configUrl); throw new ConfigurationException("Cannot load configuration from " + configUrl, e); } finally { if (null != resolver) { this.manager.release(resolver); resolver.release(configSource); } } } }
private String read(Source src) throws Exception { byte[] data = new byte[(int) src.getContentLength()]; InputStream is = src.getInputStream(); assertEquals(data.length, is.read(data)); is.close(); return new String(data, "ISO-8859-1"); }
/** Test the generateSaxFragment method. */ public void testGenerateSaxFragment() throws Exception { List beans = new ArrayList(2); beans.add(new TestBean("1", "One")); beans.add(new TestBean("2", "Two")); Map flowContextObject = new HashMap(); flowContextObject.put("beans", beans); Request request = new MockRequest(); Map objectModel = new HashMap(); FlowHelper.setContextObject(objectModel, flowContextObject); objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request); Map contextObjectModel = new HashMap(); contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectModel); Context context = new DefaultContext(contextObjectModel); Source sampleSource = new ResourceSource( "resource://org/apache/cocoon/forms/datatype/FlowJXPathSelectionListTestCase.source.xml"); Document sample = parser.parse(sampleSource.getInputStream()); Element datatypeElement = (Element) sample.getElementsByTagNameNS(FormsConstants.DEFINITION_NS, "datatype").item(0); Datatype datatype = datatypeManager.createDatatype(datatypeElement, false); FlowJXPathSelectionList list = new FlowJXPathSelectionList( context, "beans", "key", "value", datatype, null, false, null, false); DOMBuilder dest = new DOMBuilder(); list.generateSaxFragment(dest, Locale.ENGLISH); Source expectedSource = new ResourceSource( "resource://org/apache/cocoon/forms/datatype/FlowJXPathSelectionListTestCase.dest.xml"); Document expected = parser.parse(expectedSource.getInputStream()); assertEqual("Test if generated list matches expected", expected, dest.getDocument()); }
/** * Resolves the URI and returns it's contents as a String object * * @param resolver The SourceResolver * @param uri The URI * @return The URI content * @throws Exception */ private String getURIContentsAsString(SourceResolver resolver, String uri) throws Exception { Source source = resolver.resolveURI(uri); Reader reader = new InputStreamReader(source.getInputStream(), "UTF-8"); StringBuffer buf = new StringBuffer(); int rb = 0; char[] cbuf = new char[1024]; while ((rb = reader.read(cbuf)) > 0) { buf.append(cbuf, 0, rb); } return buf.toString(); }
public SourceProperty getSourceProperty(Source source, String namespace, String name) throws SourceException { if ((namespace.equals(m_namespace)) && (name.equals(m_propertyname)) && (source.getURI().endsWith(m_extension))) { DOMParser parser = null; Document doc = null; try { parser = (DOMParser) manager.lookup(DOMParser.ROLE); InputSource is = new InputSource(source.getInputStream()); is.setSystemId(source.getURI()); doc = parser.parseDocument(is); } catch (SAXException se) { getLogger().error(source.getURI() + " is not a valid XML file"); } catch (IOException ioe) { getLogger().error("Could not read file", ioe); } catch (ServiceException ce) { getLogger().error("Missing service dependency: DOMParser", ce); } finally { if (parser != null) { this.manager.release(parser); } } if (doc != null) { XPathProcessor processor = null; try { processor = (XPathProcessor) manager.lookup(XPathProcessor.ROLE); NodeList nodelist = processor.selectNodeList(doc.getDocumentElement(), m_xpath); SourceProperty property = new SourceProperty(m_namespace, m_propertyname); property.setValue(nodelist); return property; } catch (ServiceException se) { this.getLogger().error("Could not retrieve component", se); } finally { if (processor != null) { this.manager.release(processor); } } } } return null; }
/** Checks the source uri for the .jp(e)g extension. */ protected final boolean isImageMimeType(Source source) { final String uri = source.getURI(); final int index = uri.lastIndexOf('.'); if (index != -1) { String extension = uri.substring(index); return extension.equalsIgnoreCase(".jpg") || extension.equalsIgnoreCase(".JPEG"); } return false; }
/** * Force the refresh of the wrapped <code>Source</code>, even if the refresh period isn't over, * and starts a new period. * * <p>This method is thread-safe, even if the underlying Source is not. */ public final synchronized void refresh() { this.nextCheckTime = System.currentTimeMillis() + this.delay; // Refresh modifiable sources this.source.refresh(); // Keep the last modified date this.lastModified = source.getLastModified(); }
public void testCreateFirstLevelFile() throws Exception { String someText = "Some text"; JCRNodeSource root = (JCRNodeSource) resolver.resolveURI("jcr://"); JCRNodeSource firstChild = (JCRNodeSource) root.getChild("child1"); assertFalse(firstChild.exists()); assertEquals(firstChild.getURI(), "jcr://child1"); write(firstChild, someText); assertTrue(firstChild.exists()); // Check content Source child1 = resolver.resolveURI("jcr://child1"); assertTrue(child1.exists()); int len = (int) child1.getContentLength(); assertEquals(someText.length(), len); assertEquals(someText, read(child1)); }
public void testTraverseDir() throws Exception { String text = "Look Ma, more data!"; ModifiableTraversableSource dir = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/dir"); dir.makeCollection(); for (int i = 0; i < 10; i++) { ModifiableTraversableSource src = (ModifiableTraversableSource) dir.getChild("file" + i); write(src, text + i); } // Lookup dir again, and inspect children dir = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/dir"); Collection children = dir.getChildren(); assertEquals(10, children.size()); for (int i = 0; i < 10; i++) { Source src = dir.getChild("file" + i); assertTrue(src.exists()); assertEquals(text + i, read(src)); } }
/** Checks that this is in fact a jpeg file. */ protected final boolean isImageFileType(Source source) throws SourceException { BufferedInputStream in = null; try { in = new BufferedInputStream(source.getInputStream()); byte[] buf = new byte[2]; int count = in.read(buf, 0, 2); if (count < 2) return false; if ((buf[0] == (byte) 0xFF) && (buf[1] == (byte) 0xD8)) return true; } catch (IOException ioe) { throw new SourceException("Could not read source", ioe); } finally { if (in != null) try { in.close(); } catch (Exception e) { } } return false; }
/** returns width as first element, height as second */ protected final int[] getImageSize(Source source) throws SourceException { BufferedInputStream in = null; try { in = new BufferedInputStream(source.getInputStream()); // check for "magic" header byte[] buf = new byte[2]; int count = in.read(buf, 0, 2); if (count < 2) throw new SourceException("Not a valid Jpeg file!"); if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) throw new SourceException("Not a valid Jpeg file!"); int width = 0; int height = 0; boolean done = false; int ch = 0; try { while (ch != 0xDA && !done) { /* Find next marker (JPEG markers begin with 0xFF) */ while (ch != 0xFF) { ch = in.read(); } /* JPEG markers can be padded with unlimited 0xFF's */ while (ch == 0xFF) { ch = in.read(); } /* Now, ch contains the value of the marker. */ if (ch >= 0xC0 && ch <= 0xC3) { // skip 3 bytes in.read(); in.read(); in.read(); height = 256 * in.read(); height += in.read(); width = 256 * in.read(); width += in.read(); done = true; } else { /* We MUST skip variables, since FF's within variable names are NOT valid JPEG markers */ int length = 256 * in.read(); length += in.read(); if (length < 2) throw new RuntimeException("Erroneous JPEG marker length"); for (int foo = 0; foo < length - 2; foo++) in.read(); } } } catch (Exception e) { throw new SourceException("Not a valid Jpeg file!", e); } int[] dim = {width, height}; return dim; } catch (IOException ioe) { throw new SourceException("Could not read source", ioe); } finally { if (in != null) try { in.close(); } catch (Exception e) { } } }
/** * Generate the unique key. This key must be unique inside the space of this component. * * @return The generated key hashes the src */ public Serializable getKey() { return inputSource.getURI() + ";localizable=" + localizable + ";encoding=" + encoding; }