Example usage for org.dom4j Document addDocType

List of usage examples for org.dom4j Document addDocType

Introduction

In this page you can find the example usage for org.dom4j Document addDocType.

Prototype

Document addDocType(String name, String publicId, String systemId);

Source Link

Document

Adds a DOCTYPE declaration to this document

Usage

From source file:com.bsoft.baseframe.baseframe_utils.beanUtils.CreateXMLFile.java

License:Open Source License

/**
 * xml??/*from w w  w. j a v a 2 s . co m*/
 * @return ??
 */
public boolean createXMLFile() {
    String clsName = FileUtils.getClassName(tbName);
    String fileName = path + clsName + ".xml";
    String beanName = beanPkg + "." + clsName;

    List<GenaBean> list = GenaBeanDBUtils.getFields(tbName);
    if (list.size() == 0) {
        return false;
    }
    StringBuffer fields = new StringBuffer();
    StringBuffer values = new StringBuffer();
    GenaBean gb = null;
    String fn = null;
    for (int i = 0; i < list.size(); i++) {
        gb = list.get(i);
        fn = gb.getColumnName();
        fields.append(fn);
        if ("delflag".equals(fn)) {
            values.append("'0'");
        } else if ("ctb1".equals(fn)) {
            values.append("getdate()");
        } else {
            values.append("#").append(fn).append("#");
        }
        if (i != list.size() - 1) {
            fields.append(", ");
            values.append(", ");
        }
    }
    System.out.println(fields.toString());
    System.out.println(values.toString());

    //Document
    Document document = DocumentHelper.createDocument();

    //
    document.addDocType("sqlMap", "-//ibatis.apache.org//DTD SQL Map 2.0//EN",
            "http://ibatis.apache.org/dtd/sql-map-2.dtd");

    // document
    Element rootElement = document.addElement("sqlMap");

    //??
    rootElement.addAttribute("namespace", clsName.toUpperCase());

    //insert
    Element insertNode = rootElement.addElement("insert");
    insertNode.addAttribute("id", "add" + clsName);
    insertNode.addAttribute("parameterClass", beanName);
    insertNode.setText("insert into " + tbName + "(" + fields + ") values (" + values + ")");

    //?
    Element removeNode = rootElement.addElement("delete");
    removeNode.addAttribute("id", "remove" + clsName);
    removeNode.addAttribute("parameterClass", beanName);
    removeNode.setText("delete from " + tbName + " where id = #id#");

    //
    Element deleteNode = rootElement.addElement("update");
    deleteNode.addAttribute("id", "del" + clsName);
    deleteNode.addAttribute("parameterClass", beanName);
    deleteNode.setText("update " + tbName + " set delflag=#delflag# where id = #id#");

    //selectOne
    Element selectOneNode = rootElement.addElement("select");
    selectOneNode.addAttribute("id", "getOne" + clsName);
    selectOneNode.addAttribute("parameterClass", beanName);
    selectOneNode.addAttribute("resultClass", beanName);
    selectOneNode.setText("select *  from " + tbName + " where id = #id# ");

    //select
    Element selectNode = rootElement.addElement("select");
    selectNode.addAttribute("id", "queryCount");
    selectNode.addAttribute("resultClass", "int");
    selectNode.setText("select count(*) as countnum from " + tbName + " where 1=1");

    //select
    Element selectNode2 = rootElement.addElement("select");
    selectNode2.addAttribute("id", "list" + clsName);
    selectNode2.addAttribute("resultClass", beanName);
    selectNode2.addAttribute("parameterClass", "java.util.Map");
    selectNode2.setText("select * from (select top  $endNum$ row_number()over(order by TT.id) as RN,* from"
            + "(SELECT * FROM " + tbName + " where 1=1) as TT)as H where RN > #startNum#");

    //update
    Element updateNode = rootElement.addElement("update");
    updateNode.addAttribute("id", "upSap" + clsName);
    updateNode.addAttribute("parameterClass", beanName);
    updateNode.setText("UPDATE " + tbName + " ");
    Element erElement = updateNode.addElement("dynamic");
    erElement.addAttribute("prepend", "set");
    for (int i = 0; i < list.size(); i++) {
        gb = list.get(i);
        fn = gb.getColumnName();
        if (!"id".equals(fn)) {
            Element saElement = erElement.addElement("isNotNull");
            saElement.addAttribute("property", fn);
            saElement.addAttribute("removeFirstPrepend", "true");
            saElement.addAttribute("prepend", ",");
            saElement.setText(fn + "=#" + fn + "#");
        }
    }
    updateNode.addText(" where ");
    Element terElement = updateNode.addElement("isNotNull");
    terElement.addAttribute("property", "id");
    terElement.setText(" id = #id# ");

    //update
    Element updateSimpleNode = rootElement.addElement("update");
    updateSimpleNode.addAttribute("id", "update" + clsName);
    updateSimpleNode.addAttribute("parameterClass", beanName);

    StringBuffer sb = new StringBuffer();
    sb.append("update ").append(tbName).append(" set ");

    for (int i = 0; i < list.size(); i++) {
        gb = list.get(i);
        fn = gb.getColumnName();
        if (!"id".equals(fn)) {
            if ("ctb3".equals(fn)) {
                sb.append(fn).append("=").append("getdate()");
            } else {
                sb.append(fn).append("=#").append(fn).append("#");
            }
            if (i != list.size() - 1) {
                sb.append(", ");
            }

        }
    }
    sb.append(" where id = #id# ");
    updateSimpleNode.setText(sb.toString());
    return FileUtils.wrieteXML2Doc(document, new File(fileName));
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * , ?DTD? Doctype// ww  w  .  jav  a 2  s. c om
 *
 * @param document
 *            XML
 * @param rootName
 *            ??
 * @param docType
 *            
 */
public void addDocType(Document document, String rootName, String docType) {
    document.addDocType(rootName, null, docType);
    //<!DOCTYPE catalog SYSTEM "file://c:/Dtds/catalog.dtd">
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * doc type/*  w w  w .j  av a2s .  c  o m*/
 * @param document
 */
private void generateDocType(Document document) {
    document.addDocType(ROOT_ELEMENT_NAME, "-//ibatis.apache.org//DTD Mapper 3.0//EN",
            "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd");
}

From source file:com.sap.data.db.dao.StructureUtil.java

private void save(Document document, String filepath) throws IOException {
    document.addDocType(this.HIBERNATE_MAPPING, this.HIBERNATE_MAPPING_EN, this.HIBERNATE_MAPPING_DTD);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    XMLWriter writer = new XMLWriter(new FileWriter(new File(filepath)), format);
    writer.write(document);/*w ww  .  j  a  va2s  .c  o m*/
    writer.close();
}

From source file:fullyMeshedNet.FullyMeshedNet.java

License:Open Source License

@Override
public void exportToFile(File saveFile) {
    String extension = FrevoMain.getExtension(saveFile);
    if (extension.equals("net")) {
        System.out.println("Exporting Pajek network file to " + saveFile.getName());
        try {//from  ww w. jav a 2 s  .c  om
            // Create file
            FileWriter fstream = new FileWriter(saveFile);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("*Vertices " + this.nodes);
            out.newLine();
            // input neurons
            for (int i = 1; i < input_nodes + 1; i++) {
                out.write(i + " \"I" + i + "\"");
                out.newLine();
            }
            for (int h = input_nodes + 1; h < nodes - output_nodes + 1; h++) {
                out.write(h + " \"H" + (h - input_nodes) + "\"");
                out.newLine();
            }
            int a = 1;
            for (int o = nodes - output_nodes + 1; o < nodes + 1; o++) {
                out.write(o + " \"O" + a + "\"");
                out.newLine();
                a++;
            }

            // Edges
            out.write("*Edges");
            out.newLine();
            for (int n = 0; n < input_nodes - 1; n++) {
                for (int p = n + 1; p < input_nodes; p++) {
                    if (n != p) {
                        out.write((n + 1) + " " + (p + 1) + " " + 0);
                        out.newLine();
                    }
                }
            }

            for (int n = input_nodes; n < nodes; n++) {
                for (int from = 0; from < nodes; from++) {
                    out.write((n + 1) + " " + (from + 1) + " " + weight[from][n]);
                    out.newLine();
                }
            }

            // Close the output stream
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    } else if (extension.equals("xml")) {
        System.out.println("Saving to XML");

        Document doc = DocumentHelper.createDocument();
        doc.addDocType("CompleteNetwork", null, System.getProperty("user.dir")
                + "//Components//Representations//CompleteNetwork//src//CompleteNetwork.dtd");
        Element cnetwork = doc.addElement("CompleteNetwork");
        this.exportToXmlElement(cnetwork);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));

        try {
            saveFile.createNewFile();
            FileWriter out = new FileWriter(saveFile);
            BufferedWriter bw = new BufferedWriter(out);
            XMLWriter wr = new XMLWriter(bw, format);
            wr.write(doc);
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:hebbNet.HebbNet.java

License:Open Source License

@Override
public void exportToFile(File saveFile) {
    String extension = FrevoMain.getExtension(saveFile);
    if (extension.equals("net")) {
        System.out.println("Exporting Pajek network file to " + saveFile.getName());
        try {/*  w  w  w .  j a v  a 2 s  . com*/
            // Create file
            FileWriter fstream = new FileWriter(saveFile);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("*Vertices " + this.nodes);
            out.newLine();
            // input neurons
            for (int i = 1; i < input_nodes + 1; i++) {
                out.write(i + " \"I" + i + "\"");
                out.newLine();
            }
            for (int h = input_nodes + 1; h < nodes - output_nodes + 1; h++) {
                out.write(h + " \"H" + (h - input_nodes) + "\"");
                out.newLine();
            }
            int a = 1;
            for (int o = nodes - output_nodes + 1; o < nodes + 1; o++) {
                out.write(o + " \"O" + a + "\"");
                out.newLine();
                a++;
            }

            // Edges
            out.write("*Edges");
            out.newLine();
            for (int n = 0; n < input_nodes - 1; n++) {
                for (int p = n + 1; p < input_nodes; p++) {
                    if (n != p) {
                        out.write((n + 1) + " " + (p + 1) + " " + 0);
                        out.newLine();
                    }
                }
            }

            for (int n = input_nodes; n < nodes; n++) {
                for (int from = 0; from < nodes; from++) {
                    out.write((n + 1) + " " + (from + 1) + " " + weight[from][n]);
                    // out.write((n+1)+" "+(from+1)+" "+plasticity[from][n]);
                    out.newLine();
                }
            }

            // Close the output stream
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    } else if (extension.equals("xml")) {
        System.out.println("Saving to XML");

        Document doc = DocumentHelper.createDocument();
        doc.addDocType("HebbNet", null,
                System.getProperty("user.dir") + "//Components//Representations//HebbNet//src//HebbNet.dtd");
        Element cnetwork = doc.addElement("HebbNet");
        this.exportToXmlElement(cnetwork);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));

        try {
            saveFile.createNewFile();
            FileWriter out = new FileWriter(saveFile);
            BufferedWriter bw = new BufferedWriter(out);
            XMLWriter wr = new XMLWriter(bw, format);
            wr.write(doc);
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:main.FrevoMain.java

License:Open Source License

/**
 * Starts a saving procedure calling the method's own saveResults function.
 * Run-specific data will be added to the end of the file.
 * /*  www .j ava 2  s.  co m*/
 * @param fileName
 *            Name of the saved file without the extension. (E.g.
 *            solution_generation_13)
 * @param method
 *            The corresponding method instance to be called for saving.
 */
public static File saveResult(String fileName, final Element representationRootElement, long startSeed,
        long currentActiveSeed) {
    // create new document for output
    Document doc = DocumentHelper.createDocument();
    doc.addDocType("frevo", null, System.getProperty("user.dir") + "//Components//ISave.dtd");
    Element dfrevo = doc.addElement("frevo");

    String fileLocation = "Undefined";

    // export sessionconfig
    Element sessionconfig = dfrevo.addElement("sessionconfig");

    // custom name
    Element configentry = sessionconfig.addElement("configentry");
    configentry.addAttribute("key", "CustomName");
    configentry.addAttribute("type", "STRING");
    configentry.addAttribute("value", customName);

    // number of runs
    Element runentry = sessionconfig.addElement("configentry");
    runentry.addAttribute("key", "NumberofRuns");
    runentry.addAttribute("type", "INT");
    runentry.addAttribute("value", Integer.toString(getNumberOfSimulationRuns()));

    // starting seed
    Element seedentry = sessionconfig.addElement("configentry");
    seedentry.addAttribute("key", "StartingSeed");
    seedentry.addAttribute("type", "LONG");
    // seedentry.addAttribute("value", Long.toString(getSeed()));
    seedentry.addAttribute("value", Long.toString(startSeed));

    // active seed
    Element aseedentry = sessionconfig.addElement("configentry");
    aseedentry.addAttribute("key", "CurrentSeed");
    aseedentry.addAttribute("type", "LONG");
    aseedentry.addAttribute("value", Long.toString(currentActiveSeed));

    try {
        // export problem
        Element problemsettings = dfrevo.addElement("problem");
        ComponentXMLData problem = FrevoMain.SELECTED_PROBLEM;
        problemsettings.addAttribute("class", problem.getClassName());
        Vector<String> keys = new Vector<String>(problem.getProperties().keySet());
        for (String k : keys) {
            Element entry = problemsettings.addElement("problementry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", problem.getTypeOfProperty(k).toString());
            entry.addAttribute("value", problem.getValueOfProperty(k));
        }

        // export method
        Element methodsettings = dfrevo.addElement("method");
        ComponentXMLData method = FrevoMain.SELECTED_METHOD;
        methodsettings.addAttribute("class", method.getClassName());
        keys = new Vector<String>(method.getProperties().keySet());
        for (String k : keys) {
            Element entry = methodsettings.addElement("methodentry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", method.getTypeOfProperty(k).toString());
            entry.addAttribute("value", method.getValueOfProperty(k));
        }

        // export ranking
        Element rankingsettings = dfrevo.addElement("ranking");
        ComponentXMLData ranking = FrevoMain.SELECTED_RANKING;
        rankingsettings.addAttribute("class", ranking.getClassName());
        keys = new Vector<String>(ranking.getProperties().keySet());
        for (String k : keys) {
            Element entry = rankingsettings.addElement("rankingentry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", ranking.getTypeOfProperty(k).toString());
            entry.addAttribute("value", ranking.getValueOfProperty(k));
        }

        // export representation
        Element repsettings = dfrevo.addElement("representation");
        ComponentXMLData representation = FrevoMain.SELECTED_REPRESENTATION;
        repsettings.addAttribute("class", representation.getClassName());
        keys = new Vector<String>(representation.getProperties().keySet());
        for (String k : keys) {
            Element entry = repsettings.addElement("representationentry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", representation.getTypeOfProperty(k).toString());
            entry.addAttribute("value", representation.getValueOfProperty(k));
        }

        // call method's own save solution
        dfrevo.add(representationRootElement);

        // save contents to file

        String location = FREVO_INSTALL_DIRECTORY + File.separator + "Results" + File.separator + customName;
        File rootSaveDir = new File(location);

        // remove spaces from filename
        fileName.replaceAll(" ", "_");

        // create save directory based on given custom name
        rootSaveDir.mkdirs();

        // create sub-directories for different seeds
        if (FrevoMain.getNumberOfSimulationRuns() > 1) {
            // create seed directory if there are more than one run
            File seedDir = new File(location + File.separator + "seed_" + startSeed);
            seedDir.mkdir();
            fileLocation = seedDir + File.separator + fileName + ".zre";
        } else {
            // save it the root location
            fileLocation = rootSaveDir + File.separator + fileName + ".zre";
        }

        File saveFile = new File(fileLocation);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));

        saveFile.createNewFile();
        FileWriter out = new FileWriter(saveFile);
        BufferedWriter bw = new BufferedWriter(out);
        XMLWriter wr = new XMLWriter(bw, format);
        wr.write(doc);
        wr.close();
        System.out.println("XML Writing Completed: " + fileLocation);

        if (isFrevoWithGraphics()) {
            mainWindow.addRecentResult(saveFile);
        }
        return saveFile;
    } catch (OutOfMemoryError mem) {
        System.err.println("Could not export! (Out of memory)");
    } catch (IOException e) {
        System.err.println("IOException while writing to XML! Check path at: " + fileLocation);
        e.printStackTrace();
    }
    return null;
}

From source file:main.FrevoMain.java

License:Open Source License

/**
 * Saves FREVO settings to the configuration file. Initiates file writing to
 * store data.//from   w ww . ja  va  2  s . com
 */
public static void saveSettings() {
    Document doc;
    File configfile = new File(FREVO_INSTALL_DIRECTORY + "/frevo_config.xml");
    if (configfile.exists()) {
        // load data if it already exists
        doc = SafeSAX.read(configfile, true);

        Node windowsizes = doc.selectSingleNode("/frevo/window-sizes");

        List<?> items = windowsizes.selectNodes(".//window");
        Iterator<?> it = items.iterator();

        while (it.hasNext()) {
            Element el = (Element) it.next();
            String name = el.valueOf("./@name");

            // save main data
            if (name.equals("main")) {
                el.addAttribute("width", Integer.toString(FrevoMain.mainWindowParameters[0]));
                el.addAttribute("height", Integer.toString(FrevoMain.mainWindowParameters[1]));
            }
            // save component browser data
            if (name.equals("componentbrowser")) {
                el.addAttribute("width", Integer.toString(FrevoMain.componentBrowserParameters[0]));
                el.addAttribute("height", Integer.toString(FrevoMain.componentBrowserParameters[1]));
                el.addAttribute("topsplit", Integer.toString(FrevoMain.componentBrowserParameters[2]));
                el.addAttribute("bigsplit", Integer.toString(FrevoMain.componentBrowserParameters[3]));
            }
        }
    } else {
        try {
            configfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // file does not exist
        doc = DocumentHelper.createDocument();
        doc.addDocType("frevo", null, "config.dtd");
        Element dfrevo = doc.addElement("frevo");

        dfrevo.addElement("tags");

        Element windowsizes = dfrevo.addElement("window-sizes");
        Element mainwindow = windowsizes.addElement("window");
        mainwindow.addAttribute("name", "main");
        mainwindow.addAttribute("width", Integer.toString(FrevoMain.mainWindowParameters[0]));
        mainwindow.addAttribute("height", Integer.toString(FrevoMain.mainWindowParameters[1]));

        Element compwindow = windowsizes.addElement("window");
        compwindow.addAttribute("name", "componentbrowser");
        compwindow.addAttribute("width", Integer.toString(FrevoMain.componentBrowserParameters[0]));
        compwindow.addAttribute("height", Integer.toString(FrevoMain.componentBrowserParameters[1]));
        compwindow.addAttribute("topsplit", Integer.toString(FrevoMain.componentBrowserParameters[2]));
        compwindow.addAttribute("bigsplit", Integer.toString(FrevoMain.componentBrowserParameters[3]));
    }

    // write to file
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));

        configfile.createNewFile();
        FileWriter out = new FileWriter(configfile);
        BufferedWriter bw = new BufferedWriter(out);
        XMLWriter wr = new XMLWriter(bw, format);
        wr.write(doc);
        wr.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:net.contextfw.web.application.internal.WebResponder.java

License:Apache License

public void sendHTMLResponse(Document document, HttpServletResponse resp, Mode mode)
        throws ServletException, IOException {

    resp.setContentType(mode.getContentType());
    resp.setHeader("Expires", "-1");
    resp.setHeader("Pragma", "no-cache");
    resp.setHeader("Cache-Control", "no-cache, no-store");

    if (!transformers.isInitialized()) {
        synchronized (transformers) {
            if (!transformers.isInitialized()) {
                transformers.initialize(getXSLDocument());
            }/*from w  w  w . java 2 s  . com*/
        }
    }

    Document rDocument = transformers.transform(document);

    if (mode == Mode.INIT) {
        rDocument.addDocType("html", "-//W3C//DTD XHTML 1.0 Transitional//EN",
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
    }

    new HTMLWriter(resp.getWriter(), htmlFormat).write(rDocument);
}

From source file:net.sf.eclipsecs.core.config.ConfigurationWriter.java

License:Open Source License

/**
 * Writes the modules of the configuration to the output stream.
 *
 * @param out//from   w  w  w .jav  a2 s.  com
 *            the ouput stream.
 * @param modules
 *            the modules
 * @param checkConfig
 *            the Check configuration object
 * @throws CheckstylePluginException
 *             error writing the checkstyle configuration
 */
public static void write(OutputStream out, List<Module> modules, ICheckConfiguration checkConfig)
        throws CheckstylePluginException {

    try {
        // pass the configured modules through the save filters
        SaveFilters.process(modules);

        Document doc = DocumentHelper.createDocument();
        doc.addDocType(XMLTags.MODULE_TAG, "-//Puppy Crawl//DTD Check Configuration 1.3//EN",
                "http://www.puppycrawl.com/dtds/configuration_1_3.dtd");

        String lineSeperator = System.getProperty("line.separator"); //$NON-NLS-1$

        String comment = lineSeperator
                + "    This configuration file was written by the eclipse-cs plugin configuration editor" //$NON-NLS-1$
                + lineSeperator;
        doc.addComment(comment);

        // write out name and description as comment
        String description = lineSeperator + "    Checkstyle-Configuration: " //$NON-NLS-1$
                + checkConfig.getName() + lineSeperator + "    Description: " //$NON-NLS-1$
                + (StringUtils.trimToNull(checkConfig.getDescription()) != null
                        ? lineSeperator + checkConfig.getDescription() + lineSeperator
                        : "none" + lineSeperator); //$NON-NLS-1$
        doc.addComment(description);

        // find the root module (Checker)
        // the root module is the only module that has no parent
        List<Module> rootModules = getChildModules(null, modules);
        if (rootModules.size() < 1) {
            throw new CheckstylePluginException(Messages.errorNoRootModule);
        }

        if (rootModules.size() > 1) {
            throw new CheckstylePluginException(Messages.errorMoreThanOneRootModule);
        }

        writeModule(rootModules.get(0), doc, null, modules);

        out.write(XMLUtil.toByteArray(doc));
    } catch (IOException e) {
        CheckstylePluginException.rethrow(e);
    }
}