List of usage examples for org.dom4j.tree DefaultNamespace DefaultNamespace
public DefaultNamespace(String prefix, String uri)
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 ww. java 2 s . c o m*/ * * @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; }