Beispiel #1
0
  /** Use Staxon to convert JSON to an XML string */
  @Override
  protected Object doTransform(Object src, String enc) throws TransformerException {
    XMLInputFactory inputFactory = new JsonXMLInputFactory();
    inputFactory.setProperty(JsonXMLInputFactory.PROP_MULTIPLE_PI, false);
    TransformerInputs inputs = new TransformerInputs(this, src);
    Source source;
    try {
      if (inputs.getInputStream() != null) {
        source =
            new StAXSource(
                inputFactory.createXMLStreamReader(
                    inputs.getInputStream(), enc == null ? "UTF-8" : enc));
      } else {
        source = new StAXSource(inputFactory.createXMLStreamReader(inputs.getReader()));
      }

      XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
      return convert(source, outputFactory);
    } catch (Exception ex) {
      throw new TransformerException(this, ex);
    } finally {
      IOUtils.closeQuietly(inputs.getInputStream());
      IOUtils.closeQuietly(inputs.getReader());
    }
  }
Beispiel #2
0
 public Object doTransform(Object src, String encoding) throws TransformerException {
   byte[] buf;
   if (src instanceof String) {
     buf = src.toString().getBytes();
   } else if (src instanceof InputStream) {
     InputStream input = (InputStream) src;
     try {
       buf = IOUtils.toByteArray(input);
     } finally {
       IOUtils.closeQuietly(input);
     }
   } else {
     buf = (byte[]) src;
   }
   try {
     byte[] result = getTransformedBytes(buf);
     if (getReturnClass().equals(String.class)) {
       if (encoding != null) {
         try {
           return new String(result, encoding);
         } catch (UnsupportedEncodingException ex) {
           return new String(result);
         }
       } else {
         return new String(result);
       }
     } else {
       return result;
     }
   } catch (CryptoFailureException e) {
     throw new TransformerException(this, e);
   }
 }