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;
}
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);
}
}
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;
}
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();
}
}
}
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;
}
Aggregations