Пример #1
0
 /**
  * Allocate and initial an XmlPullParser
  *
  * <p>At the time this code was written the XmlPullParser did not support validation, so the call
  * to setValidating() is passed "false".
  */
 private XmlPullParser getParser() throws XmlPullParserException {
   XmlPullParserFactory factory;
   factory = XmlPullParserFactory.newInstance();
   factory.setNamespaceAware(true);
   factory.setValidating(false);
   XmlPullParser parser = factory.newPullParser();
   return parser;
 } // getParser
 static {
   try {
     PARSER_FACTORY = XmlPullParserFactory.newInstance();
     PARSER_FACTORY.setNamespaceAware(true);
     PARSER_FACTORY.setValidating(false);
   } catch (XmlPullParserException e) {
     log.severe(e.toString());
   }
 }
 public static XmlSerializer createXmlSerializer(Writer out) {
   XmlSerializer result = null;
   try {
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     factory.setValidating(true);
     result = factory.newSerializer();
     result.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
     result.setOutput(out);
   } catch (Exception e) {
     log.error("When creating XmlSerializer: " + e.getClass().getName() + ": " + e.getMessage());
   }
   return result;
 }
  public XmlSimpleFeatureParser(
      final InputStream getFeatureResponseStream,
      final SimpleFeatureType targetType,
      QName featureDescriptorName,
      String axisOrder)
      throws IOException {

    // this.inputStream = new TeeInputStream(inputStream, System.err);

    this.inputStream = getFeatureResponseStream;
    this.featureNamespace = featureDescriptorName.getNamespaceURI();
    this.featureName = featureDescriptorName.getLocalPart();
    this.targetType = targetType;
    this.builder = new SimpleFeatureBuilder(targetType);
    this.axisOrder = axisOrder;

    XmlPullParserFactory factory;
    try {
      factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(false);

      // parse root element
      parser = factory.newPullParser();
      parser.setInput(inputStream, "UTF-8");
      parser.nextTag();
      parser.require(START_TAG, null, WFS.FeatureCollection.getLocalPart());

      String nof = parser.getAttributeValue(null, "numberOfFeatures");
      if (nof != null) {
        try {
          this.numberOfFeatures = Integer.valueOf(nof);
        } catch (NumberFormatException nfe) {
          LOGGER.warning("Can't parse numberOfFeatures out of " + nof);
        }
      }
    } catch (XmlPullParserException e) {
      throw new DataSourceException(e);
    }

    // HACK! use a case insensitive set to compare the comming attribute names with the ones in
    // the schema. Rationale being that the FGDC CubeWerx server has a missmatch in the case of
    // property names between what it states in a DescribeFeatureType and in a GetFeature
    // requests
    expectedProperties = new TreeMap<String, AttributeDescriptor>(String.CASE_INSENSITIVE_ORDER);
    for (AttributeDescriptor desc : targetType.getAttributeDescriptors()) {
      expectedProperties.put(desc.getLocalName(), desc);
    }
  }
Пример #5
0
  /** Helper method for finding which style handler/version to use from the actual content. */
  Object[] getVersionAndReader(Object input) throws IOException {
    // need to determine version of sld from actual content
    BufferedReader reader = null;

    if (input instanceof InputStream) {
      reader = RequestUtils.getBufferedXMLReader((InputStream) input, XML_LOOKAHEAD);
    } else {
      reader = RequestUtils.getBufferedXMLReader(toReader(input), XML_LOOKAHEAD);
    }

    if (!reader.ready()) {
      return null;
    }

    String version;
    try {
      // create stream parser
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(false);

      // parse root element
      XmlPullParser parser = factory.newPullParser();
      parser.setInput(reader);
      parser.nextTag();

      version = null;
      for (int i = 0; i < parser.getAttributeCount(); i++) {
        if ("version".equals(parser.getAttributeName(i))) {
          version = parser.getAttributeValue(i);
        }
      }

      parser.setInput(null);
    } catch (XmlPullParserException e) {
      throw (IOException) new IOException("Error parsing content").initCause(e);
    }

    // reset input stream
    reader.reset();

    if (version == null) {
      LOGGER.warning("Could not determine SLD version from content. Assuming 1.0.0");
      version = "1.0.0";
    }

    return new Object[] {new Version(version), reader};
  }
