/** * Parse a repository document. * * @param url * @throws IOException * @throws XmlPullParserException * @throws Exception */ void parseDocument(URL url) throws IOException, XmlPullParserException, Exception { if (!visited.contains(url)) { visited.add(url); try { System.out.println("Visiting: " + url); InputStream in = null; if (url.getPath().endsWith(".zip")) { ZipInputStream zin = new ZipInputStream(url.openStream()); ZipEntry entry = zin.getNextEntry(); while (entry != null) { if (entry.getName().equals("repository.xml")) { in = zin; break; } entry = zin.getNextEntry(); } } else { in = url.openStream(); } Reader reader = new InputStreamReader(in); XmlPullParser parser = new KXmlParser(); parser.setInput(reader); parseRepository(parser); } catch (MalformedURLException e) { System.out.println("Cannot create connection to url"); } } }
private Tweets parse(XmlPullParser aParser) throws Exception { tweets = new Tweets(); int _e = aParser.next(); while (_e != XmlPullParser.END_DOCUMENT) { if (_e == XmlPullParser.START_TAG) { startTag(aParser.getPrefix(), aParser.getName(), aParser); } _e = aParser.next(); } return tweets; }
/** * We have a referral to another repository. Just create another parser and read it inline. * * @param parser */ void referral(XmlPullParser parser) { // TODO handle depth! try { parser.require(XmlPullParser.START_TAG, null, "referral"); // String depth = parser.getAttributeValue(null, "depth"); String path = parser.getAttributeValue(null, "url"); URL url = new URL(this.url, path); parseDocument(url); parser.next(); parser.require(XmlPullParser.END_TAG, null, "referral"); } catch (Exception e) { e.printStackTrace(); } }
/** * Parse start of element from document. Ignores character data to next start or end tag, but * throws exception if an end tag is seen before a start tag, or if the start tag seen does not * match the expected name. * * @param tag element name expected * @throws IOException if error reading document * @throws XmlPullParserException if expected element not found, or if other parse error */ protected void parseStartTag(String tag) throws IOException, XmlPullParserException { while (true) { switch (m_parser.next()) { case XmlPullParser.START_TAG: m_parser.readStartTag(m_startTag); if (m_startTag.getLocalName().equals(tag)) { return; } // fall through for error handling case XmlPullParser.END_TAG: case XmlPullParser.END_DOCUMENT: throw new XmlPullParserException("Missing expected start tag " + tag); } } }
/** * Parse end of element from document. Collects character data to the end tag and returns it with * whitespace stripped. Throws an exception if a start tag is seen before an end tag, or if the * end tag seen does not match the expected name. * * @param tag element name expected * @return content text with whitespace stripped * @throws IOException if error reading document * @throws XmlPullParserException if expected element not found, or if other parse error */ protected String parseEndTag(String tag) throws IOException, XmlPullParserException { m_buffer.setLength(0); while (true) { switch (m_parser.next()) { case XmlPullParser.CONTENT: m_buffer.append(m_parser.readContent()); break; case XmlPullParser.END_TAG: m_parser.readEndTag(m_endTag); if (m_endTag.getLocalName().equals(tag)) { return m_buffer.toString().trim(); } // fall through for error handling case XmlPullParser.START_TAG: case XmlPullParser.END_DOCUMENT: throw new XmlPullParserException("Missing expected end tag " + tag); } } }
/** * Parse and process trade history information stream. * * @param in XML document reader * @throws IOException if error reading document * @throws XmlPullParserException on document parse error */ public void parse(Reader in) throws IOException, XmlPullParserException { // set document source for parse m_parser.reset(); m_parser.setInput(in); // main pull parsing loop byte type; while ((type = m_parser.next()) != XmlPullParser.END_DOCUMENT) { // ignore everything other than a start tag if (type == XmlPullParser.START_TAG) { // process the start tags we're interested in m_parser.readStartTag(m_startTag); String lname = m_startTag.getLocalName(); if (lname.equals(STOCK_ELEMENT_NAME)) { parseStockTrade(); } else if (lname.equals(OPTION_ELEMENT_NAME)) { parseOptionTrade(); } } } }
private void startTag(String aPrefix, String aName, XmlPullParser aParser) throws Exception { if ("entry".equals(aName)) { tweets.addTweet(currentTweet = new Tweet()); } else if ("published".equals(aName)) { aParser.next(); currentTweet.setPublished(dateFormat.parse(aParser.getText())); } else if (("title".equals(aName)) && (currentTweet != null)) { aParser.next(); currentTweet.setTitle(aParser.getText()); } else if ("content".equals(aName)) { Content _c = new Content(); _c.setType(aParser.getAttributeValue(null, "type")); aParser.next(); _c.setValue(aParser.getText()); currentTweet.setContent(_c); } else if ("lang".equals(aName)) { aParser.next(); currentTweet.setLanguage(aParser.getText()); } else if ("author".equals(aName)) { currentTweet.setAuthor(currentAuthor = new Author()); } else if ("name".equals(aName)) { aParser.next(); currentAuthor.setName(aParser.getText()); } else if ("uri".equals(aName)) { aParser.next(); currentAuthor.setUri(aParser.getText()); } }
@Override public Tweets read(InputStream anInputStream) throws Exception { XmlPullParser _p = f.newPullParser(); _p.setInput(anInputStream, "utf-8"); return parse(_p); }
public Object readInstance( XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException { return IsoDate.stringToDate(parser.nextText(), IsoDate.DATE_TIME); }
/** * Parse the repository. * * @param parser * @throws Exception */ private void parseRepository(XmlPullParser parser) throws Exception { try { parser.require(XmlPullParser.START_DOCUMENT, null, null); parser.nextTag(); if (parser.getName().equals("bundles")) parseOscar(parser); else { parser.require(XmlPullParser.START_TAG, null, "repository"); date = parser.getAttributeValue(null, "lastmodified"); name = parser.getAttributeValue(null, "name"); if (name == null) name = "Untitled"; while (parser.nextTag() == XmlPullParser.START_TAG) { if (parser.getName().equals("resource")) { ResourceImpl resource = new ResourceImpl(this, parser); resources.add(resource); } else if (parser.getName().equals("referral")) referral(parser); else throw new IllegalArgumentException( "Invalid tag in repository: " + url + " " + parser.getName()); } parser.require(XmlPullParser.END_TAG, null, "repository"); } } catch (XmlPullParserException e) { e.printStackTrace(); throw new IllegalArgumentException( "XML unregognized around: " + e.getLineNumber() + " " + e.getMessage()); } }
/** * Parse an old style OBR repository. * * <p><dtd-version>1.0</dtd-version> <repository> <name>Oscar Bundle Repository</name> * <url>http://oscar-osgi.sourceforge.net/</url> <date>Fri May 07 16:45:07 CEST 2004</date> * <extern-repositories> * <!-- * Stefano Lenzi ([email protected]) --> * <url>http://domoware.isti.cnr.it/osgi-obr/niche-osgi-obr.xml</url> * <!--Manuel Palencia ([email protected]) --> * <!-- * <url>http://jmood.forge.os4os.org/repository.xml</url> --> * <!-- Enrique * Rodriguez ([email protected]) --> * <url>http://update.cainenable.org/repository.xml</url> </extern-repositories> </repository> * <bundle> <bundle-name>Bundle Repository</bundle-name> <bundle-description> A bundle repository * service for Oscar. </bundle-description> <bundle-updatelocation> * http://oscar-osgi.sf.net/repo/bundlerepository/bundlerepository.jar </bundle-updatelocation> * <bundle-sourceurl> http://oscar-osgi.sf.net/repo/bundlerepository/bundlerepository-src.jar * </bundle-sourceurl> <bundle-version>1.1.3</bundle-version> <bundle-docurl> * http://oscar-osgi.sf.net/repo/bundlerepository/ </bundle-docurl> * <bundle-category>General</bundle-category> <import-package package="org.osgi.framework"/> * <export-package package="org.ungoverned.osgi.service.bundlerepository" * specification-version="1.1.0"/> </bundle> * */ private void parseOscar(XmlPullParser parser) throws Exception { parser.require(XmlPullParser.START_TAG, null, "bundles"); while (true) { int event = parser.next(); // Error .. if (event == XmlPullParser.TEXT) event = parser.next(); if (event != XmlPullParser.START_TAG) break; ResourceImpl resource = new ResourceImpl(this); if (parser.getName().equals("bundle")) { while (parser.nextTag() == XmlPullParser.START_TAG) { String key = parser.getName(); if (key.equals("import-package")) { RequirementImpl requirement = new RequirementImpl("package"); requirement.setOptional(false); requirement.setMultiple(false); String p = parser.getAttributeValue(null, "package"); StringBuffer sb = new StringBuffer(); sb.append("(&(package="); sb.append(p); sb.append(")"); String version = parser.getAttributeValue(null, "specification-version"); VersionRange v = new VersionRange("0"); if (version != null) { sb.append("(version="); sb.append(v = new VersionRange(version)); sb.append(")"); } sb.append(")"); requirement.setFilter(sb.toString()); requirement.setComment("Import-Package: " + p + ";" + v); resource.addRequirement(requirement); parser.nextTag(); } else if (key.equals("export-package")) { CapabilityImpl capability = new CapabilityImpl("package"); capability.addProperty("package", parser.getAttributeValue(null, "package")); String version = parser.getAttributeValue(null, "specification-version"); if (version != null) { capability.addProperty("version", new VersionRange(version)); } resource.addCapability(capability); parser.nextTag(); } else { String value = parser.nextText().trim(); if (key.equals("bundle-sourceurl")) resource.setSource(new URL(value)); else if (key.equals("bundle-docurl")) resource.setDocumentation(new URL(value)); else if (key.equals("bundle-updatelocation")) resource.setURL(new URL(value)); else if (key.equals("bundle-description")) resource.setDescription(value); else if (key.equals("bundle-category")) resource.addCategory(value); else if (key.equals("bundle-name")) { resource.setName(value); resource.setPresentationName(value); } else if (key.equals("bundle-version")) resource.setVersion(new VersionRange(value)); else { resource.put(key, value); } } } resources.add(resource); parser.require(XmlPullParser.END_TAG, null, "bundle"); } else if (parser.getName().equals("repository")) { parser.require(XmlPullParser.START_TAG, null, "repository"); while (parser.nextTag() == XmlPullParser.START_TAG) { String tag = parser.getName(); if (tag.equals("name")) { String name = parser.nextText(); if (this.name == null) this.name = name.trim(); } else if (tag.equals("url")) parser.nextText().trim(); else if (tag.equals("date")) parser.nextText().trim(); else if (tag.equals("extern-repositories")) { parser.require(XmlPullParser.START_TAG, null, "extern-repositories"); while (parser.nextTag() == XmlPullParser.START_TAG) { if (parser.getName().equals("url")) parseDocument(new URL(parser.nextText().trim())); else throw new IllegalArgumentException( "Invalid tag in repository while parsing extern repositories: " + url + " " + parser.getName()); } parser.require(XmlPullParser.END_TAG, null, "extern-repositories"); } else throw new IllegalArgumentException( "Invalid tag in repository: " + url + " " + parser.getName()); } parser.require(XmlPullParser.END_TAG, null, "repository"); } else if (parser.getName().equals("dtd-version")) { parser.nextText(); } else throw new IllegalArgumentException( "Invalid tag in repository: " + url + " " + parser.getName()); } parser.require(XmlPullParser.END_TAG, null, "bundles"); }
/** * Retrieve DiscoverInfo for a specific node. * * @param caps the <tt>Caps</tt> i.e. the node, the hash and the ver * @return The corresponding DiscoverInfo or null if none is known. */ public static DiscoverInfo getDiscoverInfoByCaps(Caps caps) { synchronized (caps2discoverInfo) { DiscoverInfo discoverInfo = caps2discoverInfo.get(caps); /* * If we don't have the discoverInfo in the runtime cache yet, we * may have it remembered in a previous application instance. */ if (discoverInfo == null) { ConfigurationService configurationService = getConfigService(); String capsPropertyName = getCapsPropertyName(caps); String xml = configurationService.getString(capsPropertyName); if ((xml != null) && (xml.length() != 0)) { IQProvider discoverInfoProvider = (IQProvider) ProviderManager.getInstance() .getIQProvider("query", "http://jabber.org/protocol/disco#info"); if (discoverInfoProvider != null) { XmlPullParser parser = new MXParser(); try { parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new StringReader(xml)); // Start the parser. parser.next(); } catch (XmlPullParserException xppex) { parser = null; } catch (IOException ioex) { parser = null; } if (parser != null) { try { discoverInfo = (DiscoverInfo) discoverInfoProvider.parseIQ(parser); } catch (Exception ex) { } if (discoverInfo != null) { if (caps.isValid(discoverInfo)) caps2discoverInfo.put(caps, discoverInfo); else { logger.error( "Invalid DiscoverInfo for " + caps.getNodeVer() + ": " + discoverInfo); /* * The discoverInfo doesn't seem valid * according to the caps which means that we * must have stored invalid information. * Delete the invalid information in order * to not try to validate it again. */ configurationService.removeProperty(capsPropertyName); } } } } } } return discoverInfo; } }