/** * 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()); } }
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()); } }
/** * Parses a ConferenceMedium extension sub-packet and creates a {@link * ConferenceMediumPacketExtension} instance. At the beginning of the method call, the xml parser * will be positioned on the opening element of the packet extension. As required by the smack * API, at the end of the method call, the parser will be positioned on the closing element of the * packet extension. * * @param parser an XML parser positioned at the opening <tt>ConferenceMedium</tt> element. * @return a new {@link ConferenceMediumPacketExtension} instance. * @throws java.lang.Exception if an error occurs parsing the XML. */ public PacketExtension parseExtension(XmlPullParser parser) throws Exception { boolean done = false; int eventType; String elementName = null; String label = parser.getAttributeValue("", ConferenceMediumPacketExtension.LABEL_ATTR_NAME); if (label == null) { throw new Exception("Coin medium element must contain entity attribute"); } ConferenceMediumPacketExtension ext = new ConferenceMediumPacketExtension("entry", label); while (!done) { eventType = parser.next(); elementName = parser.getName(); if (eventType == XmlPullParser.START_TAG) { if (elementName.equals(MediaPacketExtension.ELEMENT_DISPLAY_TEXT)) { ext.setDisplayText(CoinIQProvider.parseText(parser)); } else if (elementName.equals(MediaPacketExtension.ELEMENT_STATUS)) { ext.setStatus(CoinIQProvider.parseText(parser)); } else if (elementName.equals(MediaPacketExtension.ELEMENT_TYPE)) { ext.setType(CoinIQProvider.parseText(parser)); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals(ConferenceMediumPacketExtension.ELEMENT_NAME)) { done = true; } } } return ext; }
/** * Parses a User extension sub-packet and creates a {@link UserPacketExtension} instance. At the * beginning of the method call, the xml parser will be positioned on the opening element of the * packet extension. As required by the smack API, at the end of the method call, the parser will * be positioned on the closing element of the packet extension. * * @param parser an XML parser positioned at the opening <tt>User</tt> element. * @return a new {@link UserPacketExtension} instance. * @throws java.lang.Exception if an error occurs parsing the XML. */ public UserPacketExtension parseExtension(XmlPullParser parser) throws Exception { boolean done = false; int eventType; String elementName = null; String entity = parser.getAttributeValue("", UserPacketExtension.ENTITY_ATTR_NAME); StateType state = StateType.full; String stateStr = parser.getAttributeValue("", UserPacketExtension.STATE_ATTR_NAME); if (stateStr != null) { state = StateType.parseString(stateStr); } if (entity == null) { throw new Exception("Coin user element must contain entity attribute"); } UserPacketExtension ext = new UserPacketExtension(entity); ext.setAttribute(UserPacketExtension.STATE_ATTR_NAME, state); while (!done) { eventType = parser.next(); elementName = parser.getName(); if (eventType == XmlPullParser.START_TAG) { if (elementName.equals(UserPacketExtension.ELEMENT_DISPLAY_TEXT)) { ext.setDisplayText(CoinIQProvider.parseText(parser)); } else if (elementName.equals(EndpointPacketExtension.ELEMENT_NAME)) { PacketExtensionProvider provider = new EndpointProvider(); PacketExtension childExtension = provider.parseExtension(parser); ext.addChildExtension(childExtension); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals(UserPacketExtension.ELEMENT_NAME)) { done = true; } } } return ext; }
/** * 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 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"); }
/** * Parses the given <tt>parser</tt> in order to create a <tt>FileElement</tt> from it. * * @param parser the parser to parse * @see IQProvider#parseIQ(XmlPullParser) */ public IQ parseIQ(final XmlPullParser parser) throws Exception { boolean done = false; // si String id = parser.getAttributeValue("", "id"); String mimeType = parser.getAttributeValue("", "mime-type"); StreamInitiation initiation = new StreamInitiation(); // file String name = null; String size = null; String hash = null; String date = null; String desc = null; ThumbnailElement thumbnail = null; boolean isRanged = false; // feature DataForm form = null; DataFormProvider dataFormProvider = new DataFormProvider(); int eventType; String elementName; String namespace; while (!done) { eventType = parser.next(); elementName = parser.getName(); namespace = parser.getNamespace(); if (eventType == XmlPullParser.START_TAG) { if (elementName.equals("file")) { name = parser.getAttributeValue("", "name"); size = parser.getAttributeValue("", "size"); hash = parser.getAttributeValue("", "hash"); date = parser.getAttributeValue("", "date"); } else if (elementName.equals("desc")) { desc = parser.nextText(); } else if (elementName.equals("range")) { isRanged = true; } else if (elementName.equals("x") && namespace.equals("jabber:x:data")) { form = (DataForm) dataFormProvider.parseExtension(parser); } else if (elementName.equals("thumbnail")) { thumbnail = new ThumbnailElement(parser.getText()); } } else if (eventType == XmlPullParser.END_TAG) { if (elementName.equals("si")) { done = true; } // The name-attribute is required per XEP-0096, so ignore the // IQ if the name is not set to avoid exceptions. Particularly, // the SI response of Empathy contains an invalid, empty // file-tag. else if (elementName.equals("file") && name != null) { long fileSize = 0; if (size != null && size.trim().length() != 0) { try { fileSize = Long.parseLong(size); } catch (NumberFormatException e) { logger.warn( "Received an invalid file size," + " continuing with fileSize set to 0", e); } } FileElement file = new FileElement(name, fileSize); file.setHash(hash); if (date != null) { // try all known date formats boolean found = false; if (date.matches(".*?T\\d+:\\d+:\\d+(\\.\\d+)?(\\+|-)\\d+:\\d+")) { int timeZoneColon = date.lastIndexOf(":"); date = date.substring(0, timeZoneColon) + date.substring(timeZoneColon + 1, date.length()); } for (DateFormat fmt : DATE_FORMATS) { try { file.setDate(fmt.parse(date)); found = true; break; } catch (ParseException ex) { } } if (!found) { logger.warn("Unknown dateformat on incoming file transfer: " + date); } } if (thumbnail != null) file.setThumbnailElement(thumbnail); file.setDesc(desc); file.setRanged(isRanged); initiation.setFile(file); } } } initiation.setSesssionID(id); initiation.setMimeType(mimeType); initiation.setFeatureNegotiationForm(form); return initiation; }