Example usage for org.jdom2 Namespace getNamespace

List of usage examples for org.jdom2 Namespace getNamespace

Introduction

In this page you can find the example usage for org.jdom2 Namespace getNamespace.

Prototype

public static Namespace getNamespace(final String prefix, final String uri) 

Source Link

Document

This will retrieve (if in existence) or create (if not) a Namespace for the supplied prefix and uri.

Usage

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method replaceXpp3DOM./*from www.  j  a  v a 2s  .co  m*/
 * 
 * @param parent
 * @param counter
 * @param parentDom
 */
public static void replaceXpp3DOM(final Element parent, final Xpp3Dom parentDom,
        final IndentationCounter counter) {
    if (parentDom.getChildCount() > 0) {
        final Xpp3Dom[] childs = parentDom.getChildren();
        final Collection domChilds = new ArrayList();
        for (final Xpp3Dom child : childs) {
            domChilds.add(child);
        }
        final ListIterator it = parent.getChildren().listIterator();
        while (it.hasNext()) {
            final Element elem = (Element) it.next();
            final Iterator it2 = domChilds.iterator();
            Xpp3Dom corrDom = null;
            while (it2.hasNext()) {
                final Xpp3Dom dm = (Xpp3Dom) it2.next();
                if (dm.getName().equals(elem.getName())) {
                    corrDom = dm;
                    break;
                }
            }
            if (corrDom != null) {
                domChilds.remove(corrDom);
                replaceXpp3DOM(elem, corrDom, new IndentationCounter(counter.getDepth() + 1));
                counter.increaseCount();
            } else {
                it.remove();
            }
        }
        final Iterator it2 = domChilds.iterator();
        while (it2.hasNext()) {
            final Xpp3Dom dm = (Xpp3Dom) it2.next();
            final String rawName = dm.getName();
            final String[] parts = rawName.split(":");

            Element elem;
            if (parts.length > 1) {
                final String nsId = parts[0];
                String nsUrl = dm.getAttribute("xmlns:" + nsId);
                final String name = parts[1];
                if (nsUrl == null) {
                    nsUrl = parentDom.getAttribute("xmlns:" + nsId);
                }
                elem = factory.element(name, Namespace.getNamespace(nsId, nsUrl));
            } else {
                Namespace root = parent.getNamespace();
                for (Namespace n : parent.getNamespacesInherited()) {
                    if (n.getPrefix() == null || n.getPrefix().length() == 0) {
                        root = n;
                        break;
                    }
                }
                elem = factory.element(dm.getName(), root);
            }

            final String[] attributeNames = dm.getAttributeNames();
            for (final String attrName : attributeNames) {
                if (attrName.startsWith("xmlns:")) {
                    continue;
                }
                elem.setAttribute(attrName, dm.getAttribute(attrName));
            }

            insertAtPreferredLocation(parent, elem, counter);
            counter.increaseCount();
            replaceXpp3DOM(elem, dm, new IndentationCounter(counter.getDepth() + 1));
        }
    } else if (parentDom.getValue() != null) {
        List<Content> cl = parent.getContent();
        boolean foundCdata = false;
        for (Content c : cl) {
            if (c instanceof CDATA) {
                foundCdata = true;
            }
        }

        if (!foundCdata) {
            parent.setText(parentDom.getValue());
        }
    }
}

From source file:org.culturegraph.mf.jdom.StreamToJDomDocument.java

License:Apache License

public StreamToJDomDocument(final String rootTagName, final String namespaceProperties) {
    this.rootTagName = rootTagName;
    namespaces.put(XML, Namespace.getNamespace(XML, "http://www.w3.org/XML/1998/namespace"));
    final Properties properties;
    try {/*from  ww  w . j  av  a  2  s  .  c o m*/
        properties = ResourceUtil.loadProperties(namespaceProperties);
    } catch (IOException e) {
        throw new MetafactureException("Cannot load namespaces list", e);
    }
    for (Entry<Object, Object> entry : properties.entrySet()) {
        namespaces.put(entry.getKey().toString(),
                Namespace.getNamespace(entry.getKey().toString(), entry.getValue().toString()));
    }
}

From source file:org.educautecisystems.core.chat.elements.UserChat.java

License:Open Source License

