/** 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());
 }
示例#2
0
 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");
 }
示例#3
0
  /** 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);
        }
      }
    }
  }
示例#4
0
 /**
  * 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();
 }
示例#5
0
  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 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) {
        }
    }
  }