/** Get XHTML view */ private String getXHTMLView(PriorityQueue priority_locs) { StringBuilder sbView = new StringBuilder( "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><div><h1>Look for the most convenient Store, according to your current position:</h1><p><ul>"); int n = priority_locs.size(); // Retrieve all the locations from the queue, the retrieved locations are sorted (closest // Location is retrieved first) for (int i = 0; i < n; ++i) { Location loc = (Location) priority_locs.poll(); String name = loc.getName(); String distance = ((Double) loc.getDistance()).toString(); sbView.append("<li>" + name + ":    " + distance + " km</li>"); } sbView.append("</ul></p></div></body></html>"); return sbView.toString(); }
/** Get XML view */ private InputStream getXMLViewAsInputStream(PriorityQueue priority_locs) throws Exception { Document doc = XMLHelper.createDocument(null, "locations"); // Retrieve all the locations from the queue, the retrieved locations are sorted (closest // Location is retrieved first) int numberOfLocations = priority_locs .size(); // INFO: Please note that poll() will change the size, hence set it as fixed // variable first! for (int i = 0; i < numberOfLocations; ++i) { Location loc = (Location) priority_locs.poll(); String name = loc.getName(); String distance = roundToOneDecimal(loc.getDistance()); // log.debug("Distance: " + loc.getDistance() + ", " + distance); // sbView.append("<loc name=\"" + name + "\" distance=\"" + distance + "\" id=\"" + // loc.getID() + "\" href=\"" + "TODO" + "\"/>"); Element locElem = doc.createElement("loc"); locElem.setAttribute("name", name); locElem.setAttribute("distance", distance); locElem.setAttribute("id", loc.getID()); // locElem.setAttribute("href", "TODO"); doc.getDocumentElement().appendChild(locElem); } return XMLHelper.getInputStream(doc, false, false, null); }
/** @see org.wyona.yanel.impl.resources.BasicXMLResource#getContentXML(String) */ @Override protected InputStream getContentXML(String viewId) throws Exception { HttpServletRequest request = (HttpServletRequest) getEnvironment().getRequest(); String query = request.getQueryString(); if ((query == "" || query == null) && !(viewId == "json")) { String view = first_view(request); return new StringBufferInputStream(view); } else { String long_here = ""; String lat_here = ""; long_here = request.getParameter("long"); lat_here = request.getParameter("lat"); // long_here = "13.408056"; // lat_here = "52.518611"; log.debug("Longitude: " + long_here); log.debug("Latitude: " + lat_here); if (long_here == "" || lat_here == "" || long_here == null || lat_here == null) { StringBuilder sbView_disabled = new StringBuilder( "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><div><h1>Look for the most convenient Store, according to your current position:</h1><p><ul>"); sbView_disabled.append( "<li>The geolocation of your firefox is disabled. We're sorry but we can't provide you with the closest location.</li></ul></p></div></body></html>"); return new StringBufferInputStream(sbView_disabled.toString()); } else { Location users_loc = new Location(Double.parseDouble(lat_here), Double.parseDouble(long_here)); Document xmlDoc; String xmlPath = getResourceConfigProperty("xml-path"); if (xmlPath == null || xmlPath == "") { File xmlFile = org.wyona.commons.io.FileUtil.file( rtd.getConfigFile().getParentFile().getAbsolutePath(), "xml" + File.separator + "locations.xml"); xmlDoc = XMLHelper.readDocument(new FileInputStream(xmlFile)); log.warn( "No custom/specific locations file specified, hence default file is read: " + rtd.getConfigFile().getParentFile().getAbsolutePath() + "xml" + File.separator + "locations.xml"); } else { xmlDoc = XMLHelper.readDocument(getRealm().getRepository().getNode(xmlPath).getInputStream()); } Element element = xmlDoc.getDocumentElement(); Element[] childElements = XMLHelper.getElements(element, "node", "", ""); // Get all the locations specified in the xmlFile // fill ArrayLists with the Locations (including Name) ArrayList<Location> locations = new ArrayList(); int i; for (i = 0; i < childElements.length; ++i) { String name = ""; String longitude = ""; String latitude = ""; NodeList text_Node_list; org.w3c.dom.Node final_text_node; Element current_text_element = null; Element current_element = childElements[i]; // INFO: Get the name Element[] name_elements = XMLHelper.getChildElements(current_element, "name", null); String contentLanguage = getContentLanguage(); log.debug( "Number of 'name' elements: " + name_elements.length + " (Content language : " + contentLanguage + ")"); if (name_elements.length >= 1) { try { name = getElementValue(name_elements[0]); if (name_elements.length > 1) { for (int j = 0; j < name_elements.length; j++) { String langAttr = name_elements[j].getAttribute("xml:lang"); log.debug("Language of name element: " + langAttr); if (langAttr != null && langAttr.equals(contentLanguage)) { name = getElementValue(name_elements[j]); break; } } } else { log.info("Name '" + name + "' seems to exist only in one language."); } } catch (Exception e) { log.error(e.getMessage()); return new StringBufferInputStream(getErrorMessageAsXHTML(e).toString()); } } else { log.error("Location does not seem to have name!"); StringBuilder sbView_error2 = new StringBuilder( "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><div><h1>Look for the most convenient Store, according to your current position:</h1><p><ul>"); sbView_error2.append( "<li>We're sorry an error occured.</li></ul></p></div></body></html>"); return new StringBufferInputStream(sbView_error2.toString()); } // INFO: Get the latitude Element[] text_elements = XMLHelper.getElements(current_element, "latitude", "", ""); current_text_element = text_elements[0]; text_Node_list = current_text_element.getChildNodes(); final_text_node = text_Node_list.item(0); // log.warn("How many names elements:" + // java.lang.Integer.toString((Integer)(text_Node_list.getLength()))); latitude = final_text_node.getNodeValue(); // Get the longitude text_elements = XMLHelper.getElements(current_element, "longitude", "", ""); current_text_element = text_elements[0]; text_Node_list = current_text_element.getChildNodes(); final_text_node = text_Node_list.item(0); // log.warn("How many names elements:" + // java.lang.Integer.toString((Integer)(text_Node_list.getLength()))); longitude = final_text_node.getNodeValue(); // log.warn("This is the "+ ((Integer)i).toString() +"location with: " + longitude + // latitude+ "long, lat"); // Collect all the predifined Location, we need to compare to the User's Location locations.add( new Location( Double.parseDouble(latitude), Double.parseDouble(longitude), name, current_element.getAttribute("id"))); } int n = locations.toArray().length; PriorityQueue<Location> priority_locs = new PriorityQueue(); // Calculate Distance for all Locations; for (i = 0; i < n; i++) { Location current_location = locations.get(i); double dist = GeoUtil.getDistance(users_loc, current_location); // Set distance in order to copmare it accordingly (Comparision of a Location-Class // Instance is difined by their its set distance) current_location.setDistance(dist); priority_locs.add(current_location); } if (viewId.equals("json")) { log.warn("DEBUG: Get JSON view..."); return getXMLViewAsInputStream(priority_locs); // return new StringBufferInputStream(getXMLView(priority_locs)); } else { log.warn("DEBUG: Get XML/XHTML view..."); return new StringBufferInputStream(getXHTMLView(priority_locs)); } } } }