Пример #6
0
  private static void doParse(final Story s)
      throws XmlPullParserException, MalformedURLException, IOException {
    // Set up URL
    URL storyURL = new URL(mobileStoryUrlBase + s.url.substring(s.url.lastIndexOf('/')));

    // Set up parser
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    XmlPullParser xpp = factory.newPullParser();

    Reader input = new InputStreamReader(storyURL.openConnection().getInputStream());
    xpp.setInput(input);
    int eventType = xpp.getEventType();
    ParseState state = ParseState.OTHER;

    // parse
    StringBuilder textSB = new StringBuilder();
    // so when a tag ends we know exactly which tag is ending
    int depth = 0;
    while (eventType != XmlPullParser.END_DOCUMENT) {
      if (eventType == XmlPullParser.START_TAG) {

        // Body starts
        if (getAttr(xpp, "class").equals(BODY_CLASS)) {
          state = ParseState.BODY;
          depth = 0; // the start of the body is the reference depth
        }

        // Photo div starts
        else if (getAttr(xpp, "class").equals(PHOTO_CLASS)) state = ParseState.PHOTO;

        // Photo img found
        else if (state == ParseState.PHOTO && xpp.getName().equals("img")) {
          s.imgUrl = xpp.getAttributeValue(null, "src");
          state = ParseState.OTHER;
        }

        // byline <p> found
        else if (getAttr(xpp, "class").equals(BYLINE_CLASS)) {
          state = ParseState.BYLINE;
        }

        depth++;
      } else if (eventType == XmlPullParser.TEXT) {
        // Found body text
        if (state == ParseState.BODY) {
          if (xpp.getText() != null) {
            // Log.d("isd", "body text: " + xpp.getText());
            textSB.append(xpp.getText().replace("\n", " ").replace("\t", ""));
          }
        } else if (state == ParseState.BYLINE) {
          // Log.d("isd", "byline: " + xpp.getText());
          s.byline = xpp.getText().trim().replace(" |  ", "\n");
        }

      } else if (eventType == XmlPullParser.END_TAG) {
        depth--;
        // Log.d("isd", "ended " + xpp.getName() + " with depth " + depth);

        // Body ends
        if (state == ParseState.BODY && depth == 0) {
          // Sometimes paragraphs will start with a space b/c
          // we replace \n with " ". This fixes that.
          s.text = textSB.toString().trim().replace("\n ", "\n");
          state = ParseState.OTHER;
        } else if (state == ParseState.BODY) {
          if (xpp.getName().equals("p")) textSB.append("\n\n");
        } else if (state == ParseState.BYLINE) state = ParseState.OTHER;
      }
      try {
        eventType = xpp.next();
      } catch (XmlPullParserException e) {
        // LE PROBLEM: barfs on &copy; no idea why. Reads &amp; fine.
        // Tried setting encoding to UTF-8 manually in the InputStreamReader
        // but it picks up UTF-8 anyway on its own. Tried using the
        // xpp.setInput(InputStream, encoding) method but no difference.
        // For now just ignore the exception
      }
    }
  }
  /**
   * Get specific color of related theme from theme apk asset resource path.
   *
   * @param colorName the key string of the color.
   * @return the color value for the current theme, if the current theme is the default theme, or
   *     the colorName is not present in the color.xml, return 0.
   */
  public int getThemeColor(String colorName) {
    if (EncapsulationConstant.USE_MTK_PLATFORM) {
      return mResources.getThemeColor(colorName);
    } else {
      InputStream raw = null;
      AssetManager am = mResources.getAssets();
      String themepath = SystemProperties.get("persist.sys.skin", DEFAULT_THEME_PATH);
      // If the current theme is the default theme, return 0 directly.
      if (DEFAULT_THEME_PATH.equals(themepath)) {
        return 0;
      }
      // get themeColor from cache
      Integer themeColor = mMtkColorCache.get(colorName);
      if (themeColor != null) {
        return themeColor;
      }
      // Add theme path to asset path to access it, if add asset path failed,
      // return 0 directly.
      int cookie = am.addAssetPath(themepath);
      if (cookie == 0) {
        return 0;
      }

      // Get color value from xml file.
      try {
        // Open color.xml as assets.
        raw = am.openNonAsset(cookie, THEME_COLOR_PATH, AssetManager.ACCESS_STREAMING);

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setValidating(false);
        XmlPullParser myxml = factory.newPullParser();
        myxml.setInput(raw, null);
        int eventType = myxml.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
          switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
              break;
            case XmlPullParser.START_TAG:
              if (STR_COLOR.equals(myxml.getName())) {
                if (colorName.equals(myxml.getAttributeValue(0))) {
                  String colorStr = myxml.nextText();
                  themeColor = Color.parseColor(colorStr);
                  mMtkColorCache.put(colorName, themeColor);
                  return themeColor;
                }
              }
              break;
            case XmlPullParser.END_TAG:
              break;
            default:
              break;
          }
          eventType = myxml.next();
        }
      } catch (IOException e) {
        Log.e(TAG, "IOException happened when getThemeColor, msg = " + e.getMessage());
      } catch (XmlPullParserException e) {
        Log.e(TAG, "XmlPullParserException happened when getThemeColor, msg = " + e.getMessage());
      }

      return 0;
    }
  }
