private String serializeStackTrace(ExceptionInfo exception) {
    StringBuffer text = new StringBuffer();

    if (exception.getStackTrace() != null
        && exception.getStackTrace().length > getMaxStackTraceLength()) {
      text.append("<s more=\"")
          .append(exception.getStackTrace().length - getMaxStackTraceLength())
          .append("\">");
    } else {
      text.append("<s>");
    }
    StackTraceElement[] stackTrace = exception.getStackTrace();
    if (stackTrace != null) {

      for (int i = 0; i < stackTrace.length && i < getMaxStackTraceLength(); i++) {
        text.append("<e ");
        text.append("c=\"")
            .append(StringEscapeUtils.escapeXml(stackTrace[i].getClassName()))
            .append("\" ");
        text.append("m=\"")
            .append(StringEscapeUtils.escapeXml(stackTrace[i].getMethodName()))
            .append("\" ");
        text.append("f=\"")
            .append(StringEscapeUtils.escapeXml(stackTrace[i].getFileName()))
            .append("\" ");
        text.append("l=\"").append(stackTrace[i].getLineNumber()).append("\" ");
        text.append("/>");
      }
    }
    text.append("</s>");
    return text.toString();
  }
示例#2
0
    private void writeLogs(final cgCache cache) throws IOException {
      final List<LogEntry> logs = cache.getLogs();
      if (logs.size() <= 0) {
        return;
      }
      gpx.write("<groundspeak:logs>");

      for (LogEntry log : logs) {
        gpx.write("<groundspeak:log id=\"");
        gpx.write(Integer.toString(log.id));
        gpx.write("\">");

        gpx.write("<groundspeak:date>");
        gpx.write(StringEscapeUtils.escapeXml(dateFormatZ.format(new Date(log.date))));
        gpx.write("</groundspeak:date>");

        gpx.write("<groundspeak:type>");
        gpx.write(StringEscapeUtils.escapeXml(log.type.type));
        gpx.write("</groundspeak:type>");

        gpx.write("<groundspeak:finder id=\"\">");
        gpx.write(StringEscapeUtils.escapeXml(log.author));
        gpx.write("</groundspeak:finder>");

        gpx.write("<groundspeak:text encoded=\"False\">");
        gpx.write(StringEscapeUtils.escapeXml(log.log));
        gpx.write("</groundspeak:text>");

        gpx.write("</groundspeak:log>");
      }

      gpx.write("</groundspeak:logs>");
    }
示例#3
0
  public static void main(String args[]) {

    // handling xml special character & in Java String
    String xmlWithSpecial = "Java & HTML"; // xml String with & as special
    // characters
    System.out.println("Original unescaped XML String: " + xmlWithSpecial);
    System.out.println(
        "Escaped XML String in Java: " + StringEscapeUtils.escapeXml(xmlWithSpecial));

    // handling xml special character > in String on Java
    xmlWithSpecial = "Java > HTML"; // xml String with & as special
    // characters
    System.out.println("Original unescaped XML String: " + xmlWithSpecial);
    System.out.println("Escaped XML String : " + StringEscapeUtils.escapeXml(xmlWithSpecial));

    // handling xml and html special character < in String
    xmlWithSpecial = "Java < HTML"; // xml String with & as special
    // characters
    System.out.println("Original unescaped XML String: " + xmlWithSpecial);
    System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));

    // handling html and xml special character " in Java
    xmlWithSpecial = "Java \" HTML"; // xml String with & as special
    // characters
    System.out.println("Original unescaped XML String: " + xmlWithSpecial);
    System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));

    // handling xml special character ' in String from Java
    xmlWithSpecial = "Java ' HTML"; // xml String with & as special
    // characters
    System.out.println("Original unescaped XML String: " + xmlWithSpecial);
    System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
  }
 @Override
 public String renderReasons(List<ReportReason> reasons) {
   StringBuffer text = new StringBuffer();
   text.append("<reasons>");
   for (ReportReason reason : reasons) {
     text.append("<reason level=\"")
         .append(StringEscapeUtils.escapeXml(reason.getLevel()))
         .append("\">")
         .append(StringEscapeUtils.escapeXml(reason.getReason()))
         .append("</reason>");
   }
   text.append("</reasons>");
   return text.toString();
 }
 @Override
 public void write(OsmRelation relation) {
   out.print("  <relation id=\"" + relation.getId() + "\"");
   if (printMetadata) {
     OsmMetadata metadata = relation.getMetadata();
     printMetadata(metadata);
   }
   if (relation.getNumberOfTags() == 0 && relation.getNumberOfMembers() == 0) {
     out.println("/>");
   } else {
     out.println(">");
     for (int i = 0; i < relation.getNumberOfMembers(); i++) {
       OsmRelationMember member = relation.getMember(i);
       EntityType type = member.getType();
       String t = type == EntityType.Node ? "node" : type == EntityType.Way ? "way" : "relation";
       String role = member.getRole();
       role = StringEscapeUtils.escapeXml(role);
       out.println(
           "    <member type=\""
               + t
               + "\" ref=\""
               + member.getId()
               + "\" role=\""
               + role
               + "\"/>");
     }
     printTags(relation);
     out.println("  </relation>");
   }
 }
