List of usage examples for org.w3c.dom Node getTextContent
public String getTextContent() throws DOMException;
From source file:com.linuxbox.enkive.filter.EnkiveFiltersBean.java
protected void startup() { if (LOGGER.isTraceEnabled()) LOGGER.trace("Loading Enkive filters"); try {/*from w w w . j a v a 2 s . com*/ Resource res = new ClassPathResource(ENKIVE_FILTERS_FILENAME); FileInputStream filterFile = new FileInputStream(res.getFile()); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(filterFile); NodeList filters = doc.getElementsByTagName(FILTER); doc.getElementsByTagName(DEFAULT_ACTION).item(0).getTextContent(); if (doc.getElementsByTagName(DEFAULT_ACTION).item(0).getTextContent().toLowerCase().equals(DENY)) defaultAction = FilterAction.DENY; for (int i = 0; i < filters.getLength(); i++) { Element filter = (Element) filters.item(i); if (filter.getAttribute(ENABLED).equals(FILTER_TRUE)) { int filterAction = 0; Node action = filter.getElementsByTagName(ACTION).item(0); if (action.getTextContent().toLowerCase().equals(ALLOW)) filterAction = FilterAction.ALLOW; if (action.getTextContent().toLowerCase().equals(DENY)) filterAction = FilterAction.DENY; Node header = filter.getElementsByTagName(HEADER).item(0); Node value = filter.getElementsByTagName(VALUE).item(0); int filterType = 0; int filterComparator = 0; if (((Element) value).getAttribute(TYPE).toLowerCase().equals(INTEGER)) filterType = FilterType.INTEGER; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(STRING)) filterType = FilterType.STRING; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(ADDRESS)) filterType = FilterType.ADDRESS; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(FLOAT)) filterType = FilterType.FLOAT; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(DATE)) filterType = FilterType.DATE; if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(IS_GREATER_THAN)) filterComparator = FilterComparator.IS_GREATER_THAN; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(IS_LESS_THAN)) filterComparator = FilterComparator.IS_LESS_THAN; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(CONTAINS)) filterComparator = FilterComparator.CONTAINS; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(DOES_NOT_CONTAIN)) filterComparator = FilterComparator.DOES_NOT_CONTAIN; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(MATCHES)) filterComparator = FilterComparator.MATCHES; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(DOES_NOT_MATCH)) filterComparator = FilterComparator.DOES_NOT_MATCH; filterSet.add(new EnkiveFilter(header.getTextContent(), filterAction, filterType, value.getTextContent(), filterComparator)); if (LOGGER.isTraceEnabled()) LOGGER.info("Enkive filtering by header " + header.getTextContent()); } } filterFile.close(); } catch (FileNotFoundException e) { LOGGER.fatal("Could not find enkive-filters.xml, Filters not initialized"); } catch (IOException e) { LOGGER.fatal("Could not read file enkive-filters.xml, Filters not initialized"); } catch (ParserConfigurationException e) { LOGGER.fatal("Could not initialize parser for enkive-filters.xml, Filters not initialized"); } catch (SAXException e) { LOGGER.fatal("Could not parse enkive-filters.xml, Filters not initialized"); } }
From source file:de.cuseb.bilderbuch.images.DeviantArtImageSearch.java
@Override public List<Image> searchImages(String query) throws ImageSearchException { List<Image> result = new ArrayList<Image>(); try {/*ww w . j a va 2 s . c o m*/ HttpClient client = httpClientProviderService.getHttpClient(); HttpUriRequest request = RequestBuilder.get().setUri("http://backend.deviantart.com/rss.xml") .addParameter("limit", "10").addParameter("q", "in=photography " + query).build(); HttpResponse response = client.execute(request); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(response.getEntity().getContent()); NodeList items = document.getElementsByTagName("item"); for (int i = 0; i < items.getLength(); i++) { Element item = (Element) items.item(i); NodeList title = item.getElementsByTagName("title"); NodeList content = item.getElementsByTagName("media:content"); if (content.getLength() > 0) { Node url = content.item(0).getAttributes().getNamedItem("url"); if (url != null) { Image image = new Image(); image.setSource("deviantart"); image.setUrl(url.getTextContent()); image.setTitle(title.item(0).getFirstChild().getTextContent()); result.add(image); } } } } catch (Exception e) { log.error("searchImages", e); } return result; }
From source file:eu.guardiansystems.livesapp.android.ui.GMapDirections.java
public String getDurationText(Document doc) { try {// ww w . j a v a2 s .c o m NodeList nl1 = doc.getElementsByTagName("duration"); Node node1 = nl1.item(0); NodeList nl2 = node1.getChildNodes(); Node node2 = nl2.item(getNodeIndex(nl2, "text")); return node2.getTextContent(); } catch (Exception e) { return "0"; } }
From source file:com.mirth.connect.model.converters.NCPDPSerializer.java
@Override public Map<String, String> getMetadataFromDocument(Document document) { Map<String, String> metadata = new HashMap<String, String>(); String serviceProviderId = StringUtils.EMPTY; if ((document != null) && (document.getElementsByTagName("ServiceProviderId") != null)) { Node sender = document.getElementsByTagName("ServiceProviderId").item(0); if (sender != null) { serviceProviderId = sender.getTextContent(); }/* www .java 2 s .c o m*/ } String transactionCode = StringUtils.EMPTY; if ((document != null) && (document.getElementsByTagName("TransactionCode") != null)) { Node type = document.getElementsByTagName("TransactionCode").item(0); if (type != null) { transactionCode = NCPDPReference.getInstance().getTransactionName(type.getTextContent()); } } String versionReleaseNumber = "5.1"; if ((document != null) && (document.getElementsByTagName("VersionReleaseNumber") != null)) { Node versionNode = document.getElementsByTagName("VersionReleaseNumber").item(0); if (versionNode != null) { versionReleaseNumber = versionNode.getTextContent(); } } metadata.put("version", versionReleaseNumber); metadata.put("type", transactionCode); metadata.put("source", serviceProviderId); return metadata; }
From source file:nz.co.jsrsolutions.tideservice.geocoding.GoogleTideDataGeoCoder.java
@Override public GeoLocation getGeoLocation(Port port) throws TideDataGeoCoderException { URI uri;/* ww w .j a v a2 s . c om*/ try { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(port.getName()); stringBuffer.append(", "); stringBuffer.append(port.getSubArea().getName()); uri = mUriBuilder.buildGetGeoLocUri(stringBuffer.toString()); HttpUriRequest request = new GoogleGeoCoderHttpGet(uri); HttpResponse response = mHttpClient.execute(request); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new TideDataGeoCoderException( "HTTP GET returned: " + response.getStatusLine().getStatusCode()); } // read result and parse into XML Document HttpEntity entity = response.getEntity(); Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(entity.getContent()); // prepare XPath XPath xpath = XPathFactory.newInstance().newXPath(); // extract the result NodeList resultNodeList = null; // a) Examine the status resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/status", geocoderResultDocument, XPathConstants.NODESET); if (resultNodeList.getLength() != 1) { throw new TideDataGeoCoderException("Couldn't parse result document."); } GoogleGeoCoderResultStatus resultStatus = GoogleGeoCoderResultStatus .valueOf(resultNodeList.item(0).getTextContent()); switch (resultStatus) { case OK: break; case ZERO_RESULTS: throw new TideDataGeoCoderException("No results found for: " + stringBuffer.toString()); case OVER_QUERY_LIMIT: throw new TideDataGeoCoderException("Over query limit (was 2500 requests per day)"); case REQUEST_DENIED: throw new TideDataGeoCoderException("Request denied (using sensor=true|false ?)"); case INVALID_REQUEST: throw new TideDataGeoCoderException("Invalid request - badly formed query"); } // c) extract the coordinates of the first result resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*", geocoderResultDocument, XPathConstants.NODESET); float lat = Float.NaN; float lng = Float.NaN; for (int i = 0; i < resultNodeList.getLength(); ++i) { Node node = resultNodeList.item(i); if ("lat".equals(node.getNodeName())) { lat = Float.parseFloat(node.getTextContent()); } if ("lng".equals(node.getNodeName())) { lng = Float.parseFloat(node.getTextContent()); } } GeoLocation geoLocation = new GeoLocation(); geoLocation.setLatitude((long) (lat * 1e6)); geoLocation.setLongitude((long) (lng * 1e6)); return geoLocation; } catch (URISyntaxException e) { throw new TideDataGeoCoderException(e); } catch (ClientProtocolException e) { throw new TideDataGeoCoderException(e); } catch (IOException e) { throw new TideDataGeoCoderException(e); } catch (IllegalStateException e) { throw new TideDataGeoCoderException(e); } catch (SAXException e) { throw new TideDataGeoCoderException(e); } catch (ParserConfigurationException e) { throw new TideDataGeoCoderException(e); } catch (XPathExpressionException e) { throw new TideDataGeoCoderException(e); } }
From source file:com.amalto.core.save.context.BeforeSaving.java
public void save(SaverSession session, DocumentSaverContext context) { // Invoke the beforeSaving MutableDocument updateReportDocument = context.getUpdateReportDocument(); if (updateReportDocument == null) { throw new IllegalStateException("Update report is missing."); //$NON-NLS-1$ }// w w w. j a v a2 s .com OutputReport outputreport = session.getSaverSource().invokeBeforeSaving(context, updateReportDocument); if (outputreport != null) { // when a process was found String errorCode; message = outputreport.getMessage(); if (!validateFormat(message)) throw new BeforeSavingFormatException(message); try { Document doc = Util.parse(message); // handle output_report String xpath = "//report/message"; //$NON-NLS-1$ Node errorNode; synchronized (XPATH) { errorNode = (Node) XPATH.evaluate(xpath, doc, XPathConstants.NODE); } errorCode = null; if (errorNode instanceof Element) { Element errorElement = (Element) errorNode; errorCode = errorElement.getAttribute("type"); //$NON-NLS-1$ Node child = errorElement.getFirstChild(); if (child instanceof org.w3c.dom.Text) { message = child.getTextContent(); } else { message = ""; //$NON-NLS-1$ } } if (!"info".equals(errorCode)) { //$NON-NLS-1$ throw new BeforeSavingErrorException(message); } // handle output_item if (outputreport.getItem() != null) { xpath = "//exchange/item"; //$NON-NLS-1$ SkipAttributeDocumentBuilder builder = new SkipAttributeDocumentBuilder( SaverContextFactory.DOCUMENT_BUILDER, false); doc = builder.parse(new InputSource(new StringReader(outputreport.getItem()))); Node item; synchronized (XPATH) { item = (Node) XPATH.evaluate(xpath, doc, XPathConstants.NODE); } if (item != null && item instanceof Element) { Node node = null; Node current = item.getFirstChild(); while (current != null) { if (current instanceof Element) { node = current; break; } current = item.getNextSibling(); } if (node != null) { // set back the modified item by the process DOMDocument document = new DOMDocument(node, context.getUserDocument().getType(), context.getDataCluster(), context.getDataModelName()); context.setUserDocument(document); context.setDatabaseDocument(null); // Redo a set of actions and security checks. // TMDM-4599: Adds UpdateReport phase so a new update report is generated based on latest changes. next = new ID( new GenerateActions(new Security(new UpdateReport(new ApplyActions(next))))); } } } } catch (Exception e) { throw new RuntimeException("Exception occurred during before saving phase.", e); //$NON-NLS-1$ } } next.save(session, context); }
From source file:eu.guardiansystems.livesapp.android.ui.GMapDirections.java
public int getDurationValue(Document doc) { try {/* ww w . j a va 2 s . c om*/ NodeList nl1 = doc.getElementsByTagName("duration"); Node node1 = nl1.item(0); NodeList nl2 = node1.getChildNodes(); Node node2 = nl2.item(getNodeIndex(nl2, "value")); return Integer.parseInt(node2.getTextContent()); } catch (Exception e) { return -1; } }
From source file:eu.planets_project.tb.impl.services.tags.DefaultServiceTagHandlerImpl.java
private void parseTags() { int defaultPriority = 2; NodeList tags = root.getElementsByTagName("tag"); for (int k = 0; k < tags.getLength(); k++) { ServiceTagImpl serTag = new ServiceTagImpl(); Node tag = tags.item(k);/* www .j a va2 s . com*/ NodeList tag_children = tag.getChildNodes(); NamedNodeMap tagattributes = tag.getAttributes(); try { //priority not mandatory - may have a default value int priority = Integer.parseInt(tagattributes.getNamedItem("priority").getNodeValue()); serTag.setPriority(priority); } catch (Exception e) { serTag.setPriority(defaultPriority); } for (int i = 0; i < tag_children.getLength(); i++) { Node child = tag_children.item(i); if (child.getNodeName().equals("name") && child.getNodeType() == Node.ELEMENT_NODE) { //set name serTag.setName(child.getTextContent()); } if (child.getNodeName().equals("description") && child.getNodeType() == Node.ELEMENT_NODE) { //set description serTag.setDescription(child.getTextContent()); } } //store tag, name is id this.hmDefaultTags.put(serTag.getName(), serTag); } }
From source file:eu.guardiansystems.livesapp.android.ui.GMapDirections.java
public String getDistanceText(Document doc) { /*/*from w w w . ja va 2s. c o m*/ * while (en.hasMoreElements()) { type type = (type) en.nextElement(); * * } */ try { NodeList nl1; nl1 = doc.getElementsByTagName("distance"); Node node1 = nl1.item(nl1.getLength() - 1); NodeList nl2 = null; nl2 = node1.getChildNodes(); Node node2 = nl2.item(getNodeIndex(nl2, "value")); return node2.getTextContent(); } catch (Exception e) { return "-1"; } /* * NodeList nl1; if(doc.getElementsByTagName("distance")!=null){ nl1= * doc.getElementsByTagName("distance"); * * Node node1 = nl1.item(nl1.getLength() - 1); NodeList nl2 = null; if * (node1.getChildNodes() != null) { nl2 = node1.getChildNodes(); Node * node2 = nl2.item(getNodeIndex(nl2, "value")); Log.d("DistanceText", * node2.getTextContent()); return node2.getTextContent(); } else return * "-1";} else return "-1"; */ }