Пример #8
0
  public static Collection<Statistic> parse(File inFile)
      throws IOException, XmlPullParserException {
    Collection<Statistic> results = new ArrayList<Statistic>();
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
      fis = new FileInputStream(inFile);
      bis = new BufferedInputStream(fis);
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(false);
      XmlPullParser parser = factory.newPullParser();
      parser.setInput(bis, null);

      // check that the first tag is <javancss>
      expectNextTag(parser, "javancss");

      // skip until we get to the <packages> tag
      while (parser.getDepth() > 0
          && (parser.getEventType() != XmlPullParser.START_TAG
              || !"packages".equals(parser.getName()))) {
        parser.next();
      }
      while (parser.getDepth() > 0
          && (parser.getEventType() != XmlPullParser.START_TAG
              || !"package".equals(parser.getName()))) {
        parser.next();
      }
      while (parser.getDepth() >= 2
          && parser.getEventType() == XmlPullParser.START_TAG
          && "package".equals(parser.getName())) {
        Map<String, String> data = new HashMap<String, String>();
        String lastTag = null;
        String lastText = null;
        int depth = parser.getDepth();
        while (parser.getDepth() >= depth) {
          parser.next();
          switch (parser.getEventType()) {
            case XmlPullParser.START_TAG:
              lastTag = parser.getName();
              break;
            case XmlPullParser.TEXT:
              lastText = parser.getText();
              break;
            case XmlPullParser.END_TAG:
              if (parser.getDepth() == 4 && lastTag != null && lastText != null) {
                data.put(lastTag, lastText);
              }
              lastTag = null;
              lastText = null;
              break;
          }
        }
        if (data.containsKey("name")) {
          Statistic s = new Statistic(data.get("name"));
          s.setClasses(Long.valueOf(data.get("classes")));
          s.setFunctions(Long.valueOf(data.get("functions")));
          s.setNcss(Long.valueOf(data.get("ncss")));
          s.setJavadocs(Long.valueOf(data.get("javadocs")));
          s.setJavadocLines(Long.valueOf(data.get("javadoc_lines")));
          s.setSingleCommentLines(Long.valueOf(data.get("single_comment_lines")));
          s.setMultiCommentLines(Long.valueOf(data.get("multi_comment_lines")));
          results.add(s);
        }
        parser.next();
      }

    } catch (XmlPullParserException e) {
      throw new IOException2(e);
    } finally {
      if (bis != null) {
        bis.close();
      }
      if (fis != null) {
        fis.close();
      }
    }
    return results;
  }