示例#6
0
    private void writeAttributes(final cgCache cache) throws IOException {
      if (!cache.hasAttributes()) {
        return;
      }
      // TODO: Attribute conversion required: English verbose name, gpx-id
      gpx.write("<groundspeak:attributes>");

      for (String attribute : cache.getAttributes()) {
        final CacheAttribute attr =
            CacheAttribute.getByGcRawName(CacheAttribute.trimAttributeName(attribute));
        final boolean enabled = CacheAttribute.isEnabled(attribute);

        gpx.write("<groundspeak:attribute id=\"");
        gpx.write(Integer.toString(attr.id));
        gpx.write("\" inc=\"");
        if (enabled) {
          gpx.write('1');
        } else {
          gpx.write('0');
        }
        gpx.write("\">");
        gpx.write(StringEscapeUtils.escapeXml(attr.getL10n(enabled)));
        gpx.write("</groundspeak:attribute>");
      }

      gpx.write("</groundspeak:attributes>");
    }
示例#7
0
  /**
   * Recursively write the XML data for the node tree starting at <code>node</code>.
   *
   * @param indent white space to indent child nodes
   * @param printWriter writer where child nodes are written
   */
  @Override
  protected void printXml(final String indent, final PrintWriter printWriter) {
    printWriter.print(indent + "<");
    printOpeningTagContentAsXml(printWriter);

    printWriter.print(">");
    printWriter.print(StringEscapeUtils.escapeXml(getText()));
    printWriter.print("</textarea>");
  }
  public String escapeXml(String text) {

    String value = text;
    if (text == null) {
      return text;
    } else {
      value = StringEscapeUtils.escapeXml(value);
    }
    return value;
  }
  private String serializeExceptionInfo(ExceptionInfo exception) {
    StringBuffer text = new StringBuffer();

    text.append("<n>").append(StringEscapeUtils.escapeXml(exception.getClassName())).append("</n>");

    String message = "";
    if (exception.getMessageName() != null) {
      message = exception.getMessageName();
    }
    text.append("<m>").append(StringEscapeUtils.escapeXml(message)).append("</m>");

    text.append(serializeStackTrace(exception));

    if (exception.getCause() != null) {
      text.append("<c>").append(serializeExceptionInfo(exception.getCause())).append("</c>");
    }

    return text.toString();
  }
  public String render(ReportNode node) {
    if (node != null) {
      StringBuffer text = new StringBuffer();
      text.append("<r c=\"")
          .append(StringEscapeUtils.escapeXml(node.getClass().getSimpleName()))
          .append("\" ");

      String icon = node.getIcon();
      if (icon != null) {
        text.append("i=\"").append(StringEscapeUtils.escapeXml(icon)).append("\" ");
      }

      if (node.getDate() != null) {
        text.append("t=\"").append("" + node.getDate().getTime()).append("\" ");
      }

      text.append("l=\"").append(StringEscapeUtils.escapeXml(node.getLevel())).append("\" ");

      text.append("d=\"").append(node.getDebug()).append("\" ");

      text.append(">");
      if (node.getTitle() != null) {
        text.append("<t>").append(StringEscapeUtils.escapeXml(node.getTitle())).append("</t>");
      }

      if (node.getHint() != null) {
        text.append("<h>").append(StringEscapeUtils.escapeXml(node.getHint())).append("</h>");
      }

      if (node instanceof TextReportNode) {
        TextReportNode textNode = (TextReportNode) node;
        if (textNode.getDetails() != null) {
          text.append("<d>")
              .append(StringEscapeUtils.escapeXml(textNode.getDetails()))
              .append("</d>");
        }
      } else if (node instanceof ExceptionReportNode) {
        ExceptionReportNode exceptionNode = (ExceptionReportNode) node;
        text.append("<e>")
            .append(serializeExceptionInfo(exceptionNode.getException()))
            .append("</e>");
      } else if (node instanceof BranchReportNode) {
        BranchReportNode branch = (BranchReportNode) node;
        text.append("<c>");
        if (branch.getChildNodes() != null) {
          for (ReportNode rn : branch.getChildNodes()) {
            text.append(render(rn));
          }
        }
        text.append("</c>");
      }

      text.append("</r>");
      return text.toString();
    } else {
      throw new IllegalArgumentException("Main report node should not be null");
    }
  }
  private void writeDeveloperError(WebView webView, Exception e, OutputStream entityStream) {
    try {
      // Note:
      // This could be seen as a "server error", but we don't set the Status header to 500 because
      // we want to be
      // able to distinguish between actual server errors (internal Mayocat Shop server error) and
      // theme
      // developers errors (which this is).
      // This is comes at play when setting up monitoring with alerts on a number of 5xx response
      // above a
      // certain threshold.

      // Re-serialize the context as json with indentation for better debugging
      ObjectMapper mapper = new ObjectMapper();
      mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
      Map<String, Object> context = webView.data();
      String jsonContext = mapper.writeValueAsString(context);
      Template error =
          new Template(
              "developerError",
              Resources.toString(
                  Resources.getResource("templates/developerError.html"), Charsets.UTF_8));
      Map<String, Object> errorContext = Maps.newHashMap();
      errorContext.put(
          "error", StringEscapeUtils.escapeXml(cleanErrorMessageForDisplay(e.getMessage())));
      errorContext.put("stackTrace", StringEscapeUtils.escapeXml(ExceptionUtils.getStackTrace(e)));
      errorContext.put("context", StringEscapeUtils.escapeXml(jsonContext).trim());
      errorContext.put("rawContext", jsonContext);
      errorContext.put("template", webView.template().toString());

      engine.get().register(error);
      String rendered = engine.get().render(error.getId(), mapper.writeValueAsString(errorContext));
      entityStream.write(rendered.getBytes());
    } catch (Exception e1) {
      throw new RuntimeException(e1);
    }
  }
  public Map<String, Object> getAllProperties(Document doc) {

    Map<String, Object> allProperties = new HashMap<String, Object>();
    String rawdoc = doc.getContent();
    List<String> properties = getPropertiesNames();

    for (String propertyName : properties) {
      String propertyValue =
          org.apache.commons.lang3.StringUtils.substringsBetween(
              rawdoc, "<" + propertyName + ">", "</" + propertyName + ">")[0];
      propertyValue = org.apache.commons.lang3.StringEscapeUtils.escapeXml(propertyValue);

      allProperties.put(propertyName, propertyValue);
    }

    return allProperties;
  }