public static String generateXMLFromList(ArrayList<UserChat> users) {
    Document xmlDocument = new Document();

    Namespace baseNamespace = Namespace.getNamespace("chat", "http://free.chat.com/");
    Element root = new Element("users", baseNamespace);

    for (UserChat user : users) {
        Element userXml = new Element("user", baseNamespace);

        userXml.addContent(new Element("id").setText("" + user.getId()));
        userXml.addContent(new Element("real_name").setText(user.getRealName()));
        userXml.addContent(new Element("nickname").setText(user.getNickName()));

        root.addContent(userXml);/*  w ww .j av  a  2 s  .c o  m*/
    }

    xmlDocument.setRootElement(root);
    XMLOutputter xmlOutputter = new XMLOutputter();

    return xmlOutputter.outputString(xmlDocument);
}

From source file:org.educautecisystems.core.chat.elements.UserChat.java

License:Open Source License

public static ArrayList<UserChat> generateListFromXML(String xml) {
    StringReader xmlSR = new StringReader(xml);
    ArrayList<UserChat> response = new ArrayList<UserChat>();

    SAXBuilder builder = new SAXBuilder();

    try {//from   ww  w  .  ja  va2s.c o m
        Document documentXML = builder.build(xmlSR);

        Element root = documentXML.getRootElement();
        Namespace baseNamespace = Namespace.getNamespace("chat", "http://free.chat.com/");

        /* Get all users */
        List<Element> users = root.getChildren("user", baseNamespace);

        for (Element user : users) {
            String id_string = user.getChildText("id");
            int id = 0;
            String realName = user.getChildText("real_name");
            String nickName = user.getChildText("nickname");

            try {
                id = Integer.parseInt(id_string);
            } catch (NumberFormatException nfe) {
                return null;
            }

            UserChat newUserChat = new UserChat(id, realName, nickName);
            response.add(newUserChat);
        }
    } catch (JDOMException jdome) {
        return null;
    } catch (IOException ioe) {
        return null;
    }

    return response;
}

From source file:org.educautecisystems.core.Sistema.java

License:Open Source License

private static void cargarConfPrincipal(File archivoConfPrincipal) {
    SAXBuilder builder = new SAXBuilder();
    Document documento = null;/* w ww.j  a v  a  2  s. c o m*/

    try {
        documento = builder.build(archivoConfPrincipal);
    } catch (JDOMException jdome) {
        System.err.println("JDOME: " + jdome);
    } catch (IOException ioe) {
        System.err.println("IOE: " + ioe);
    }

    Namespace baseNamespace = Namespace.getNamespace("eus", "http://educautecisystems.org/");
    Element root = documento.getRootElement();

    /* Informacin de la base de datos. */
    Element eBaseDeDatos = root.getChild("database", baseNamespace);
    confBaseDeDatos.setHost(eBaseDeDatos.getChildText("host"));
    confBaseDeDatos.setPort(eBaseDeDatos.getChildText("port"));
    confBaseDeDatos.setUser(eBaseDeDatos.getChildText("user"));
    confBaseDeDatos.setPassword(eBaseDeDatos.getChildText("password"));
    confBaseDeDatos.setEsquema(eBaseDeDatos.getChildText("esquema"));
}

From source file:org.educautecisystems.core.Sistema.java

License:Open Source License

public static void guardarConfPrincipal() {
    File archivoConfPrincipal = new File(pathGeneralConf);

    if (archivoConfPrincipal.exists()) {
        archivoConfPrincipal.delete();//  w  w w .  j  a va2s .  c  o m
    }

    Document documento = new Document();

    Namespace baseNamespace = Namespace.getNamespace("eus", "http://educautecisystems.org/");
    Element root = new Element("config", baseNamespace);
    documento.setRootElement(root);

    Element eBaseDeDatos = new Element("database", baseNamespace);
    eBaseDeDatos.addContent(new Element("host").setText(confBaseDeDatos.getHost()));
    eBaseDeDatos.addContent(new Element("port").setText(confBaseDeDatos.getPort()));
    eBaseDeDatos.addContent(new Element("user").setText(confBaseDeDatos.getUser()));
    eBaseDeDatos.addContent(new Element("password").setText(confBaseDeDatos.getPassword()));
    eBaseDeDatos.addContent(new Element("esquema").setText(confBaseDeDatos.getEsquema()));
    root.addContent(eBaseDeDatos);

    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

    try {
        FileOutputStream fis = new FileOutputStream(archivoConfPrincipal);
        outputter.output(documento, fis);
        fis.close();
    } catch (IOException ioe) {
        System.err.println("No se pudo escribor configuracin principal.");
    }
}

From source file:org.educautecisystems.core.Sistema.java

