Java Code Examples for javax.xml.transform.Transformer#reset()

The following examples show how to use javax.xml.transform.Transformer#reset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FHIRXMLGenerator.java    From FHIR with Apache License 2.0 5 votes vote down vote up
private void writeXhtml(java.lang.String elementName, Xhtml xhtml) {
    try {
        Transformer transformer = THREAD_LOCAL_TRANSFORMER.get();
        transformer.reset();
        transformer.transform(new StreamSource(new StringReader(xhtml.getValue())), new StAXResult(createNonClosingStreamWriterDelegate(writer)));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: DcUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
/**
 * MODS -> DC transformer.
 * 
 * @param model fedora object model
 * @return transformer
 * @throws TransformerConfigurationException 
 */
public static Transformer modsTransformer(String model) throws TransformerConfigurationException {
    Transformer t = defaultModsTransformer.get();
    if (t == null) {
        t = MODS2DC.newTransformer();
        defaultModsTransformer.set(t);
    } else {
        t.reset();
    }
    t.setParameter("MODEL", model);
    return t;
}
 
Example 3
Source File: Xml.java    From RADL with Apache License 2.0 5 votes vote down vote up
public static void transform(Source source, Result destination) {
  Transformer transformer = getTransformer();
  try {
    transformer.transform(source, destination);
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    transformer.reset();
  }
}
 
Example 4
Source File: Xml.java    From RADL with Apache License 2.0 5 votes vote down vote up
public static void identityTransform(Document source, File destination) throws XmlException {
  try (FileWriter writer = new FileWriter(destination, false)) {
    Transformer transformer = getTransformer();
    try {
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.transform(new DOMSource(source), new StreamResult(writer));
    } finally {
      transformer.reset();
    }
  } catch (Exception e) {
    throw new XmlException(e);
  }
}
 
Example 5
Source File: Method.java    From jlibs with Apache License 2.0 4 votes vote down vote up
private boolean execute(List<String> args) throws Exception{
    File responseFile = getFile(args, ">");

    HttpURLConnection con = prepare(args);
    if(con==null)
        return false;

    if(con.getResponseCode()==401){ // Unauthorized
        if(authenticate(con))
            return execute(args);
        else
            return false;
    }
    Ansi result = con.getResponseCode()/100==2 ? SUCCESS : FAILURE;
    result.outln(con.getResponseCode()+" "+con.getResponseMessage());
    System.out.println();

    boolean success = true;
    InputStream in = con.getErrorStream();
    if(in==null)
        in = con.getInputStream();
    else
        success = false;

    PushbackInputStream pin = new PushbackInputStream(in);
    int data = pin.read();
    if(data==-1){
        if(responseFile!=null)
            responseFile.delete();
        return success;
    }
    pin.unread(data);
    if(success && responseFile!=null){
        IOUtil.pump(pin, new FileOutputStream(responseFile), true, true);
        return true;
    }

    String contentType = con.getContentType();
    if(Util.isXML(contentType)){
        PrintStream sysErr = System.err;
        System.setErr(new PrintStream(new ByteArrayOutputStream()));
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.transform(new StreamSource(pin), new SAXResult(new AnsiHandler()));
            transformer.reset();
            return success;
        } catch (Exception ex) {
            sysErr.println("response is not valid xml: "+ex.getMessage());
            return false;
        } finally {
            System.setErr(sysErr);
        }
    }
    if(Util.isPlain(contentType) || Util.isJSON(contentType) || Util.isHTML(contentType)){
        IOUtil.pump(pin, System.out, true, false);
        System.out.println();
    }else{
        File temp = File.createTempFile("attachment", "."+Util.getExtension(contentType), FileUtil.USER_DIR);
        IOUtil.pump(pin, new FileOutputStream(temp), true, true);
        System.out.println("response saved to "+temp.getAbsolutePath());
    }
    return success;
}