示例#13
0
    /**
     * Writes one waypoint entry for cache waypoint.
     *
     * @param cache The
     * @param wp
     * @param prefix
     * @throws IOException
     */
    private void writeCacheWaypoint(final cgWaypoint wp, final String prefix) throws IOException {
      gpx.write("<wpt lat=\"");
      final Geopoint coords = wp.getCoords();
      gpx.write(
          coords != null
              ? Double.toString(coords.getLatitude())
              : ""); // TODO: check whether is the best way to handle unknown waypoint coordinates
      gpx.write("\" lon=\"");
      gpx.write(coords != null ? Double.toString(coords.getLongitude()) : "");
      gpx.write("\">");

      gpx.write("<name>");
      gpx.write(StringEscapeUtils.escapeXml(prefix));
      gpx.write(StringEscapeUtils.escapeXml(wp.getGeocode().substring(2)));
      gpx.write("</name>");

      gpx.write("<cmt>");
      gpx.write(StringEscapeUtils.escapeXml(wp.getNote()));
      gpx.write("</cmt>");

      gpx.write("<desc>");
      gpx.write(StringEscapeUtils.escapeXml(wp.getName()));
      gpx.write("</desc>");

      gpx.write("<sym>");
      gpx.write(
          StringEscapeUtils.escapeXml(
              wp.getWaypointType().toString())); // TODO: Correct identifier string
      gpx.write("</sym>");

      gpx.write("<type>Waypoint|");
      gpx.write(
          StringEscapeUtils.escapeXml(
              wp.getWaypointType().toString())); // TODO: Correct identifier string
      gpx.write("</type>");

      gpx.write("</wpt>");
    }
