Example usage for org.dom4j.tree DefaultDocumentType DefaultDocumentType

List of usage examples for org.dom4j.tree DefaultDocumentType DefaultDocumentType

Introduction

In this page you can find the example usage for org.dom4j.tree DefaultDocumentType DefaultDocumentType.

Prototype

public DefaultDocumentType(String elementName, String publicID, String systemID) 

Source Link

Document

This will create a new DocumentType with a reference to the external DTD

Usage

From source file:com.cladonia.xngreditor.FileUtilities.java

License:Open Source License

/**
 * Create a new document from a Grammar type or a schema.
 *
 * @param schema the schema used as a template.
 * @param type the type of document./*  w w  w. ja  v a2s . c om*/
 *
 * @return the new document.
 */
public static ExchangerDocument createDocument(XMLSchema schema, GrammarProperties type)
        throws IOException, SAXParseException {
    //      System.out.println( "FileUtilities.createDocument( "+schema+", "+type+")");
    XElement root = null;
    ExchangerDocument document = null;

    if (type != null) {
        String prefix = type.getNamespacePrefix();

        if (!StringUtilities.isEmpty(prefix)) {
            root = new XElement(type.getRootElementName(), type.getNamespace(), type.getNamespacePrefix());
        } else {
            root = new XElement(type.getRootElementName(), type.getNamespace());
        }

        Vector namespaces = type.getNamespaces();
        for (int i = 0; i < namespaces.size(); i++) {
            NamespaceProperties namespace = (NamespaceProperties) namespaces.elementAt(i);
            root.addNamespace(namespace.getPrefix(), namespace.getURI());
        }

        document = new ExchangerDocument((XElement) root);
        root = document.getRoot();

        String publicID = type.getPublicID();
        String validationLocation = type.getSystemID();

        if (StringUtilities.isEmpty(validationLocation)) {
            validationLocation = type.getValidationLocation();
        }

        if (type.getValidationGrammar() == XMLGrammar.TYPE_DTD
                && !StringUtilities.isEmpty(validationLocation)) {
            if (!StringUtilities.isEmpty(publicID)) {
                document.getDocument().setDocType(
                        new DefaultDocumentType(type.getRootElementName(), publicID, validationLocation));
            } else {
                document.getDocument()
                        .setDocType(new DefaultDocumentType(type.getRootElementName(), validationLocation));
            }
        } else if (type.getValidationGrammar() == XMLGrammar.TYPE_XSD
                && !StringUtilities.isEmpty(validationLocation)) {
            validationLocation = URLUtilities.encodeURL(validationLocation);

            Namespace xsiNamespace = root.getNamespaceForURI("http://www.w3.org/2001/XMLSchema-instance");
            if (xsiNamespace == null) {
                xsiNamespace = new DefaultNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                root.add(xsiNamespace);
            }

            String namespace = type.getNamespace();
            if (!StringUtilities.isEmpty(namespace)) {
                root.addAttribute(new QName("schemaLocation", xsiNamespace),
                        namespace + " " + validationLocation);
            } else {
                root.addAttribute(new QName("noNamespaceSchemaLocation", xsiNamespace), validationLocation);
            }
        }

    } else if (schema != null) {
        // bring up the root selection dialog
        RootSelectionDialog dialog = getRootSelectionDialog();
        dialog.setSchema(schema);

        if (!dialog.isCancelled()) {
            SchemaElement element = dialog.getSelectedElement();

            root = new XElement(element.getName(), element.getNamespace());
        }
    } else {
        root = new XElement("xngr");
    }

    root.setText("\n");

    if (document == null) {
        document = new ExchangerDocument((XElement) root);
    }

    document.update();
    return document;
}

From source file:net.sf.ginp.config.Configuration.java

License:Open Source License

/**
 * @throws java.io.IOException/*from  ww  w . j a  va 2 s.c om*/
 * @throws net.sf.ginp.setup.SetupException
 */
private static void readConfig() {
    try {
        //Parse Ginp Config File
        File confFile = new File(configfilelocation);

        if (confFile.exists()) {
            FileInputStream fis = new FileInputStream(confFile);
            SetupManager service = ModelUtil.getSetupManager();
            document = service.testValidConfig(fis);
            configOK = true;
        } else {
            // log that we have no config
            log.warn("No configuration at: " + configfilelocation);
            log.warn("Setting up standard configuration document.");
            configOK = false;

            // Make new default config.
            DefaultDocumentType docType = new DefaultDocumentType("ginp", "-//GINP//DTD ginp XML//EN",
                    "ginp.dtd");

            document = DocumentHelper.createDocument();
            document.setDocType(docType);

            Element root = document.addElement("ginp");
            root.addAttribute("characterencoding", "UTF-8");
            root.addAttribute("thumbsize", "200");
            root.addAttribute("filmstripthumbsize", "100");
            root.addAttribute("picturepagename", "picture.jsp");
            root.addAttribute("collectionpagename", "collection.jsp");

            // TODO: Add Admin
            Element user = root.addElement("user").addAttribute("fullname", "Administrator")
                    .addAttribute("username", "admin").addAttribute("userpass", "");
        }

        // set static vars once.
        forcelocale = document.valueOf("/ginp/@forcelocale");
        thumbSize = document.numberValueOf("/ginp/@thumbsize").intValue();
        filmStripThumbSize = document.numberValueOf("/ginp/@filmstripthumbsize").intValue();
        picturePageName = document.valueOf("/ginp/@picturepagename");
        collectionPageName = document.valueOf("/ginp/@collectionpagename");
        characterEncoding = document.valueOf("/ginp/@characterencoding");

        // wrtie to debug output.
        if (log.isDebugEnabled()) {
            log.debug("Setting Configuation: forcelocale=" + forcelocale);
            log.debug("Setting Configuation: thumbSize=" + thumbSize);
            log.debug("Setting Configuation: filmStripThumbSize=" + filmStripThumbSize);
            log.debug("Setting Configuation: getPicturePageName=" + picturePageName);
            log.debug("Setting Configuation: getCollectionPageName=" + collectionPageName);
            log.debug("Setting Configuation: characterEncoding=" + characterEncoding);
        }

        log.info("Read configuration at: " + configfilelocation);
    } catch (Exception ex) {
        configOK = false;
        log.error("Error reading configuration file located at: " + configfilelocation, ex);
    }
}

From source file:net.sf.ginp.setup.SetupManagerImpl.java

License:Open Source License

/**
 * @param visit//from w  ww . jav a2s  .com
 * @return
 * @throws DocumentException
 * @throws TransformerException
 */
private Document getConfigFromSetupVisit(final SetupVisit visit)
        throws DocumentException, TransformerException {
    XStream stream = new XStream();
    String visitXML = stream.toXML(visit);
    SAXReader read = new SAXReader();
    Document visitDoc = read.read(new StringReader(visitXML));
    Document outputDocument = GinpUtil.transform("/net/sf/ginp/setup/visitToConfig.xsl", visitDoc);

    // necessary because the xsl transform ignores the XSL:output tag
    outputDocument.setDocType(new DefaultDocumentType("ginp", "-//GINP//DTD ginp XML//EN", "ginp.dtd"));

    return outputDocument;
}