google / cap-library

Common Alerting Protocol Library
Apache License 2.0
84 stars 26 forks source link

cap-library doesn't work on android #14

Open sschiavoni opened 10 years ago

sschiavoni commented 10 years ago

Original issue 15 created by shakusa@google.com on 2011-04-11T01:20:22.000Z:

wondering if you can help me to get your Cap library running on Android.

It seems to be having a problem resolving the schema factory:

No schema factory available for http://www.iana.org/assignments/media-types/application/relax-ng-compact-syntax

here is the Schema Factory Android page:

http://developer.android.com/reference/javax/xml/validation/SchemaFactory.html

Thanks and regards, Eric

I am seeing the following exception when attempting to parse a cap feed:

I/EventMonitor: ( 717): <?xml version='1.0' encoding='ISO-8859-1'?> <rss version='2.0'> <channel> <title>Hawaii Local Storm Reports 30 day archive</title> <link>http://www.prh.noaa.gov/hnl/pages/ls r.php</link> <description>Local Storm Reports issued in the last 30 days from the National Weather Service Forecast Office in Honolulu, Hawaii.</description> <language>en-us</language> <managingEdi tor>W-HFO.Webmaster@noaa.gov</managingEditor> <webMaster>W-HFO.Webmaster@noaa.gov</webMaster> <lastBuildDate>Sun, 3 Apr 2011 13:42:35 +0000</lastBuildDate> <image> <url>http://weather.gov/images /xml_logo.gif</url> <title>Hawaii Local Storm Reports 30 day archive</title> <link>http://www.prh.noaa.gov/hnl/pages/lsr.php&lt;/link&gt; </image><item><title> NON-TSTM WND D - CORRECTED PRELIMINARY LO CAL STORM REPORT...CORRECTED</title><link>http://www.prh.noaa.gov/data/HFO/LSRHFO.1103172036&lt;/link&gt;&lt;description&gt;1018 AM NON-TSTM WND DMG HONOLULU 21.31N 157.86W...</description><pub Date>Thu, 17 Mar 2011 10:36:00 +1000</pubDate></item><item><title> NON-TSTM WND D PRELIMINARY LOCAL STORM REPORT</title><link>http://www.prh.noaa.gov/data/HFO/LSRHFO.1103171819&lt;/link&gt;&lt;description&gt;0807 AM NON-TSTM WND DMG HONOLULU 21.31N 157.86W...</description><pubDate>Thu, 17 Mar 2011 8:17:00 +1000</pubDate></item><item><title> TSTM WND DMG , HEAVY RAIN PRELIMINARY LOCAL STOR M REPORT</title><link>http://www.prh.noaa.gov/data/HFO/LSRHFO.1103041822&lt;/link&gt;&lt;description&gt;0415 AM TSTM WND DMG 2 E WAIPAHU 21.39N 157.98W...</description><pubDate>Fri, 04 Mar 201 1 8:22:00 +1000</pubDate></item><item><title> HEAVY RAIN PRELIMINARY LOCAL STORM REPORT</title><link>http://www.prh.noaa.gov/data/HFO/LSRHFO.1103041642&lt;/link&gt;&lt;description&gt;0237 AM HEAVY RAIN 4 N KOLOA 21.96N 159.46W...</description><pubDate>Fri, 04 Mar 2011 2:42:00 +1000</pubDate></item> </channel> </rss> W/CapFeedParser( 717): No schema factory available for http://www.iana.org/assignments/media-types/application/relax-ng-compact-syntax W/CapFeedParser( 717): java.lang.IllegalArgumentException: http://www.iana.org/assignments/media-types/application/relax-ng-compact-syntax W/CapFeedParser( 717): at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:192) W/CapFeedParser( 717): at com.google.publicalerts.cap.feed.CapFeedParser.loadRelaxNgSchema(CapFeedParser.java:85) W/CapFeedParser( 717): at com.google.publicalerts.cap.feed.CapFeedParser.<clinit>(CapFeedParser.java:78) W/CapFeedParser( 717): at net.cs76.projects.mobilealert.model.EventMonitor.getAlerts(EventMonitor.java:72) W/CapFeedParser( 717): at net.cs76.projects.mobilealert.WelcomeActivity$MonitorForEventsTask.doInBackground(WelcomeActivity.java:210) W/CapFeedParser( 717): at net.cs76.projects.mobilealert.WelcomeActivity$MonitorForEventsTask.doInBackground(WelcomeActivity.java:1) W/CapFeedParser( 717): at android.os.AsyncTask$2.call(AsyncTask.java:252) W/CapFeedParser( 717): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) W/CapFeedParser( 717): at java.util.concurrent.FutureTask.run(FutureTask.java:137) W/CapFeedParser( 717): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081) W/CapFeedParser( 717): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574) W/CapFeedParser( 717): at java.lang.Thread.run(Thread.java:1020) W/dalvikvm( 717): Exception Ljava/lang/IllegalArgumentException; thrown while initializing Lcom/google/publicalerts/cap/feed/CapFeedParser; W/dalvikvm( 717): threadid=9: thread exiting with uncaught exception (group=0x40014760) E/AndroidRuntime( 717): FATAL EXCEPTION: AsyncTask # 1 E/AndroidRuntime( 717): java.lang.RuntimeException: An error occured while executing doInBackground() E/AndroidRuntime( 717): at android.os.AsyncTask$3.done(AsyncTask.java:266) E/AndroidRuntime( 717): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) E/AndroidRuntime( 717): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) E/AndroidRuntime( 717): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) E/AndroidRuntime( 717): at java.util.concurrent.FutureTask.run(FutureTask.java:137) E/AndroidRuntime( 717): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081) E/AndroidRuntime( 717): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574) E/AndroidRuntime( 717): at java.lang.Thread.run(Thread.java:1020) E/AndroidRuntime( 717): Caused by: java.lang.ExceptionInInitializerError E/AndroidRuntime( 717): at net.cs76.projects.mobilealert.model.EventMonitor.getAlerts(EventMonitor.java:72) E/AndroidRuntime( 717): at net.cs76.projects.mobilealert.WelcomeActivity$MonitorForEventsTask.doInBackground(WelcomeActivity.java:210) E/AndroidRuntime( 717): at net.cs76.projects.mobilealert.WelcomeActivity$MonitorForEventsTask.doInBackground(WelcomeActivity.java:1) E/AndroidRuntime( 717): at android.os.AsyncTask$2.call(AsyncTask.java:252) E/AndroidRuntime( 717): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) E/AndroidRuntime( 717): ... 4 more E/AndroidRuntime( 717): Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema E/AndroidRuntime( 717): at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:192) E/AndroidRuntime( 717): at com.google.publicalerts.cap.feed.CapFeedParser.loadXsd(CapFeedParser.java:103) E/AndroidRuntime( 717): at com.google.publicalerts.cap.feed.CapFeedParser.<clinit>(CapFeedParser.java:80) E/AndroidRuntime( 717): ... 9 more W/ActivityManager( 72): Force finishing activity net.cs76.projects.mobilealert/.WelcomeActivity W/WindowManager( 72): Failure taking screenshot for (216x135) to layer 21010 I/TtsService( 454): Stopping