示例#14
0
 /** Xml 转码. */
 public static String escapeXml(String xml) {
   return StringEscapeUtils.escapeXml(xml);
 }
示例#15
0
 public static String clean(String sXML) {
   return StringEscapeUtils.escapeXml(sXML);
 }
示例#16
0
 /**
  * The strategy for escaping markup. The default is escape for XML.
  *
  * @param raw maybe <code>null</code>.
  * @return maybe <code>null</code> if null for input.
  * @see #text(String)
  */
 protected String escapeMarkup(String raw) {
   return StringEscapeUtils.escapeXml(raw);
 }
示例#17
0
    @Override
    protected Boolean doInBackground(Void... params) {
      // quick check for being able to write the GPX file
      if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return false;
      }

      try {
        final File exportLocation = new File(Settings.getGpxExportDir());
        exportLocation.mkdirs();

        final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        exportFile =
            new File(
                Settings.getGpxExportDir()
                    + File.separatorChar
                    + "export_"
                    + fileNameDateFormat.format(new Date())
                    + ".gpx");

        gpx = new BufferedWriter(new FileWriter(exportFile));

        gpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        gpx.write(
            "<gpx version=\"1.0\" creator=\"c:geo - http://www.cgeo.org\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd\">");

        for (int i = 0; i < caches.size(); i++) {
          // reload the cache. otherwise logs, attributes and other detailed information is not
          // available
          final cgCache cache =
              cgeoapplication
                  .getInstance()
                  .loadCache(caches.get(i).getGeocode(), LoadFlags.LOAD_ALL_DB_ONLY);

          gpx.write("<wpt ");
          gpx.write("lat=\"");
          gpx.write(Double.toString(cache.getCoords().getLatitude()));
          gpx.write("\" ");
          gpx.write("lon=\"");
          gpx.write(Double.toString(cache.getCoords().getLongitude()));
          gpx.write("\">");

          final Date hiddenDate = cache.getHiddenDate();
          if (hiddenDate != null) {
            gpx.write("<time>");
            gpx.write(StringEscapeUtils.escapeXml(dateFormatZ.format(hiddenDate)));
            gpx.write("</time>");
          }

          gpx.write("<name>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getGeocode()));
          gpx.write("</name>");

          gpx.write("<desc>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getName()));
          gpx.write("</desc>");

          gpx.write("<url>");
          gpx.write(cache.getUrl());
          gpx.write("</url>");

          gpx.write("<urlname>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getName()));
          gpx.write("</urlname>");

          gpx.write("<sym>");
          gpx.write(cache.isFound() ? "Geocache Found" : "Geocache");
          gpx.write("</sym>");

          gpx.write("<type>");
          gpx.write(StringEscapeUtils.escapeXml("Geocache|" + cache.getType().pattern));
          gpx.write("</type>");

          gpx.write("<groundspeak:cache ");
          gpx.write("id=\"");
          gpx.write(cache.getCacheId());
          gpx.write("\" available=\"");
          gpx.write(!cache.isDisabled() ? "True" : "False");
          gpx.write("\" archived=\"");
          gpx.write(cache.isArchived() ? "True" : "False");
          gpx.write("\" ");
          gpx.write("xmlns:groundspeak=\"http://www.groundspeak.com/cache/1/0/1\">");

          gpx.write("<groundspeak:name>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getName()));
          gpx.write("</groundspeak:name>");

          gpx.write("<groundspeak:placed_by>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getOwnerDisplayName()));
          gpx.write("</groundspeak:placed_by>");

          gpx.write("<groundspeak:owner>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getOwnerUserId()));
          gpx.write("</groundspeak:owner>");

          gpx.write("<groundspeak:type>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getType().pattern));
          gpx.write("</groundspeak:type>");

          gpx.write("<groundspeak:container>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getSize().id));
          gpx.write("</groundspeak:container>");

          writeAttributes(cache);

          gpx.write("<groundspeak:difficulty>");
          gpx.write(Float.toString(cache.getDifficulty()));
          gpx.write("</groundspeak:difficulty>");

          gpx.write("<groundspeak:terrain>");
          gpx.write(Float.toString(cache.getTerrain()));
          gpx.write("</groundspeak:terrain>");

          gpx.write("<groundspeak:country>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getLocation()));
          gpx.write("</groundspeak:country>");

          gpx.write(
              "<groundspeak:state></groundspeak:state>"); // c:geo cannot manage 2 separate fields,
                                                          // so we export as country

          gpx.write("<groundspeak:short_description html=\"");
          gpx.write(BaseUtils.containsHtml(cache.getShortDescription()) ? "True" : "False");
          gpx.write("\">");
          gpx.write(StringEscapeUtils.escapeXml(cache.getShortDescription()));
          gpx.write("</groundspeak:short_description>");

          gpx.write("<groundspeak:long_description html=\"");
          gpx.write(BaseUtils.containsHtml(cache.getDescription()) ? "True" : "False");
          gpx.write("\">");
          gpx.write(StringEscapeUtils.escapeXml(cache.getDescription()));
          gpx.write("</groundspeak:long_description>");

          gpx.write("<groundspeak:encoded_hints>");
          gpx.write(StringEscapeUtils.escapeXml(cache.getHint()));
          gpx.write("</groundspeak:encoded_hints>");

          writeLogs(cache);

          gpx.write("</groundspeak:cache>");

          gpx.write("</wpt>");

          writeWaypoints(cache);

          publishProgress(i + 1);
        }

        gpx.write("</gpx>");

        gpx.close();
      } catch (Exception e) {
        Log.e("GpxExport.ExportTask export", e);

        if (gpx != null) {
          try {
            gpx.close();
          } catch (IOException ee) {
          }
        }

        // delete partial gpx file on error
        if (exportFile.exists()) {
          exportFile.delete();
        }

        return false;
      }

      return true;
    }
示例#18
0
 private void encodeStates(StringBuilder bldr, BayesNode node) {
   for (String outcome : node.getOutcomes()) {
     XMLUtil.emptyTag(bldr, STATE, ID, StringEscapeUtils.escapeXml(outcome));
     bldr.append('\n');
   }
 }