Example #1
0
 public static String XmlFilter(boolean isSoapReq, String value) {
   if ((value == null)
       || value.equals("")) { // do not use StringTools.isBlank (spaces are significant)
     return "";
   } else if (StringTools.isAlphaNumeric(value, XML_CHARS)) {
     return value; // return all significant spaces
   } else {
     String v = StringTools.replace(value, "\n", "\\n");
     return XMLTools.CDATA(isSoapReq, v);
   }
 }
  // http://code.google.com/apis/maps/documentation/geocoding/index.html
  public GeoPoint getGeocode(String address, String country) {

    /* URL */
    String url = this.getGeoPointGeocodeURL(address, country);
    Print.logDebug("Google GC URL: " + url);

    /* create XML document */
    Document xmlDoc = GetXMLDocument(url, this.getGeocodeTimeout());
    if (xmlDoc == null) {
      return null;
    }

    /* vars */
    String errCode = null;
    GeoPoint geoPoint = null;

    /* parse "xml" */
    Element kml = xmlDoc.getDocumentElement();
    if (kml.getTagName().equalsIgnoreCase(TAG_kml)) {
      NodeList ResponseList = XMLTools.getChildElements(kml, TAG_Response);
      for (int g = 0; (g < ResponseList.getLength()); g++) {
        Element response = (Element) ResponseList.item(g);
        NodeList responseNodes = response.getChildNodes();
        for (int n = 0; n < responseNodes.getLength(); n++) {
          Node responseNode = responseNodes.item(n);
          if (!(responseNode instanceof Element)) {
            continue;
          }
          Element responseElem = (Element) responseNode;
          String responseNodeName = responseElem.getNodeName();
          if (responseNodeName.equalsIgnoreCase(TAG_name)) {
            // <name>1600 Amphitheatre Parkway, Mountain View, CA</name>
          } else if (responseNodeName.equalsIgnoreCase(TAG_Status)) {
            // <Status> ... </Status>
            NodeList statusNodes = responseElem.getChildNodes();
            for (int st = 0; st < statusNodes.getLength(); st++) {
              Node statusNode = statusNodes.item(st);
              if (!(statusNode instanceof Element)) {
                continue;
              }
              Element statusElem = (Element) statusNode;
              String statusNodeName = statusElem.getNodeName();
              if (statusNodeName.equalsIgnoreCase(TAG_code)) {
                errCode = StringTools.trim(GoogleGeocodeV2.GetNodeText(statusElem)); // expect "200"
                break; // we only care about the 'code'
              }
            }
          } else if (responseNodeName.equalsIgnoreCase(TAG_Placemark)) {
            // <Placemark> ... </Placemark>
            String id = responseElem.getAttribute(ATTR_id);
            if ((id != null) && id.equals("p1")) {
              NodeList placemarkNodes = responseElem.getChildNodes();
              for (int pm = 0; (geoPoint == null) && (pm < placemarkNodes.getLength()); pm++) {
                Node placemarkNode = placemarkNodes.item(pm);
                if (!(placemarkNode instanceof Element)) {
                  continue;
                }
                Element placemarkElem = (Element) placemarkNode;
                String placemarkNodeName = placemarkElem.getNodeName();
                if (placemarkNodeName.equalsIgnoreCase(TAG_Point)) {
                  NodeList pointNodes = placemarkElem.getChildNodes();
                  for (int ptn = 0; (geoPoint == null) && (ptn < pointNodes.getLength()); ptn++) {
                    Node pointNode = pointNodes.item(ptn);
                    if (!(pointNode instanceof Element)) {
                      continue;
                    }
                    Element pointElem = (Element) pointNode;
                    String pointNodeName = pointElem.getNodeName();
                    if (pointNodeName.equalsIgnoreCase(TAG_coordinates)) {
                      String ll[] = StringTools.split(GoogleGeocodeV2.GetNodeText(pointElem), ',');
                      if (ll.length >= 2) {
                        double lon = StringTools.parseDouble(ll[0], 0.0); // longitude is first
                        double lat = StringTools.parseDouble(ll[1], 0.0);
                        if (GeoPoint.isValid(lat, lon)) {
                          geoPoint = new GeoPoint(lat, lon);
                          break; // we only care about the 'GeoPoint'
                        }
                      }
                    }
                  }
                }
              }
            } else {
              // Print.logInfo("Skipping Placemark ID = %s", id);
            }
          }
        }
      }
    }

    /* create address */
    if (geoPoint != null) {
      // GeoPoint found
      return geoPoint;
    }

    /* check for Google reverse-geocode limit exceeded */
    if ((errCode != null) && errCode.equals("620")) {
      Print.logError("!!!! Google Reverse-Geocode Limit Exceeded [Error 620] !!!!");
    }

    /* no reverse-geocode available */
    return null;
  }
  /* return reverse-geocode using nearest address */
  public ReverseGeocode getAddressReverseGeocode(GeoPoint gp, String localeStr, boolean cache) {

    /* check for failover mode */
    if (this.isReverseGeocodeFailoverMode()) {
      ReverseGeocodeProvider frgp = this.getFailoverReverseGeocodeProvider();
      return frgp.getReverseGeocode(gp, localeStr, cache);
    }

    /* URL */
    String url = this.getAddressReverseGeocodeURL(gp, localeStr);
    Print.logDebug("Google RG URL: " + url);
    // byte xmlBytes[] = HTMLTools.readPage(url);

    /* create XML document */
    Document xmlDoc = GetXMLDocument(url, this.getReverseGeocodeTimeout());
    if (xmlDoc == null) {
      return null;
    }

    /* vars */
    String errCode = null;
    String address = null;

    /* parse "xml" */
    Element kml = xmlDoc.getDocumentElement();
    if (kml.getTagName().equalsIgnoreCase(TAG_kml)) {
      NodeList ResponseList = XMLTools.getChildElements(kml, TAG_Response);
      for (int g = 0; (g < ResponseList.getLength()); g++) {
        Element response = (Element) ResponseList.item(g);
        NodeList responseNodes = response.getChildNodes();
        for (int n = 0; n < responseNodes.getLength(); n++) {
          Node responseNode = responseNodes.item(n);
          if (!(responseNode instanceof Element)) {
            continue;
          }
          Element responseElem = (Element) responseNode;
          String responseNodeName = responseElem.getNodeName();
          if (responseNodeName.equalsIgnoreCase(TAG_name)) {
            // <name>40.479581,-117.773438</name>
          } else if (responseNodeName.equalsIgnoreCase(TAG_Status)) {
            // <Status> ... </Status>
            NodeList statusNodes = responseElem.getChildNodes();
            for (int st = 0; st < statusNodes.getLength(); st++) {
              Node statusNode = statusNodes.item(st);
              if (!(statusNode instanceof Element)) {
                continue;
              }
              Element statusElem = (Element) statusNode;
              String statusNodeName = statusElem.getNodeName();
              if (statusNodeName.equalsIgnoreCase(TAG_code)) {
                errCode = StringTools.trim(GoogleGeocodeV2.GetNodeText(statusElem)); // expect "200"
                break; // we only care about the 'code'
              }
            }
          } else if (responseNodeName.equalsIgnoreCase(TAG_Placemark)) {
            // <Placemark> ... </Placemark>
            String id = responseElem.getAttribute(ATTR_id);
            if ((id != null) && id.equals("p1")) {
              NodeList placemarkNodes = responseElem.getChildNodes();
              for (int pm = 0; pm < placemarkNodes.getLength(); pm++) {
                Node placemarkNode = placemarkNodes.item(pm);
                if (!(placemarkNode instanceof Element)) {
                  continue;
                }
                Element placemarkElem = (Element) placemarkNode;
                String placemarkNodeName = placemarkElem.getNodeName();
                if (placemarkNodeName.equalsIgnoreCase(TAG_address)) {
                  address = GoogleGeocodeV2.GetNodeText(placemarkElem);
                  break; // we only care about the 'address'
                }
              }
            } else {
              // Print.logInfo("Skipping Placemark ID = %s", id);
            }
          }
        }
      }
    }

    /* create address */
    if (FAILOVER_DEBUG) {
      errCode = "620";
    } else if (!StringTools.isBlank(address)) {
      // address found
      ReverseGeocode rg = new ReverseGeocode();
      rg.setFullAddress(address);
      return rg;
    }

    /* check for Google reverse-geocode limit exceeded */
    if ((errCode != null) && errCode.equals("620")) {
      Print.logError("!!!! Google Reverse-Geocode Limit Exceeded [Error 620] !!!!");
      if (this.hasFailoverReverseGeocodeProvider()) {
        this.startReverseGeocodeFailoverMode();
        ReverseGeocodeProvider frgp = this.getFailoverReverseGeocodeProvider();
        Print.logWarn("Failing over to '" + frgp.getName() + "'");
        return frgp.getReverseGeocode(gp, localeStr, cache);
      }
    }

    /* no reverse-geocode available */
    return null;
  }
