Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Store.java
   *
   * Copyright 2008 Mark Logic Corporation.
   * Portions Copyright 2007 Sun Microsystems, Inc.
   * All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * https://xproc.dev.java.net/public/CDDL+GPL.html or
  * docs/CDDL+GPL.txt in the distribution. See the License for the
  * specific language governing permissions and limitations under the
  * License. When distributing the software, include this License Header
  * Notice in each file and include the License file at docs/CDDL+GPL.txt.
  */
 
 package com.xmlcalabash.library;
 
 import java.io.*;
 import java.net.URI;
 
 
 
 
 import java.net.URL;

Author(s):
ndw
 
 public class Store extends DefaultStep {
     private static final QName _href = new QName("href");
     private static final QName _encoding = new QName("encoding");
     private static final QName _content_type = new QName("content-type");
     private static final QName c_encoding = new QName("c"."encoding");
     private static final QName c_body = new QName("c"."body");
     private static final QName c_json = new QName("c"."json");
     private static final QName cx_decode = new QName("cx"."decode");
 
     private ReadablePipe source = null;
     private WritablePipe result = null;
 
     // Store also implements the cx:gzip step.
     // If gzip is true, then href may be null
     private URI href = null;

    
Creates a new instance of Store
 
     public Store(XProcRuntime runtimeXAtomicStep step) {
         super(runtime,step);
     }
 
     public void setInput(String portReadablePipe pipe) {
          = pipe;
     }
 
     public void setOutput(String portWritablePipe pipe) {
          = pipe;
     }
 
     public void reset() {
         .resetReader();
         .resetWriter();
     }
 
     public void run() throws SaxonApiException {
         super.run();
 
         if (.getSafeMode()) {
             throw XProcException.dynamicError(21);
         }
 
        RuntimeValue hrefOpt = getOption();
        XdmNode doc = .read();
        if (doc == null || .moreDocuments()) {
            throw XProcException.dynamicError(6);
        }
        if (hrefOpt != null) {
             = hrefOpt.getBaseURI().resolve(hrefOpt.getString());
        }
        if ( == .) {
            finer(hrefOpt == null ? null : hrefOpt.getNode(), "Gzipping" + ( == null ? "" : " to \"" +  + "\"."));
        } else {
            finer(hrefOpt.getNode(), "Storing to \"" +  + "\".");
        }
        String decode = .getExtensionAttribute();
        XdmNode root = S9apiUtils.getDocumentElement(doc);
        if (("true".equals(decode) || "1".equals(decode) ||  != .)
             && ((..equals(root.getNodeName().getNamespaceURI())
                  && "base64".equals(root.getAttributeValue()))
                 || ("".equals(root.getNodeName().getNamespaceURI())
                     && "base64".equals(root.getAttributeValue())))) {
            storeBinary(doc);
        } else if (.transparentJSON()
                   && (((.equals(root.getNodeName())
                        && ("application/json".equals(root.getAttributeValue())
                            || "text/json".equals(root.getAttributeValue())))
                       || .equals(root.getNodeName()))
                       || ..equals(root.getNodeName().getNamespaceURI())
                       || ..equals(root.getNodeName().getNamespaceURI())
                       || ..equals(root.getNodeName().getNamespaceURI()))) {
            storeJSON(doc);
        } else {
            storeXML(doc);
        }
        if ( != null) {
            TreeWriter tree = new TreeWriter();
            tree.startDocument(.getNode().getBaseURI());
            tree.addStartElement(.);
            tree.startContent();
            tree.addText(.toString());
            tree.addEndElement();
            tree.endDocument();
            .write(tree.getResult());
        }
    }
    private void storeXML(XdmNode docURI hrefthrows SaxonApiException {
        Serializer serializer = makeSerializer();
        try {
            OutputStream outstr = null;
            ByteArrayOutputStream baos = null;
            if (href == null) {
                baos = new ByteArrayOutputStream();
                outstr = baos;
            } else if (href.getScheme().equals("file")) {
                File output = new File(href);
                File path = new File(output.getParent());
                if (!path.isDirectory()) {
                    if (!path.mkdirs()) {
                        throw XProcException.stepError(50);
                    }
                }
                outstr = new FileOutputStream(output);
            } else {
                final URLConnection conn = href.toURL().openConnection();
                conn.setDoOutput(true);
                outstr = conn.getOutputStream();
            }
            if ( == .) {
                GZIPOutputStream gzout = new GZIPOutputStream(outstr);
                outstr = gzout;
            }
            serializer.setOutputStream(outstr);
            S9apiUtils.serialize(docserializer);
            outstr.close();
            if (href == null) {
                returnData(baos);
            }
        } catch (IOException ioe) {
            throw XProcException.stepError(50, ioe);
        }
    }
    private void storeBinary(XdmNode docURI href) {
        try {
            byte[] decoded = Base64.decode(doc.getStringValue());
            OutputStream outstr = null;
            ByteArrayOutputStream baos = null;
            if (href == null) {
                baos = new ByteArrayOutputStream();
                outstr = baos;
            } else {
                File output = new File(href);
                File path = new File(output.getParent());
                if (!path.isDirectory()) {
                    if (!path.mkdirs()) {
                        throw XProcException.stepError(50);
                    }
                }
                outstr = new FileOutputStream(output);
            }
            if ( == .) {
                GZIPOutputStream gzout = new GZIPOutputStream(outstr);
                outstr = gzout;
            }
            outstr.write(decoded);
            outstr.close();
            if (href == null) {
                returnData(baos);
            }
        } catch (IOException ioe) {
            throw new XProcException(ioe);
        }
    }
    private void storeJSON(XdmNode docURI hrefthrows SaxonApiException {
        try {
            OutputStream outstr = null;
            ByteArrayOutputStream baos = null;
            if (href == null) {
                baos = new ByteArrayOutputStream();
                outstr = baos;
            } else if (href.getScheme().equals("file")) {
                File output = new File(href);
                File path = new File(output.getParent());
                if (!path.isDirectory()) {
                    if (!path.mkdirs()) {
                        throw XProcException.stepError(50);
                    }
                }
                outstr = new FileOutputStream(output);
            } else {
                final URLConnection conn = href.toURL().openConnection();
                conn.setDoOutput(true);
                outstr = conn.getOutputStream();
            }
            if ( == .) {
                GZIPOutputStream gzout = new GZIPOutputStream(outstr);
                outstr = gzout;
            }
            PrintWriter writer = new PrintWriter(outstr);
            String json = XMLtoJSON.convert(doc);
            writer.print(json);
            writer.close();
            outstr.close();
            if (href == null) {
                returnData(baos);
            }
        } catch (IOException ioe) {
            throw XProcException.stepError(50, ioe);
        }
    }
    public void returnData(ByteArrayOutputStream baos) {
        // We're only called if the output is compressed
        TreeWriter tree = new TreeWriter();
        tree.startDocument(.getNode().getBaseURI());
        tree.addAttribute("base64");
        tree.addAttribute("application/x-gzip");
        tree.startContent();
        tree.addText(Base64.encodeBytes(baos.toByteArray()));
        tree.addEndElement();
        tree.endDocument();
        .write(tree.getResult());
    }
    protected enum CompressionMethod { NONE, GZIP };
New to GrepCode? Check out our FAQ X