Re: Adding <!DOCTYPE html> using XProc/Calabash

Well if it's relevant when saving to disk you could create an extension to
save output a bit differently appending that statement.

I did  something similar below but for doing a replace ("&amp;", "&");
before saving.


package com.mh.calabash.extensions;

import java.net.URI;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.model.RuntimeValue;
import com.xmlcalabash.util.TreeWriter;
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.core.XProcRuntime;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.QName;
import com.xmlcalabash.runtime.XAtomicStep;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcException;

public class StoreAsText extends DefaultStep {
    private static final QName _href = new QName("href");

    private ReadablePipe source = null;
    private WritablePipe result = null;

    /**
     * Creates a new instance of Store
     */
    public StoreAsText(XProcRuntime runtime, XAtomicStep step) {
        super(runtime, step);
    }

    public void setInput(String port, ReadablePipe pipe) {
        source = pipe;
    }

    public void setOutput(String port, WritablePipe pipe) {
        result = pipe;
    }

    public void reset() {
        source.resetReader();
        result.resetWriter();
    }

    public void run() throws SaxonApiException {
        super.run();

        if (runtime.getSafeMode()) {
            throw XProcException.dynamicError(21);
        }

        URI href = null;
        RuntimeValue hrefOpt = getOption(_href);

        XdmNode doc = source.read();

        if (doc == null || source.moreDocuments()) {
            throw XProcException.dynamicError(6);
        }

        if (hrefOpt != null) {
            href = hrefOpt.getBaseURI().resolve(hrefOpt.getString());
        } else {
            href = doc.getBaseURI();
        }

        fine(hrefOpt.getNode(), "Storing to \"" + href + "\".");

        storeXMLAsText(doc, href);
        TreeWriter tree = new TreeWriter(runtime);
        tree.startDocument(step.getNode().getBaseURI());
        tree.addStartElement(XProcConstants.c_result);
        tree.startContent();
        tree.addText(href.toString());
        tree.addEndElement();
        tree.endDocument();
        result.write(tree.getResult());
    }

    private void storeXMLAsText(XdmNode doc, URI href) throws
SaxonApiException {

        String output = doc.toString();
        output = output.replace("&amp;", "&");
        File tmpfile = new File(href);


        try {


            File path = new File(tmpfile.getParent());
            if (!path.isDirectory()) {
                if (!path.mkdirs()) {
                    throw XProcException.stepError(50);
                }
            }

            BufferedWriter bufferedWriter = new BufferedWriter(new
FileWriter(tmpfile));
            bufferedWriter.write(output);
            bufferedWriter.flush();
            bufferedWriter.close();

        } catch (IOException ioe) {
            throw XProcException.stepError(50, ioe);
        }
    }


}





On Thu, Aug 16, 2012 at 12:09 PM, Zearin <zearin@gonk.net> wrote:

> On Aug 16, 2012, at 9:14 AM, David Cramer <david@thingbag.net> wrote:
> > Hi there,
> > How do you generate html files with the html 5 DOCTYPE declaration
> > (<!DOCTYPE html>) using XProc/Calabash?
>
> Good question!  +1.
>
> This is a major issue for me, and a significant factor in why I don’t use
> Calabash so much these days.
>
>
> > I tried using <!DOCTYPE html SYSTEM "about:legacy-compat">, but that
> > caused problems in some contexts.
>
> Interesting.  Could you elaborate on those contexts?
>
>
> —T
>
>


-- 
-

Alex G. Muir
Software Engineering Consultant
Linkedin Profile : http://ca.linkedin.com/pub/alex-muir/36/ab7/125

Received on Thursday, 16 August 2012 17:36:39 UTC