/*
// $Id$
// Farrago is an extensible data management system.
// Copyright (C) 2008-2008 The Eigenbase Project
// Copyright (C) 2008-2008 Disruptive Tech
// Copyright (C) 2008-2008 LucidEra, Inc.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version approved by The Eigenbase Project.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package net.sf.farrago.syslib;

import java.net.*;
import java.io.*;
import java.sql.*;

/**
 * FarragoPerforceUDR is a set of user-defined routines for accessing
 * information from the Eigenbase Perforce server.  Currently just
 * for fun, and guaranteed brittle.
 *
 * @author John Sichi
 * @version $Id$
 */
public abstract class FarragoPerforceUDR
{
    public static void getChangelists(
        String filePattern,
        int maxChanges,
        PreparedStatement resultInserter)
        throws Exception
    {
        String urlString =
            "http://perforce.eigenbase.org:8080/@md=d&cd=//&pat="
            + filePattern + "&c=yA1@//?ac=43";
        if (maxChanges != -1) {
            urlString += "&mx=" + maxChanges;
        }
        URL url = new URL(urlString);
        InputStream inputStream = null;

        boolean readingChanges = false;

        int iField = 1;
        StringBuilder description = new StringBuilder();

        try {
            inputStream = url.openStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            LineNumberReader lineReader = new LineNumberReader(reader);
            for (;;) {
                String line = lineReader.readLine();
                if (line == null) {
                    return;
                }
                int lineNumber = lineReader.getLineNumber();

                if (line.equals("Description</th>")) {
                    readingChanges = true;
                    continue;
                }

                if (!readingChanges) {
                    continue;
                }

                if (line.equals("</Form>")) {
                    break;
                }

                if (line.startsWith("<td")) {
                    continue;
                }

                if (line.startsWith("<th")) {
                    continue;
                }

                if (line.startsWith("<tr")) {
                    continue;
                }

                if (line.equals("</tr>")) {
                    continue;
                }

                if (line.equals("</td>")) {
                    continue;
                }

                if (line.startsWith("<img")) {
                    continue;
                }

                if (line.startsWith("<pre")) {
                    continue;
                }

                if (line.equals("</pre>")) {
                    String s = description.toString();
                    if (s.length() > 2044) {
                        s = s.substring(0, 2044) + " ...";
                    }
                    resultInserter.setString(iField, s);
                    resultInserter.executeUpdate();
                    iField = 1;
                    description = new StringBuilder();
                    continue;
                }

                if (line.startsWith("<a ")) {
                    int i = line.indexOf('>');
                    int j = line.indexOf('<', 1);
                    if ((i == -1) || (j == -1)) {
                        continue;
                    }
                    line = line.substring(i+1, j);
                }

                if (line.endsWith("</td>")) {
                    line = line.substring(0, line.length() - 5);
                }

                if (iField == 5) {
                    description.append(line);
                    description.append("\n");
                } else {
                    resultInserter.setString(iField, line);
                    ++iField;
                }
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
}

// End FarragoPerforceUDR.java

@@ -1837,65 +1837,8 @@
 = 1;
-%0A        StringBuilder description = new StringBuilder();
 %0A%0A  
@@ -3280,20 +3280,25 @@
 ne.e
-quals(%22%3C/pre
+ndsWith(%22%3Cbr%3E%3C/td
 %3E%22))
@@ -3335,30 +3335,12 @@
 s = 
-description.toString()
+line
 ;%0A  
@@ -3608,63 +3608,8 @@
  1;%0A
-                    description = new StringBuilder();%0A
     
@@ -4106,164 +4106,8 @@
  %7D%0A%0A
-                if (iField == 5) %7B%0A                    description.append(line);%0A                    description.append(%22%5Cn%22);%0A                %7D else %7B%0A    
     
@@ -4174,20 +4174,16 @@
         
-    
 ++iField
@@ -4184,34 +4184,16 @@
 iField;%0A
-                %7D%0A
         

fix scraper to match latest p4web