sschiavoni commented 10 years ago

Comment #1 originally posted by shakusa@google.com on 2011-04-11T01:34:39.000Z:

There are 2 problems:

  1. The code can't find the relaxng schema used to validate atom. This isn't breaking anything in the code, but it's causing a lot of the logging
  2. The code can't find the xsd schema used to valdiate both rss and cap. This is causing the IllegalArgumentException that's killing the program.

For 1:

It seems that there are problems with Android such that when it repackages JAR files, it leaves out descriptors in META-INF/services. This is mentioned on other sites like http://wiki.restlet.org/docs_2.0/13-restlet/275-restlet/266-restlet.html

That means http://code.google.com/p/cap-library/source/browse/java/config/META-INF/services/javax.xml.validation.SchemaFactory doesn't have any effect.

There is a workaround to this one; you can use System.setProperty at http://code.google.com/p/cap-library/source/browse/java/src/com/google/publicalerts/cap/feed/CapFeedParser.java#&nbsp;83, which android allows, eg

    System.setProperty(
        SchemaFactory.class.getName() + ":" + Constants.RELAXNG_COMPACT_URI,
        "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory");

For 2:

After a lot of digging, I found the related Android bug:

http://code.google.com/p/android/issues/detail?id=7395 (3268630 internally)

Quoting that bug: Our XML APIs (including SAX, XmlPull and DOM) don't support any of the following: XML Schema validation

I think there is a workaround here, too. Part of the workaround is to bundle the xerces jar into the apk. See for example: http://stackoverflow.com/questions/2651804/xerces-on-android

Then there have to be similar code changes as for 1. that set appropriate system properties to workaround the META-INF issue. Here: http://code.google.com/p/cap-library/source/browse/java/src/com/google/publicalerts/cap/feed/CapFeedParser.java#&nbsp;102 and here: http://code.google.com/p/cap-library/source/browse/java/src/com/google/publicalerts/cap/CapXmlParser.java#&nbsp;74

AnthoSi commented 9 years ago

I would like to use the library on android. Is there a tested way to make it working ? Thanks, Anthony