Search in sources :

Example 16 with Format

use of org.jdom2.output.Format in project opentheso by miledrousset.

the class GpsQuery method queryGps2.

public ArrayList<NodeAlignment> queryGps2(String idC, String idTheso, String lexicalValue, String lang, String requete) {
    ArrayList<NodeLang> nodeLangs;
    try {
        lexicalValue = URLEncoder.encode(lexicalValue, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(GpsQuery.class.getName()).log(Level.SEVERE, null, ex);
    }
    lexicalValue = lexicalValue.replaceAll(" ", "%20");
    requete = requete.replace("##lang##", lang);
    requete = requete.replace("##value##", lexicalValue);
    try {
        // URL url = new URL("https://" + lang + ".wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=xml&srsearch=" + lexicalValue + "&srnamespace=0");
        // requete = URLEncoder.encode(requete, "UTF-8");
        URL url = new URL(requete);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/xml");
        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        String output;
        String xmlRecord = "";
        while ((output = br.readLine()) != null) {
            xmlRecord += output;
        }
        byte[] bytes = xmlRecord.getBytes();
        xmlRecord = new String(bytes, Charset.forName("UTF-8"));
        conn.disconnect();
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlRecord));
            org.w3c.dom.Document doc = db.parse(is);
            NodeList nodes = doc.getElementsByTagName("geoname");
            listeAlign = new ArrayList<>();
            // iterate the employees
            for (int i = 0; i < nodes.getLength(); i++) {
                nodeLangs = new ArrayList<>();
                org.w3c.dom.Element element = (org.w3c.dom.Element) nodes.item(i);
                NodeAlignment nodeAlignment = new NodeAlignment();
                NodeList nodeList = element.getElementsByTagName("name");
                for (int j = 0; j < nodeList.getLength(); j++) {
                    nodeAlignment.setName(nodeList.item(j).getTextContent());
                }
                nodeList = element.getElementsByTagName("lat");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setLat(Double.parseDouble(nodeList.item(j).getTextContent()));
                    }
                }
                nodeList = element.getElementsByTagName("lng");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setLng(Double.parseDouble(nodeList.item(j).getTextContent()));
                    }
                }
                nodeList = element.getElementsByTagName("geonameId");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setIdUrl("http://www.geonames.org/" + (nodeList.item(j).getTextContent()));
                    }
                }
                nodeList = element.getElementsByTagName("countryName");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setCountryName(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("toponymName");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setToponymName(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("adminName1");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setAdminName1(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("adminName2");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setAdminName2(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("alternateName");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        NodeLang nodeLang = new NodeLang();
                        // nodeAlignment.setToponymName(p.item(j).getTextContent());
                        ArrayList<String> langueFound = new ArrayList<>();
                        org.w3c.dom.Element el = (org.w3c.dom.Element) nodeList.item(j);
                        if (el.hasAttribute("lang")) {
                            nodeLang.setCode(el.getAttribute("lang"));
                            nodeLang.setValue(nodeList.item(j).getTextContent());
                        }
                        nodeLangs.add(nodeLang);
                    }
                    nodeAlignment.setAlltraductions(nodeLangs);
                }
                listeAlign.add(nodeAlignment);
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return listeAlign;
}
Also used : InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) URL(java.net.URL) SAXException(org.xml.sax.SAXException) HttpURLConnection(java.net.HttpURLConnection) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) InputStreamReader(java.io.InputStreamReader) NodeList(org.w3c.dom.NodeList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeLang(mom.trd.opentheso.bdd.helper.nodes.NodeLang) BufferedReader(java.io.BufferedReader)

Example 17 with Format

use of org.jdom2.output.Format in project weicoder by wdcode.

the class XMLWriteJDom2 method output.

/**
 * 输出XML文档
 * @param doc Document对象
 * @param out 输出流
 */
public void output(Document doc, OutputStream out) {
    // 获得out
    this.out = out;
    // 实例化 XMLOutputter
    writer = new XMLOutputter(((FormatJDom2) format).getFormat());
    // 写Document
    try {
        writer.output(((DocumentJDom2) doc).getDocument(), out);
    } catch (IOException e) {
        Logs.error(e);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) IOException(java.io.IOException)

Example 18 with Format

use of org.jdom2.output.Format in project pwm by pwm-project.

the class StoredConfigurationImpl method fromXml.

public static StoredConfigurationImpl fromXml(final InputStream xmlData) throws PwmUnrecoverableException {
    final Instant startTime = Instant.now();
    // validateXmlSchema(xmlData);
    final Document inputDocument = XmlUtil.parseXml(xmlData);
    final StoredConfigurationImpl newConfiguration = StoredConfigurationImpl.newStoredConfiguration();
    try {
        newConfiguration.document = inputDocument;
        // verify create time;
        newConfiguration.createTime();
        ConfigurationCleaner.cleanup(newConfiguration);
    } catch (Exception e) {
        final String errorMsg = "error reading configuration file format, error=" + e.getMessage();
        final ErrorInformation errorInfo = new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { errorMsg });
        throw new PwmUnrecoverableException(errorInfo);
    }
    checkIfXmlRequiresUpdate(newConfiguration);
    LOGGER.debug("successfully loaded configuration (" + TimeDuration.compactFromCurrent(startTime) + ")");
    return newConfiguration;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) Instant(java.time.Instant) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) Document(org.jdom2.Document) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmOperationalException(password.pwm.error.PwmOperationalException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException)

Example 19 with Format

use of org.jdom2.output.Format in project pwm by pwm-project.

the class XmlUtil method outputDocument.

public static void outputDocument(final Document document, final OutputStream outputStream) throws IOException {
    final Format format = Format.getPrettyFormat();
    format.setEncoding(STORAGE_CHARSET.toString());
    final XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(format);
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(outputStream, STORAGE_CHARSET);
        outputter.output(document, writer);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Format(org.jdom2.output.Format) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 20 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRBibTeXEntryTransformer method buildSourceExtension.

private Element buildSourceExtension(BibtexEntry entry) {
    Element source = new Element("source");
    source.setAttribute("format", "bibtex");
    source.setText(entry.toString());
    return source;
}
Also used : Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)57 Document (org.jdom2.Document)20 XMLOutputter (org.jdom2.output.XMLOutputter)19 IOException (java.io.IOException)15 StringWriter (java.io.StringWriter)9 Attribute (org.jdom2.Attribute)9 Format (org.jdom2.output.Format)7 File (java.io.File)5 ArrayList (java.util.ArrayList)5 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)5 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)5 JsonWriter (com.google.gson.stream.JsonWriter)4 Date (java.util.Date)4 SAXBuilder (org.jdom2.input.SAXBuilder)4 SimpleDateFormat (java.text.SimpleDateFormat)3 JDOMException (org.jdom2.JDOMException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 JComponent (javax.swing.JComponent)2