[vlma-devel] commit: Fix the parsing of the error correction. (Adrien Grand )

git version control git at videolan.org
Mon Jan 12 03:07:14 CET 2009


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Mon Jan 12 02:56:57 2009 +0100| [bafc25534f343954cab335f0be31242742e4eccc] | committer: Adrien Grand 

Fix the parsing of the error correction.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=bafc25534f343954cab335f0be31242742e4eccc
---

 .../src/main/java/org/videolan/vlma/DataImpl.java  |  101 +++++++++++++-------
 .../media/MediaProgramAddController.java           |    5 +-
 2 files changed, 71 insertions(+), 35 deletions(-)

diff --git a/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java b/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java
index 6d2f7fd..c5cd779 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java
@@ -244,12 +244,11 @@ public class DataImpl implements Data {
      *            number)
      */
     private static String htmlCellExtract(NodeList nodes, int column, int row, int maxRow) {
-        String result = "";
+        String result = null;
         if (maxRow == 0) {
             Node text = nodes.elementAt(column);
             result = text.toPlainTextString();
-        }
-        else {
+        } else {
             NodeFilter brTagFilter = new TagNameFilter("br");
             NodeList breakLines = nodes.elementAt(column).getChildren().extractAllNodesThatMatch(brTagFilter);
             if (breakLines.size() == maxRow) {
@@ -275,63 +274,100 @@ public class DataImpl implements Data {
                 }
             }
         }
-        result = result.replaceAll("&nbsp;", " ");
-        return result.trim();
+        if (result != null) {
+            result = result.replaceAll("&nbsp;", " ");
+            result = result.trim();
+        }
+        return result;
     }
 
     synchronized public void updateSatChannels(URL source) throws IOException {
+        Set<Media> newMedias = new HashSet<Media>();
+        String url = source.toString();
+        logger.info("Downloading " + url);
+
         List<String> tvFilter = new ArrayList<String>();
-        Set<String> coverages = new HashSet<String>();
         tvFilter.add("TV-DIG-CRYPT");
         tvFilter.add("TV-DIG");
         tvFilter.add("R-DIG");
         tvFilter.add("R-DIG-CRYPT");
-        tvFilter.add("TV-HD");
-        Set<Media> newMedias = new HashSet<Media>();
-        newMedias.clear();
-        logger.info("Downloading " + source.toString());
-
         try {
             Parser parser = new Parser(source.openConnection());
             NodeList rows = parser.parse(new TagNameFilter("tr"));
-            logger.info(source.toString() + " downloaded and parsed.");
+            logger.info(url + " downloaded and parsed.");
             int frequency = 0;
             char polarisation = 'A';
-            String coverage = "";
+            String coverage = null;
             NodeFilter thTagFilter = new OrFilter(new TagNameFilter("th"), new HasAttributeFilter("rowspan"));
             NodeFilter tdTagFilter = new TagNameFilter("td");
             for (int i = 0; i < rows.size(); ++i) {
                 Node node = rows.elementAt(i);
                 NodeList thCells = node.getChildren().extractAllNodesThatMatch(thTagFilter);
                 NodeList tdCells = node.getChildren().extractAllNodesThatMatch(tdTagFilter);
+                // FIXME: not very robust...
                 if (tdCells.size() == 8) {
+                    String name = htmlCellExtract(tdCells, 1, 0, 3);
+                    String category = htmlCellExtract(tdCells, 0, 0, 2);
+                    if (!tvFilter.contains(category)) {
+                        logger.debug("Skipping channel " + name + " of category " + category);
+                        continue;
+                    }
                     if (thCells.size() == 2) {
                         frequency = (int) (Double.parseDouble(htmlCellExtract(thCells, 0, 1, 2).split(" ")[0]) * 1000);
                         polarisation = htmlCellExtract(thCells, 0, 2, 2).charAt(0);
                         coverage = htmlCellExtract(thCells, 1, 0, 0);
+                        switch (polarisation) {
+                        case 'V':
+                        case 'v':
+                        case 'h':
+                        case 'H':
+                            break; // OK
+                        default:
+                            logger.warn("Unable to parse polarization of channel " + name + ", " + polarisation + " found");
+                            polarisation = 'A'; // auto
+                            break;
+                        }
+                    }
+                    if (coverage == null || coverage.length() == 0) {
+                        logger.warn("Cannot get the coverage for channel " + name);
+                        continue;
+                    }
+                    String sidAsString = null;
+                    int sid = 0;
+                    try {
+                        sidAsString = htmlCellExtract(tdCells, 4, 1, 3);
+                        sid = Integer.parseInt(sidAsString);
+                    } catch (NumberFormatException e) {
+                        logger.error("Channel " + name + ": Unable to parse the channel SID: " + sidAsString);
+                        continue;
+                    } catch (ArrayIndexOutOfBoundsException e) {
+                        logger.error("Channel " + name + ": Error while trying to parse the channel SID", e);
+                        continue;
                     }
-                    String name = htmlCellExtract(tdCells, 1, 0, 3);
-                    String category = htmlCellExtract(tdCells, 0, 0, 2);
                     String encryption = htmlCellExtract(tdCells, 2, 1, 3);
                     int symbolRate = 27500;
+                    String symbolRateAsString = null;
                     try {
-                        symbolRate = (int) (Double.parseDouble(htmlCellExtract(tdCells, 3, 0, 3))) * 1000;
+                        symbolRateAsString = htmlCellExtract(tdCells, 3, 0, 3);
+                        symbolRate = (int) (Double.parseDouble(symbolRateAsString)) * 1000;
                     } catch (ArrayIndexOutOfBoundsException e) {
+                        logger.warn("Cannot parse symbol rate: for media " + name);
                     } catch (NumberFormatException e) {
+                        logger.warn("Cannot parse symbol rate: " + symbolRateAsString + " for media " + name);
                     }
                     int errorCorrection = 9;
+                    String errorCorrectionAsString = null;
                     try {
-                        errorCorrection = Integer.parseInt((htmlCellExtract(tdCells, 3, 0, 3).substring(0, 1)));
+                        errorCorrectionAsString = htmlCellExtract(tdCells, 3, 1, 3);
+                        if (errorCorrectionAsString.length() > 1 && errorCorrectionAsString.charAt(1) == '/') {
+                            // A FEC of 7/8 => --dvb-fec=7 for VLC
+                            errorCorrectionAsString = errorCorrectionAsString.substring(0, 1);
+                            errorCorrection = Integer.parseInt(errorCorrectionAsString);
+                        }
                     } catch (ArrayIndexOutOfBoundsException e) {
-                    } catch (StringIndexOutOfBoundsException e) {
+                        logger.warn("Unable to parse error correction: for media " + name);
                     } catch (NumberFormatException e) {
-                    }
-
-                    int sid = 0;
-                    try {
-                        sid = Integer.parseInt(htmlCellExtract(tdCells, 4, 1, 3));
-                    } catch (NumberFormatException e) {
-                    } catch (ArrayIndexOutOfBoundsException e) {
+                        logger.warn("Unable to parse error correction: " + errorCorrectionAsString + " for media " + name);
                     }
                     String country = htmlCellExtract(tdCells, 5, 0, 3);
                     SatChannel ch = new SatChannel();
@@ -344,24 +380,23 @@ public class DataImpl implements Data {
                     ch.setSymbolRate(symbolRate);
                     ch.setErrorCorrection(errorCorrection);
                     ch.setSid(sid);
-                    ch.setCountry(country);
-                    if (tvFilter.contains(ch.getCategory())) {
-                        newMedias.add(ch);
-                    }
+                    ch.setCountry(country.length() > 0 ? country : null);
+                    newMedias.add(ch);
                 }
             }
-        }
-        catch (ParserException e) {
-            throw new IOException();
+        } catch (ParserException e) {
+            throw new IOException(e.getMessage());
         }
 
+        Set<String> coverages = new HashSet<String>();
         List<Media> medias = vlmaDao.getMedias();
         for (Media ch : newMedias) {
             logger.debug("Adding " + ch.getName());
             coverages.add(((SatChannel)ch).getCoverage());
             for (Media media : medias) {
-                if (ch.equals(media))
+                if (ch.equals(media)) {
                     ch.setPrograms(media.getPrograms());
+                }
             }
         }
 
diff --git a/vlma-webapp/src/main/java/org/videolan/vlma/web/controller/media/MediaProgramAddController.java b/vlma-webapp/src/main/java/org/videolan/vlma/web/controller/media/MediaProgramAddController.java
index 8523aee..2c6ed6c 100644
--- a/vlma-webapp/src/main/java/org/videolan/vlma/web/controller/media/MediaProgramAddController.java
+++ b/vlma-webapp/src/main/java/org/videolan/vlma/web/controller/media/MediaProgramAddController.java
@@ -182,10 +182,11 @@ public class MediaProgramAddController extends SimpleFormController {
         mediasProgramAdd.setPriority("10");
         if (media instanceof SatChannel){
             String country = ((SatChannel)media).getCountry();
-            if (country.length() > 0)
+            if (country != null && country.length() > 0) {
                 mediasProgramAdd.setSap("[" + country + "] " + media.getName());
-            else
+            } else {
                 mediasProgramAdd.setSap(media.getName());
+            }
         }
         mediasProgramAdd.setSap(media.getName());
         String sapGroupId = null;



More information about the vlma-devel mailing list