Example #4
0
  private int _writeXML(PrintWriter out, int level, ReportData rd, boolean urlOnly)
      throws ReportException {
    boolean isSoapRequest = rd.isSoapRequest();
    RequestProperties reqState = rd.getRequestProperties();
    PrivateLabel privLabel = rd.getPrivateLabel();
    I18N i18n = privLabel.getI18N(ReportTable.class);
    String PFX1 = XMLTools.PREFIX(isSoapRequest, level * ReportTable.INDENT);
    String PFX2 = XMLTools.PREFIX(isSoapRequest, (level + 1) * ReportTable.INDENT);

    /* begin */
    out.print(PFX1);
    out.print(
        XMLTools.startTAG(
            isSoapRequest,
            "Report", // TAG_Report
            XMLTools.ATTR("name", rd.getReportName())
                + // ATTR_name
                XMLTools.ATTR("type", rd.getReportType()), // ATTR_type
            false,
            true));

    /* constraints */
    ReportConstraints rc = rd.getReportConstraints();
    String dtFmt = DateTime.DEFAULT_DATE_FORMAT + "," + DateTime.DEFAULT_TIME_FORMAT;
    TimeZone tzone = rd.getTimeZone();
    String tzStr = rd.getTimeZoneString();
    long tmBeg = rc.getTimeStart();
    long tmEnd = rc.getTimeEnd();
    DateTime dtStr = new DateTime(tmBeg, tzone);
    DateTime dtEnd = new DateTime(tmEnd, tzone);

    /* Account */
    out.print(PFX2);
    out.print(XMLTools.startTAG(isSoapRequest, "Account", "", false, false)); // TAG_Account
    out.print(XmlFilter(isSoapRequest, rd.getAccountID()));
    out.print(XMLTools.endTAG(isSoapRequest, "Account", true)); // TAG_Account

    /* TimeFrom */
    out.print(PFX2);
    out.print(
        XMLTools.startTAG(
            isSoapRequest,
            "TimeFrom", // TAG_TimeFrom
            XMLTools.ATTR("timestamp", String.valueOf(tmBeg))
                + // ATTR_timestamp
                XMLTools.ATTR("timezone", tzStr), // ATTR_timezone
            false,
            false));
    out.print((tmBeg > 0L) ? XmlFilter(isSoapRequest, dtStr.format(dtFmt)) : "");
    out.print(XMLTools.endTAG(isSoapRequest, "TimeFrom", true)); // TAG_TimeFrom

    /* TimeTo */
    out.print(PFX2);
    out.print(
        XMLTools.startTAG(
            isSoapRequest,
            "TimeTo", // TAG_TimeTo
            XMLTools.ATTR("timestamp", String.valueOf(tmEnd))
                + // ATTR_timestamp
                XMLTools.ATTR("timezone", tzStr), // ATTR_timezone
            false,
            false));
    out.print((tmEnd > 0L) ? XmlFilter(isSoapRequest, dtEnd.format(dtFmt)) : "");
    out.print(XMLTools.endTAG(isSoapRequest, "TimeTo", true)); // TAG_TimeTo

    /* ValidGPSRequired */
    out.print(PFX2);
    out.print(
        XMLTools.startTAG(
            isSoapRequest, "ValidGPSRequired", "", false, false)); // TAG_ValidGPSRequired
    out.print(XmlFilter(isSoapRequest, rc.getValidGPSRequired()));
    out.print(XMLTools.endTAG(isSoapRequest, "ValidGPSRequired", true)); // TAG_ValidGPSRequired

    /* SelectionLimit */
    out.print(PFX2);
    out.print(
        XMLTools.startTAG(
            isSoapRequest,
            "SelectionLimit", // TAG_SelectionLimit
            XMLTools.ATTR("type", rc.getSelectionLimitType()),
            false,
            false));
    out.print(XmlFilter(isSoapRequest, rc.getSelectionLimit()));
    out.print(XMLTools.endTAG(isSoapRequest, "SelectionLimit", true)); // TAG_SelectionLimit

    /* Ascending */
    out.print(PFX2);
    out.print(XMLTools.startTAG(isSoapRequest, "Ascending", "", false, false)); // TAG_Ascending
    out.print(XmlFilter(isSoapRequest, rc.getOrderAscending()));
    out.print(XMLTools.endTAG(isSoapRequest, "Ascending", true)); // TAG_Ascending

    /* ReportLimit */
    out.print(PFX2);
    out.print(XMLTools.startTAG(isSoapRequest, "ReportLimit", "", false, false)); // TAG_ReportLimit
    out.print(XmlFilter(isSoapRequest, rc.getReportLimit()));
    out.print(XMLTools.endTAG(isSoapRequest, "ReportLimit", true)); // TAG_ReportLimit

    /* Where */
    if (rc.hasWhere()) {
      out.print(PFX2);
      out.print(XMLTools.startTAG(isSoapRequest, "Where", "", false, false)); // TAG_Where
      out.print(XmlFilter(isSoapRequest, rc.getWhere()));
      out.print(XMLTools.endTAG(isSoapRequest, "Where", true)); // TAG_Where
    }

    /* RuleSelector */
    if (rc.hasRuleSelector()) {
      out.print(PFX2);
      out.print(
          XMLTools.startTAG(isSoapRequest, "RuleSelector", "", false, false)); // TAG_RuleSelector
      out.print(XmlFilter(isSoapRequest, rc.getRuleSelector()));
      out.print(XMLTools.endTAG(isSoapRequest, "RuleSelector", true)); // TAG_RuleSelector
    }

    /* Title */
    out.print(PFX2);
    out.print(XMLTools.startTAG(isSoapRequest, "Title", "", false, false)); // TAG_Title
    out.print(XmlFilter(isSoapRequest, rd.getReportTitle()));
    out.print(XMLTools.endTAG(isSoapRequest, "Title", true)); // TAG_Title

    /* Subtitle */
    out.print(PFX2);
    out.print(XMLTools.startTAG(isSoapRequest, "Subtitle", "", false, false)); // TAG_Subtitle
    out.print(XmlFilter(isSoapRequest, rd.getReportSubtitle()));
    out.print(XMLTools.endTAG(isSoapRequest, "Subtitle", true)); // TAG_Subtitle

    /* URL */
    if (urlOnly) {
      // Web-URL only
      HttpServletRequest request = reqState.getHttpServletRequest();
      ReportDeviceList devList = rd.getReportDeviceList();
      String deviceID = devList.isDeviceGroup() ? null : devList.getFirstDeviceID();
      String groupID = devList.isDeviceGroup() ? devList.getDeviceGroupID() : null;
      String baseURL =
          privLabel.hasDefaultBaseURL()
              ? privLabel.getDefaultBaseURL()
              : ((request != null) ? request.getRequestURL().toString() : "");
      URIArg rptURL =
          ReportURL.createReportURL(
              baseURL,
              false,
              rd.getAccountID(),
              rd.getUserID(),
              "",
              deviceID,
              groupID,
              rc.getTimeStart(),
              rc.getTimeEnd(),
              rd.getTimeZoneString(),
              rd.getReportName(),
              rc.getReportLimit(),
              rc.getSelectionLimitType().toString(),
              ReportPresentation.FORMAT_HTML);
      out.print(PFX2);
      out.print(XMLTools.startTAG(isSoapRequest, "URL", "", false, false)); // TAG_URL
      out.print(XmlFilter(isSoapRequest, rptURL.toString()));
      out.print(XMLTools.endTAG(isSoapRequest, "URL", true)); // TAG_URL
    } else {
      // Report header/body
      this.rptHeader.writeXML(out, level + 1, rd);
      this.rptBody.writeXML(out, level + 1, rd);
    }

    /* Partial */
    out.print(PFX2);
    out.print(XMLTools.startTAG(isSoapRequest, "Partial", "", false, false)); // TAG_Partial
    out.print(XmlFilter(isSoapRequest, this.rptBody.isPartial()));
    out.print(XMLTools.endTAG(isSoapRequest, "Partial", true)); // TAG_Partial

    /* end of report */
    out.print(PFX1);
    out.print(XMLTools.endTAG(isSoapRequest, "Report", true)); // TAG_Report
    return this.rptBody.getRecordCount();
  }