License:Open Source License

private static void cargarChatConf(File archivoConfChatXML) {
    ChatServerConf lChatServerConf = new ChatServerConf();
    ChatSessionConf lChatSessionConf = new ChatSessionConf();

    SAXBuilder builder = new SAXBuilder();
    Document documento = null;/* w  w  w. j  a  va 2s . c  o  m*/

    try {
        documento = builder.build(archivoConfChatXML);
    } catch (JDOMException jdome) {
        System.err.println("JDOME: " + jdome);
    } catch (IOException ioe) {
        System.err.println("IOE: " + ioe);
    }

    Namespace baseNamespace = Namespace.getNamespace("chat", "http://free.chat.com/");
    Element root = documento.getRootElement();

    /* Datos del servidor */
    Element eServidor = root.getChild("server", baseNamespace);
    lChatServerConf.setIp(eServidor.getChildText("ip"));
    lChatServerConf.setPort(eServidor.getChildText("port"));

    /* Datos de la sesin */
    Element eSession = root.getChild("session", baseNamespace);
    lChatSessionConf.setNickname(eSession.getChildText("nickname"));
    lChatSessionConf.setRealName(eSession.getChildText("real_name"));

    /* Guardar informacin */
    Sistema.chatServerConf = lChatServerConf;
    Sistema.chatSessionConf = lChatSessionConf;
}

From source file:org.educautecisystems.core.Sistema.java

License:Open Source License

private static void generarChatConf(File archivoConfChatXML) {
    Document document = new Document();

    Namespace baseNamespace = Namespace.getNamespace("chat", "http://free.chat.com/");
    Element root = new Element("config", baseNamespace);

    /* Datos servidor */
    Element eServidor = new Element("server", baseNamespace);
    eServidor.addContent(new Element("ip").setText(ip_defecto));
    eServidor.addContent(new Element("port").setText(port_defecto));
    root.addContent(eServidor);//from  w  w  w.ja va2  s. c o  m

    /* Datos sesin */
    Element eSession = new Element("session", baseNamespace);
    eSession.addContent(new Element("nickname").setText(nickname_defecto));
    eSession.addContent(new Element("real_name").setText(realName_defecto));
    root.addContent(eSession);

    /* Guardar archivo */
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    document.setRootElement(root);

    try {
        outputter.output(document, new FileOutputStream(archivoConfChatXML));
    } catch (IOException ioe) {
        System.err.println("No se puedo crear archivo de configuracin.");
    }

    /* Iniciar informacin */
    chatServerConf = new ChatServerConf(ip_defecto, port_defecto);
    chatSessionConf = new ChatSessionConf(nickname_defecto, realName_defecto);
}

From source file:org.educautecisystems.core.Sistema.java

License:Open Source License

public static void guardarChatConf() {
    File archivoConfChatXML = new File(pathChatConf);

    /* Borrar archivo, si existe. */
    if (archivoConfChatXML.exists()) {
        archivoConfChatXML.delete();/*from   ww  w.  j  a va 2 s . c om*/
    }

    Document document = new Document();

    Namespace baseNamespace = Namespace.getNamespace("chat", "http://free.chat.com/");
    Element root = new Element("config", baseNamespace);

    /* Datos servidor */
    Element eServidor = new Element("server", baseNamespace);
    eServidor.addContent(new Element("ip").setText(chatServerConf.getIp()));
    eServidor.addContent(new Element("port").setText(chatServerConf.getPort()));
    root.addContent(eServidor);

    /* Datos sesin */
    Element eSession = new Element("session", baseNamespace);
    eSession.addContent(new Element("nickname").setText(chatSessionConf.getNickname()));
    eSession.addContent(new Element("real_name").setText(chatSessionConf.getRealName()));
    root.addContent(eSession);

    /* Guardar archivo */
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    document.setRootElement(root);

    try {
        outputter.output(document, new FileOutputStream(archivoConfChatXML));
    } catch (IOException ioe) {
        System.err.println("No se puedo crear archivo de configuracin.");
    }
}

From source file:org.fnppl.opensdx.xml.Document.java

License:Open Source License

public static org.jdom2.Document buildJDOMDocument(Element openSDXRoot, String url) {
    org.jdom2.Element root = (org.jdom2.Element) openSDXRoot.base.detach();
    Namespace ns = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("noNamespaceSchemaLocation", url, ns);
    Document d = new Document();
    d.base = new org.jdom2.Document(root);
    return d.base;
}