List of usage examples for org.jdom2 Element getNamespace
public Namespace getNamespace()
From source file:com.eds.Model.XMLProcessor.java
License:Apache License
public ApiErrorMessage ProcessError(String errorNumber, String errorStream) { ByteArrayInputStream errorInputStream = new ByteArrayInputStream(errorStream.getBytes()); InputStreamReader in = new InputStreamReader(errorInputStream); BufferedReader errorreader = new BufferedReader(in); ApiErrorMessage apiErrorMessage = new ApiErrorMessage(); try {// w w w . ja va2 s.c o m if (errorNumber.equals(HTTP_BAD_REQUEST)) { String line = ""; String resultListErrorStream = ""; try { while ((line = errorreader.readLine()) != null) { resultListErrorStream += line; } } catch (IOException e) { apiErrorMessage.setErrorDescription("Error occurred"); apiErrorMessage.setDetailedErrorDescription(e.getMessage()); return apiErrorMessage; } StringReader stringReader = new StringReader(resultListErrorStream); InputSource inputSource = new InputSource(stringReader); Document doc = (new SAXBuilder()).build(inputSource); if (doc.getRootElement().getName() == "APIErrorMessage") { Element root = doc.getRootElement(); String detailedErrorDescription = root.getChildText("DetailedErrorDescription", root.getNamespace()); String errorDescription = root.getChildText("ErrorDescription", root.getNamespace()); String errorNum = root.getChildText("ErrorNumber", root.getNamespace()); apiErrorMessage.setDetailedErrorDescription(detailedErrorDescription); apiErrorMessage.setErrorDescription(errorDescription); apiErrorMessage.setErrorNumber(errorNum); } else { apiErrorMessage.setDetailedErrorDescription(errorStream); apiErrorMessage.setErrorDescription(errorStream); apiErrorMessage.setErrorNumber(errorNumber); } } } catch (Exception e) { apiErrorMessage.setErrorDescription("Error occurred"); apiErrorMessage.setDetailedErrorDescription(e.getMessage()); } return apiErrorMessage; }
From source file:com.eds.Model.XMLProcessor.java
License:Apache License
public ApiErrorMessage ProcessAuthError(String errorNumber, String errorStream) { ByteArrayInputStream errorInputStream = new ByteArrayInputStream(errorStream.getBytes()); InputStreamReader in = new InputStreamReader(errorInputStream); BufferedReader errorreader = new BufferedReader(in); ApiErrorMessage apiErrorMessage = new ApiErrorMessage(); try {/*w w w . j ava2 s . c o m*/ if (errorNumber.equals(HTTP_BAD_REQUEST)) { String line = ""; String resultListErrorStream = ""; try { while ((line = errorreader.readLine()) != null) { resultListErrorStream += line; } } catch (IOException e) { apiErrorMessage.setErrorDescription("Error occurred"); apiErrorMessage.setDetailedErrorDescription(e.getMessage()); return apiErrorMessage; } StringReader stringReader = new StringReader(resultListErrorStream); InputSource inputSource = new InputSource(stringReader); Document doc = (new SAXBuilder()).build(inputSource); if (doc.getRootElement().getName() == "ErrorResponseMessage") { Element root = doc.getRootElement(); String detailedErrorDescription = root.getChildText("AdditionalDetail", root.getNamespace()); String errorDescription = root.getChildText("Reason", root.getNamespace()); String errorNum = root.getChildText("ErrorCode", root.getNamespace()); apiErrorMessage.setDetailedErrorDescription(detailedErrorDescription); apiErrorMessage.setErrorDescription(errorDescription); apiErrorMessage.setErrorNumber(errorNum); } else { apiErrorMessage.setDetailedErrorDescription(errorStream); apiErrorMessage.setErrorDescription(errorStream); apiErrorMessage.setErrorNumber(errorNumber); } } } catch (Exception e) { apiErrorMessage.setErrorDescription("Error occurred"); apiErrorMessage.setDetailedErrorDescription(e.getMessage()); } return apiErrorMessage; }
From source file:com.eds.Model.XMLProcessor.java
License:Apache License
private Result constructRecord(Element xmlRecord, Boolean parse) { Result result = new Result(); // Get Record Id String resultId = xmlRecord.getChildText("ResultId", xmlRecord.getNamespace()); result.setResultId(resultId);/* w w w . j a v a2s . c o m*/ // Get Header Info Element header = xmlRecord.getChild("Header", xmlRecord.getNamespace()); if (null != header) { String dbId = header.getChildText("DbId", header.getNamespace()); String dbLabel = header.getChildText("DbLabel", header.getNamespace()); String an = header.getChildText("An", header.getNamespace()); String pubType = header.getChildText("PubType", header.getNamespace()); String pubTypeId = header.getChildText("PubTypeId", header.getNamespace()); result.setDbId(dbId); result.setDbLabel(dbLabel); result.setPubTypeID(pubTypeId); result.setAn(an); result.setPubType(pubType); } // Get PLink String pLink = xmlRecord.getChildText("PLink", xmlRecord.getNamespace()); result.setpLink(pLink); // Get ImageInfo Element imageInfo = xmlRecord.getChild("ImageInfo", xmlRecord.getNamespace()); if (imageInfo != null) { List<Element> coverArts = imageInfo.getChildren(); for (int b = 0; b < coverArts.size(); b++) { Element coverArt = (Element) coverArts.get(b); if (null != coverArt) { BookJacket bookJacket = new BookJacket(); String size = coverArt.getChildText("Size", coverArt.getNamespace()); String target = coverArt.getChildText("Target", coverArt.getNamespace()); bookJacket.setSize(size); bookJacket.setTarget(target); result.getBookJacketList().add(bookJacket); } } } // Get Custom Links Element customLinks = xmlRecord.getChild("CustomLinks", xmlRecord.getNamespace()); if (customLinks != null) { List<Element> customLinksList = customLinks.getChildren(); for (int c = 0; c < customLinksList.size(); c++) { Element cl = (Element) customLinksList.get(c); if (null != cl) { String clurl = cl.getChildText("Url", cl.getNamespace()); String name = cl.getChildText("Name", cl.getNamespace()); String category = cl.getChildText("Category", cl.getNamespace()); String text = cl.getChildText("Text", cl.getNamespace()); String icon = cl.getChildText("Icon", cl.getNamespace()); String mouseOverText = cl.getChildText("MouseOverText", cl.getNamespace()); CustomLink customLink = new CustomLink(); customLink.setUrl(clurl); customLink.setName(name); customLink.setCategory(category); customLink.setText(text); customLink.setIcon(icon); customLink.setMouseOverText(mouseOverText); result.getCustomLinkList().add(customLink); } } } // Get Full Text Info Element fullText = xmlRecord.getChild("FullText", xmlRecord.getNamespace()); result.setHtmlAvailable("0"); result.setPdfAvailable("0"); if (null != fullText) { Element text = fullText.getChild("Text", fullText.getNamespace()); if (null != text) { // 0 - embedded full text is not available // 1 - full text is available // -1 - database not configured to provide full text to // guests String htmlAvailable = text.getChildText("Availability", fullText.getNamespace()); if (null != htmlAvailable && !htmlAvailable.isEmpty() && htmlAvailable.equals("1")) { result.setHtmlAvailable("1"); String htmlFullTextValue = text.getChildText("Value", fullText.getNamespace()); if (null != htmlFullTextValue && !htmlFullTextValue.isEmpty()) { if (parse) htmlFullTextValue = Jsoup.parse(htmlFullTextValue).text().toString(); // translate data to valid HTML tags htmlFullTextValue = TransDataToHTML.transDataToHTML(htmlFullTextValue); } result.setHtmlFullText(htmlFullTextValue); } result.setHtmlAvailable(htmlAvailable); } // determine whether or not there are full text links Element linkElement = fullText.getChild("Links", fullText.getNamespace()); if (null != linkElement) { List<Element> links = linkElement.getChildren(); if (null != links && 0 < links.size()) { ArrayList<Link> otherLinks = new ArrayList<Link>(); for (int j = 0; j < links.size(); j++) { Element link = (Element) links.get(j); String type = link.getChildText("Type", link.getNamespace()); String url = link.getChildText("Url", link.getNamespace()); if (null != type && type.equals("pdflink")) { result.setPdfAvailable("1"); result.setPdfLink(url); } else if (null != type && !type.isEmpty()) { Link otherFTLink = new Link(); otherFTLink.setType(type); otherFTLink.setUrl(url); otherLinks.add(otherFTLink); } } if (!otherLinks.isEmpty()) result.setOtherFullTextLinks(otherLinks); } } } // Process Items Element items = xmlRecord.getChild("Items", xmlRecord.getNamespace()); if (null != items) { List<Element> itemList = items.getChildren(); for (int j = 0; j < itemList.size(); j++) { Element itemElement = (Element) itemList.get(j); Item item = new Item(); String label = itemElement.getChildText("Label", itemElement.getNamespace()); String group = itemElement.getChildText("Group", itemElement.getNamespace()); String itemData = itemElement.getChildText("Data", itemElement.getNamespace()); if (parse) itemData = Jsoup.parse(itemData).text().toString(); // translate data to valid HTML tags itemData = TransDataToHTML.transDataToHTML(itemData); item.setLabel(label); item.setGroup(group); item.setData(itemData); result.getItemList().add(item); } } return result; }
From source file:com.eds.Response.XMLProcessor.java
License:Apache License
/** * Constructs a result list in response to an EDS API Search Request *///from w ww. j a v a2 s. c o m public ResultsList buildResultsList(Response response) { ResultsList resultsList = new ResultsList(); String resultsListXML = ""; if (null != response.getErrorStream() && !response.getErrorStream().isEmpty()) { resultsList.setApierrormessage(ProcessError(response.getErrorNumber(), response.getErrorStream())); } else { BufferedReader reader = response.getRead(); if (null != reader) { try { String line = ""; while ((line = reader.readLine()) != null) { resultsListXML += line; } } catch (IOException e) { ApiErrorMessage errorMessage = new ApiErrorMessage(); errorMessage.setErrorDescription("Error processing resultList response"); errorMessage.setDetailedErrorDescription(e.getMessage()); resultsList.setApierrormessage(errorMessage); } } try { StringReader stringReader = new StringReader(resultsListXML); InputSource inputSource = new InputSource(stringReader); Document doc = (new SAXBuilder()).build(inputSource); // root element (level 1), handle resultsList Element searchResponseMessageGet = doc.getRootElement(); // level 2 elements if (null == searchResponseMessageGet) return resultsList; Element searchRequestGet = searchResponseMessageGet.getChild("SearchRequestGet", searchResponseMessageGet.getNamespace()); Element searchResult = searchResponseMessageGet.getChild("SearchResult", searchResponseMessageGet.getNamespace()); // level 3 elements if (null != searchRequestGet) { Element queryString = searchRequestGet.getChild("QueryString", searchRequestGet.getNamespace()); // Get Query String String querystring = queryString.getContent(0).getValue(); resultsList.setQueryString(querystring); } Element statistics = searchResult.getChild("Statistics", searchResult.getNamespace()); Element data = searchResult.getChild("Data", searchResult.getNamespace()); /* * In next steps, elements will be analyzed separately */ // Get Total Hits and Total Search Time String totalHits = "0"; if (null != statistics) { totalHits = statistics.getContent(0).getValue(); String totalSearchTime = statistics.getContent(1).getValue(); resultsList.setHits(totalHits); resultsList.setSearchTime(totalSearchTime); } if (Integer.parseInt(totalHits) > 0 && null != data) { // Get Results Element records = data.getChild("Records", data.getNamespace()); if (null != records && null != records.getChildren()) { List<Element> recordsList = records.getChildren(); for (int i = 0; i < recordsList.size(); i++) { Element xmlRecord = (Element) recordsList.get(i); if (null == xmlRecord) continue; Record record = new Record(); record = constructRecord(xmlRecord); resultsList.getResultsList().add(record); } } } } catch (Exception e) { ApiErrorMessage errorMessage = new ApiErrorMessage(); errorMessage.setErrorDescription("Error processing search response"); errorMessage.setDetailedErrorDescription(e.getMessage()); resultsList.setApierrormessage(errorMessage); } } return resultsList; }
From source file:com.eds.Response.XMLProcessor.java
License:Apache License
/** * Constructs a retrieve response object from the EDS API response XML * message//from ww w .j a v a 2s. c om */ public RetrieveResult buildRecord(Response response) { RetrieveResult retrieveResult = new RetrieveResult(); BufferedReader reader = response.getRead(); String RecordXML = ""; if (null != response.getErrorStream() && !response.getErrorStream().isEmpty()) { retrieveResult.setApiErrorMessage(ProcessError(response.getErrorNumber(), response.getErrorStream())); } else { try { String line = ""; while ((line = reader.readLine()) != null) { RecordXML += line; } } catch (IOException e) { ApiErrorMessage errorMessage = new ApiErrorMessage(); errorMessage.setErrorDescription("Error processing resultList response"); errorMessage.setDetailedErrorDescription(e.getMessage()); retrieveResult.setApiErrorMessage(errorMessage); } try { StringReader stringReader = new StringReader(RecordXML); InputSource inputSource = new InputSource(stringReader); Document doc = (new SAXBuilder()).build(inputSource); // ---------------begin to handle record // root element (level 1) Element data = doc.getRootElement(); // level 2 elements // Get Results Element xmlRecord = data.getChild("Record", data.getNamespace()); Record record = null; if (null != xmlRecord) record = constructRecord(xmlRecord, true); retrieveResult.setRecord(record); } catch (Exception e) { ApiErrorMessage errorMessage = new ApiErrorMessage(); errorMessage.setErrorDescription("Error processing search response"); errorMessage.setDetailedErrorDescription(e.getMessage()); retrieveResult.setApiErrorMessage(errorMessage); } } return retrieveResult; }
From source file:com.eds.Response.XMLProcessor.java
License:Apache License
private Record constructRecord(Element xmlRecord, Boolean parse) { Record record = new Record(); // Get Record Id String resultId = xmlRecord.getChildText("ResultId", xmlRecord.getNamespace()); record.setResultId(resultId);// w ww .java 2s .c o m // Get Header Info Element header = xmlRecord.getChild("Header", xmlRecord.getNamespace()); if (null != header) { String dbId = header.getChildText("DbId", header.getNamespace()); String dbLabel = header.getChildText("DbLabel", header.getNamespace()); String an = header.getChildText("An", header.getNamespace()); String pubType = header.getChildText("PubType", header.getNamespace()); String pubTypeId = header.getChildText("PubTypeId", header.getNamespace()); record.setDbId(dbId); record.setDbLabel(dbLabel); record.setPubTypeId(pubTypeId); record.setAn(an); record.setPubType(pubType); } // Get PLink String pLink = xmlRecord.getChildText("PLink", xmlRecord.getNamespace()); record.setpLink(pLink); // Get ImageInfo Element imageInfo = xmlRecord.getChild("ImageInfo", xmlRecord.getNamespace()); if (imageInfo != null) { List<Element> coverArts = imageInfo.getChildren(); for (int b = 0; b < coverArts.size(); b++) { Element coverArt = (Element) coverArts.get(b); if (null != coverArt) { BookJacket bookJacket = new BookJacket(); String size = coverArt.getChildText("Size", coverArt.getNamespace()); String target = coverArt.getChildText("Target", coverArt.getNamespace()); bookJacket.setSize(size); bookJacket.setTarget(target); record.getBookJacketList().add(bookJacket); } } } // Get Custom Links Element customLinks = xmlRecord.getChild("CustomLinks", xmlRecord.getNamespace()); if (customLinks != null) { List<Element> customLinksList = customLinks.getChildren(); for (int c = 0; c < customLinksList.size(); c++) { Element cl = (Element) customLinksList.get(c); if (null != cl) { String clurl = cl.getChildText("Url", cl.getNamespace()); String name = cl.getChildText("Name", cl.getNamespace()); String category = cl.getChildText("Category", cl.getNamespace()); String text = cl.getChildText("Text", cl.getNamespace()); String icon = cl.getChildText("Icon", cl.getNamespace()); String mouseOverText = cl.getChildText("MouseOverText", cl.getNamespace()); CustomLink customLink = new CustomLink(); customLink.setUrl(clurl); customLink.setName(name); customLink.setCategory(category); customLink.setText(text); customLink.setIcon(icon); customLink.setMouseOverText(mouseOverText); record.getCustomLinkList().add(customLink); } } } // Get Full Text Info Element fullText = xmlRecord.getChild("FullText", xmlRecord.getNamespace()); if (null != fullText) { Element htmlFullText = fullText.getChild("Text", fullText.getNamespace()); if (null != htmlFullText) { String availability = htmlFullText.getChildText("Availability", fullText.getNamespace()); record.setHtml(availability); } } // Process Items Element items = xmlRecord.getChild("Items", xmlRecord.getNamespace()); if (null != items) { List<Element> itemList = items.getChildren(); for (int j = 0; j < itemList.size(); j++) { Element itemElement = (Element) itemList.get(j); Item item = new Item(); String label = itemElement.getChildText("Label", itemElement.getNamespace()); String group = itemElement.getChildText("Group", itemElement.getNamespace()); String itemData = itemElement.getChildText("Data", itemElement.getNamespace()); if (parse) itemData = Jsoup.parse(itemData).text().toString(); // translate data to valid HTML tags itemData = TransDataToHTML.transDataToHTML(itemData); item.setLabel(label); item.setGroup(group); item.setData(itemData); record.getItemList().add(item); } } return record; }
From source file:com.globalsight.dispatcher.bo.JobTask.java
License:Apache License
private void createTargetFile(JobBO p_job, String[] p_targetSegments) throws IOException { OutputStream writer = null;/*www .j a v a2 s. co m*/ File fileStorage = CommonDAO.getFileStorage(); File srcFile = p_job.getSrcFile(); Account account = DispatcherDAOFactory.getAccountDAO().getAccount(p_job.getAccountId()); File trgDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + p_job.getJobID() + File.separator + AppConstants.XLF_TARGET_FOLDER); File trgFile = new File(trgDir, srcFile.getName()); FileUtils.copyFile(srcFile, trgFile); String encoding = FileUtil.getEncodingOfXml(trgFile); try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(p_job.getSrcFile()); Element root = doc.getRootElement(); // Get root element Namespace namespace = root.getNamespace(); Element fileElem = root.getChild("file", namespace); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace); List<Element> tuList = expr.evaluate(fileElem.getChild("body", namespace)); for (int tuIndex = 0, trgIndex = 0; tuIndex < tuList.size() && trgIndex < p_targetSegments.length; tuIndex++, trgIndex++) { if (p_targetSegments[trgIndex] == null) { continue; } Element elem = (Element) tuList.get(tuIndex); Element srcElem = elem.getChild("source", namespace); Element trgElem = elem.getChild("target", namespace); if (srcElem == null || srcElem.getContentSize() == 0) { trgIndex--; continue; } if (trgElem != null) { setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding); } else { trgElem = new Element("target", namespace); setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding); elem.addContent(trgElem); } } XMLOutputter xmlOutput = new XMLOutputter(); Format format = Format.getRawFormat(); format.setEncoding(encoding); writer = new FileOutputStream(trgFile); xmlOutput.setFormat(format); writeBOM(writer, format.getEncoding()); xmlOutput.output(doc, writer); p_job.setTrgFile(trgFile); logger.info("Create Target File: " + trgFile); } catch (JDOMException e1) { logger.error("CreateTargetFile Error: ", e1); } catch (IOException e1) { logger.error("CreateTargetFile Error: ", e1); } finally { if (writer != null) writer.close(); } }
From source file:com.globalsight.dispatcher.bo.JobTask.java
License:Apache License
private void setTargetSegment(Element p_trgElement, String p_target, String p_encoding) throws UnsupportedEncodingException { if (p_target == null || p_target.trim().length() == 0) return;/*from w w w. ja v a2s.c om*/ String target = new String(p_target.getBytes("UTF-8"), p_encoding); try { StringReader stringReader = new StringReader("<target>" + p_target + "</target>"); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(stringReader); Element elem = doc.getRootElement().clone().detach(); setNamespace(elem, p_trgElement.getNamespace()); //Delete Original Target Segment. p_trgElement.removeContent(); for (int i = 0; i < elem.getContentSize(); i++) { p_trgElement.addContent(elem.getContent(i).clone().detach()); } } catch (Exception e) { p_trgElement.setText(target); } }
From source file:com.globalsight.dispatcher.controller.TranslateXLFController.java
License:Apache License
private String parseXLF(JobBO p_job, File p_srcFile) { if (p_srcFile == null || !p_srcFile.exists()) return "File not exits."; String srcLang, trgLang;/*w ww .j a va 2 s.c o m*/ List<String> srcSegments = new ArrayList<String>(); try { SAXBuilder builder = new SAXBuilder(); Document read_doc = builder.build(p_srcFile); // Get Root Element Element root = read_doc.getRootElement(); Namespace namespace = root.getNamespace(); Element fileElem = root.getChild("file", namespace); // Get Source/Target Language srcLang = fileElem.getAttributeValue(XLF_SOURCE_LANGUAGE); trgLang = fileElem.getAttributeValue(XLF_TARGET_LANGUAGE); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace); List<Element> list = expr.evaluate(fileElem.getChild("body", namespace)); for (int i = 0; i < list.size(); i++) { Element tuElem = (Element) list.get(i); Element srcElem = tuElem.getChild("source", namespace); // Get Source Segment if (srcElem != null && srcElem.getContentSize() > 0) { String source = getInnerXMLString(srcElem); srcSegments.add(source); } } p_job.setSourceLanguage(srcLang); p_job.setTargetLanguage(trgLang); p_job.setSourceSegments(srcSegments); } catch (Exception e) { String msg = "Parse XLIFF file error."; logger.error(msg, e); return msg; } return null; }
From source file:com.izforge.izpack.util.xmlmerge.mapper.NamespaceFilterMapper.java
License:Open Source License
@Override public Element map(Element patchElement) { if (patchElement == null) { return null; }/*ww w .ja v a 2 s .c o m*/ if (patchElement.getNamespace().equals(m_namespace)) { return null; } else { return filterAttributes(patchElement); } }