Exemplo n.º 1
1
    /**
     * look for a tag whose text is getStartTag() then read until it closes
     *
     * @return true if there is data
     * @throws java.io.IOException
     */
    public boolean nextKeyValue() throws IOException {
      String current = m_Sb.toString();
      if (current.contains("<scan num=\"67\"")) current = m_Sb.toString(); // break here

      if (readFromCurrentBuffer()) return true;
      int newSize;
      if (m_Current > m_End) { // we are the the end of the split
        m_Key = null;
        m_Value = null;
        m_Sb.setLength(0);
        return false;
      }

      newSize = m_Input.read(m_Buffer);

      while (newSize > 0) {
        m_Current += newSize;
        String read = new String(m_Buffer, 0, newSize);
        m_Sb.append(read);
        if (readFromCurrentBuffer()) return true;
        if (m_Current > m_End) { // we are the the end of the split
          String s = m_Sb.toString();
          if (bufferHasStartTag() == -1) { // not working on a tag
            m_Key = null;
            m_Value = null;
            m_Sb.setLength(0);
            return false;
          }
          if (m_Sb.length() > getMaxTagLength()) {
            m_Key = null;
            m_Value = null;
            m_Sb.setLength(0);
            return false;
          }
        }

        newSize = m_Input.read(m_Buffer);
      }
      // exit because we are at the m_End
      if (newSize <= 0) {
        m_Key = null;
        m_Value = null;
        m_Sb.setLength(0);
        return false;
      }
      if (m_Current > m_End) { // we are the the end of the split
        m_Key = null;
        m_Value = null;
        m_Sb.setLength(0);
        return false;
      }

      return true;
    }
Exemplo n.º 2
0
  private String ask(String cmd) {
    BufferedWriter out = null;
    BufferedReader in = null;
    Socket sock = null;
    String ret = "";
    try {
      System.out.println(server);
      sock = new Socket(server, ServerListener.PORT);
      out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
      out.write(cmd, 0, cmd.length());
      out.flush();
      in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      int inr = in.read();
      while (inr != ';') {
        ret += Character.toString((char) inr);
        inr = in.read();
      }
    } catch (IOException io) {
      io.printStackTrace();
    } finally {
      try {
        out.close();
        in.close();
        sock.close();
      } catch (IOException io) {
        io.printStackTrace();
      }
    }

    return ret;
  }
Exemplo n.º 3
0
  public static char[] readData(BufferedReader in) throws IOException {
    char[] c1 = new char[PACKET_SIZE];
    int i;
    int c_temp = in.read(); // c_temp is an int and not a char;
    if (c_temp != -1) {
      c1[0] = (char) c_temp;
    }

    for (i = 1; i < PACKET_SIZE && c_temp != -1; i++) {
      c_temp = in.read();
      c1[i] = (char) c_temp;
    }
    return c1;
  }
Exemplo n.º 4
0
  public void encrypting() {
    String plainString;
    char plainChar;
    int padInt = 0;
    char cipherChar;
    String newS = "";

    // while( encrypt.hasNextLine( ) )
    // {
    plainString = encrypt.nextLine();

    for (int i = 0; i < plainString.length(); i++) {
      plainChar = plainString.charAt(i);
      System.out.print(plainChar);
      try {
        padInt = pad.read();
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }
      cipherChar = (char) ((plainChar + padInt) % 128);
      newS = newS + cipherChar;
    }
    // }

    System.out.println("");
    System.out.println("Output  (plaintext): ");
    System.out.println(newS);
    out.print(newS);
    out.close();
    try {
      pad.reset();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
Exemplo n.º 5
0
  public void decrypting() {
    String cipherString;
    char cipherChar;
    int padInt = 0;
    char plainChar;
    String newS = "";

    while (decrypt.hasNextLine()) {
      cipherString = decrypt.nextLine();

      for (int i = 0; i < cipherString.length(); i++) {
        cipherChar = cipherString.charAt(i);
        System.out.print(cipherChar);
        try {
          padInt = pad.read();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }
        plainChar = (char) ((cipherChar - padInt + 128) % 128);
        newS = newS + plainChar;
      }
    }

    System.out.println("");
    System.out.println("Output  (plaintext): ");
    System.out.println(newS);
    try {
      pad.reset();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
Exemplo n.º 6
0
  public boolean shutdown(int port, boolean ssl) {
    try {
      String protocol = "http" + (ssl ? "s" : "");
      URL url = new URL(protocol, "127.0.0.1", port, "shutdown");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setRequestProperty("servicemanager", "shutdown");
      conn.connect();

      StringBuffer sb = new StringBuffer();
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
      int n;
      char[] cbuf = new char[1024];
      while ((n = br.read(cbuf, 0, cbuf.length)) != -1) sb.append(cbuf, 0, n);
      br.close();
      String message = sb.toString().replace("<br>", "\n");
      if (message.contains("Goodbye")) {
        cp.appendln("Shutting down the server:");
        String[] lines = message.split("\n");
        for (String line : lines) {
          cp.append("...");
          cp.appendln(line);
        }
        return true;
      }
    } catch (Exception ex) {
    }
    cp.appendln("Unable to shutdown CTP");
    return false;
  }
Exemplo n.º 7
0
  /**
   * Empfaengt einen String (blockiert solange, bis der eingestellte TimeOut ueberschritten wurde)
   * ueber die Socket-Verbindung vom Communicator-Objekt des Clients.
   *
   * @return <code>String</code> empfangene Daten
   */
  public String receive() throws InterruptedIOException, IOException {
    String s;
    int len;
    char[] c;
    StringWriter w = new StringWriter();
    PrintWriter result = new PrintWriter(w);

    // falls nichts zu lesen ist
    s = in.readLine();
    if (s == null) return null;

    // zuerst L"ange der zu empfangenen Daten lesen, dann Daten selbst
    len = Integer.parseInt(s);
    c = new char[len];
    if (in.read(c, 0, len) == -1)
      System.out.println("ServerThread " + myNumber + " : error reading from socket!");

    /*
     * while (s.equals("")) { while(in.ready()) {
     * result.print(in.readLine()); if (in.ready()) result.println(); }
     * result.flush(); w.flush(); s = w.toString(); }
     */

    return new String(c);
  }
Exemplo n.º 8
0
 public String readword() throws IOException {
   StringBuilder b = new StringBuilder();
   int c;
   c = in.read();
   while (c >= 0 && c <= ' ') {
     c = in.read();
   }
   if (c < 0) {
     return "";
   }
   while (c > ' ') {
     b.append((char) c);
     c = in.read();
   }
   return b.toString();
 }
  private void parse(String filename) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
    StringBuffer sb = new StringBuffer();

    int j;
    while ((j = br.read()) != -1) {
      char next = (char) j;

      if (next >= 'A' && next <= 'z') {
        sb.append(next);
      }

      if (sb.length() == 4) {
        String qGram = sb.toString().toUpperCase();
        sb = new StringBuffer();

        int frequency = 0;

        if (map.containsKey(qGram)) {
          frequency = map.get(qGram);
        }

        frequency++;
        map.put(qGram, frequency);
      }
    }
    br.close();
  }
Exemplo n.º 10
0
  // Read the file into a String
  public static String readFileToString(String filename) {
    try {
      File file = new File(filename);
      if (!file.exists()) {
        System.out.println("\nFILE DOES NOT EXIST: " + filename);
      }
      BufferedReader in = new BufferedReader(new FileReader(file));

      // Create an array of characters the size of the file
      char[] allChars = new char[(int) file.length()];

      // Read the characters into the allChars array
      in.read(allChars, 0, (int) file.length());
      in.close();

      // Convert to a string
      String allCharsString = new String(allChars);

      return allCharsString;

    } catch (FileNotFoundException e) {
      System.err.println(e);
      return "";
    } catch (IOException e) {
      System.err.println(e);
      return "";
    }
  }
Exemplo n.º 11
0
  protected String consume(BufferedReader br) throws Exception {
    StringBuilder ret = new StringBuilder();

    int ch;
    while ((ch = br.read()) != -1) ret.appendCodePoint(ch);

    return ret.toString();
  }
Exemplo n.º 12
0
 private String getFileText(File file) throws Exception {
   BufferedReader br =
       new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
   StringWriter sw = new StringWriter();
   int n;
   char[] cbuf = new char[1024];
   while ((n = br.read(cbuf, 0, cbuf.length)) != -1) sw.write(cbuf, 0, n);
   br.close();
   return sw.toString();
 }
Exemplo n.º 13
0
  /**
   * Listens to the output of the debugger constantly, and notifies observers when output is
   * recieved.
   */
  public void run() {
    boolean done = false;

    while (!done) {
      try {
        /*make sure we are alive */
        if (!alive) {
          close();
          break;
        }

        int c; // = debugStreamReader.read();
        char theChar; // = (char) c;
        // buffer.append(theChar);
        //

        while (debugStreamReader.ready()) {
          c = debugStreamReader.read();
          theChar = (char) c;
          if ((c == -1) || (c == 0)) {
            done = true;
          } else {
            buffer.append(theChar);
          }
        }

        if (buffer.length() > 0) {
          String output = buffer.toString();
          TextOutputEvent event = new TextOutputEvent(output, handle);
          synchronized (observers) {
            for (TextOutputListener listener : observers) {
              listener.receiveOutput(event);
            }
            buffer.delete(0, buffer.length());
          }
        }
      } catch (IOException e) {
        /*We will eventually want to eat this */
        e.printStackTrace();
        return;
      }
    }

    try {
      debugStreamReader.close();
      return;
    } catch (IOException e) {
      /*Eat it */
    }
    return;
  }
  public static String readFileToString(String filePath) throws IOException {
    StringBuilder fileData = new StringBuilder(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));

    char[] buf = new char[10];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
      //          System.out.println(numRead);
      String readData = String.valueOf(buf, 0, numRead);
      fileData.append(readData);
      buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
  }
Exemplo n.º 15
0
 private String getResponse(InputStream in) {
   StringBuilder response = new StringBuilder();
   try {
     BufferedReader rd = new BufferedReader(new InputStreamReader(in));
     char[] buf = new char[1024];
     int read = 0;
     while ((read = rd.read(buf)) > 0) {
       response.append(buf, 0, read);
     }
     rd.close();
     in.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return response.toString();
 }
Exemplo n.º 16
0
 /**
  * Adds an entire documents to the 'brain'. Useful for feeding in stray theses, but be careful not
  * to put too much in, or you may run out of memory!
  */
 public void addDocument(String uri) throws IOException {
   BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(uri).openStream()));
   StringBuffer buffer = new StringBuffer();
   int ch = 0;
   while ((ch = reader.read()) != -1) {
     buffer.append((char) ch);
     if (END_CHARS.indexOf((char) ch) >= 0) {
       String sentence = buffer.toString();
       sentence = sentence.replace('\r', ' ');
       sentence = sentence.replace('\n', ' ');
       add(sentence);
       buffer = new StringBuffer();
     }
   }
   add(buffer.toString());
   reader.close();
 }
Exemplo n.º 17
0
 String loadFile(File file) {
   try {
     FileReader fr = new FileReader(file);
     BufferedReader br = new BufferedReader(fr);
     StringBuffer str = new StringBuffer();
     char[] buffer = new char[1024];
     int read;
     while ((read = br.read(buffer)) != -1) {
       str.append(buffer, 0, read);
     }
     fr.close();
     return str.toString();
   } catch (IOException e) {
     e.printStackTrace(System.out);
   }
   return "";
 }
Exemplo n.º 18
0
 private void dumpStream(InputStream stream) throws IOException {
   BufferedReader in = new BufferedReader(new InputStreamReader(stream));
   int i;
   try {
     while ((i = in.read()) != -1) {
       MessageOutput.printDirect((char) i); // Special case: use
       //   printDirect()
     }
   } catch (IOException ex) {
     String s = ex.getMessage();
     if (!s.startsWith("Bad file number")) {
       throw ex;
     }
     // else we got a Bad file number IOException which just means
     // that the debuggee has gone away.  We'll just treat it the
     // same as if we got an EOF.
   }
 }
Exemplo n.º 19
0
	FileHash(File f) {
		hash=new long[(int)f.length()];
		length=f.length();
		int i=0;
		int h=0;
    try {
      BufferedReader br=new BufferedReader(new FileReader(f));
      while (br.ready()) {
				int c=br.read();
				h=h+c;
				hash[i]=h;
      }
      br.close();
    } catch (Exception e) {
      System.out.println("What should I do with exception "+e+" ?");
    }
		System.out.println(""+h);
  }
Exemplo n.º 20
0
  /**
   * Returns the contents of a text file located at path.
   *
   * @param path the path to the text file.
   * @return the contents of the file.
   */
  public static String readTextFile(String path) {
    try {
      StringBuilder sb = new StringBuilder(1024);
      BufferedReader reader = new BufferedReader(new FileReader(path));

      char[] chars = new char[1024];
      int numRead;
      while ((numRead = reader.read(chars)) > -1) {
        sb.append(String.valueOf(chars, 0, numRead));
      }

      reader.close();

      return sb.toString();
    } catch (IOException e) {
      return "There is no manual for this command.";
    }
  }
Exemplo n.º 21
0
 @Override
 public void run() {
   if (reader == null) {
     return;
   }
   final char[] buffer = new char[8192];
   int len;
   try {
     while ((len = reader.read(buffer)) != -1) {
       output.append(buffer, 0, len);
       if (context != null) {
         context.log(buffer, len);
       }
     }
     reader.close();
   } catch (IOException ex) {
     exception = ex;
   }
 }
Exemplo n.º 22
0
 // Loads a map from a text file. The text file contains a description of
 // the starting state of a game. See the project wiki for a description of
 // the file format. It should be called the Planet Wars Point-in-Time
 // format. On success, return 1. On failure, returns 0.
 private int LoadMapFromFile(String mapFilename) {
   String s = "";
   BufferedReader in = null;
   try {
     in = new BufferedReader(new FileReader(mapFilename));
     int c;
     while ((c = in.read()) >= 0) {
       s += (char) c;
     }
   } catch (Exception e) {
     return 0;
   } finally {
     try {
       in.close();
     } catch (Exception e) {
       // F****d.
     }
   }
   return ParseGameState(s);
 }
Exemplo n.º 23
0
  private byte[] readMultiPartChunk(ServletInputStream requestStream, String contentType)
      throws IOException {
    // fast forward stream past multi-part header
    int boundaryOff = contentType.indexOf("boundary="); // $NON-NLS-1$
    String boundary = contentType.substring(boundaryOff + 9);
    BufferedReader reader =
        new BufferedReader(new InputStreamReader(requestStream, "ISO-8859-1")); // $NON-NLS-1$
    StringBuffer out = new StringBuffer();
    // skip headers up to the first blank line
    String line = reader.readLine();
    while (line != null && line.length() > 0) line = reader.readLine();
    // now process the file

    char[] buf = new char[1000];
    int read;
    while ((read = reader.read(buf)) > 0) {
      out.append(buf, 0, read);
    }
    // remove the boundary from the output (end of input is \r\n--<boundary>--\r\n)
    out.setLength(out.length() - (boundary.length() + 8));
    return out.toString().getBytes("ISO-8859-1"); // $NON-NLS-1$
  }
Exemplo n.º 24
0
 public static void GetDownloadIndex() throws Exception {
   BufferedReader in = new BufferedReader(new InputStreamReader(ylink.openStream()));
   // BufferedWriter out = new BufferedWriter(new FileWriter("output.html"));
   int b;
   String op = null;
   while ((b = in.read()) != -1) {
     //	out.write(b);
     op = op + (char) b;
   }
   String[] list = op.split("url=");
   for (String i : list) {
     if (i.startsWith("https")) {
       set.add(i.split(",")[0]);
     }
   }
   String url = null, itag = null, quality_label = null, type = null;
   for (String i : set) {
     String temp = i;
     temp = temp.replace("\\", "\\\\");
     for (String sdata : temp.split("\\\\")) {
       if (sdata.startsWith("https")) url = sdata;
     }
     if (url != null) {
       if (url.startsWith("https")) {
         url = url.split(" ")[0];
         url = URLDecoder.decode(url, "UTF-8");
         if (url.contains("mime=")) type = (url.split("mime=")[1]).split("&")[0];
         if (url.contains("itag")) itag = (url.split("itag=")[1]).split("&")[0];
         if (url.contains("&gir=yes")) {
           if (type.startsWith("video"))
             video_set.add(url + "\ntype: " + type + "\nitag: " + itag);
           else if (type.startsWith("audio"))
             audio_set.add(url + "\ntype: " + type + "\nitag: " + itag);
         } else av_set.add(url + "\ntype: " + type + "\nitag: " + itag);
       }
     }
   }
 }
Exemplo n.º 25
0
  public static String Run(String[] cmd, String path) {
    theRun = new StringBuffer();
    try {
      process = Runtime.getRuntime().exec(cmd, null, new File(path));

      reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
      int read;
      char[] buffer = new char[4096];
      output = new StringBuffer();
      while ((read = reader.read(buffer)) > 0) {
        theRun = output.append(buffer, 0, read);
      }
      reader.close();
      process.waitFor();

    } catch (IOException e) {
      throw new RuntimeException(e);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }

    return theRun.toString().trim();
  }
  public static int beginGUIConfigurationTransaction(String sConfigurationFileName, String sUser) {

    String sConfigurationLockFile;
    BufferedWriter bwBufferedWriter;
    File fFile;

    sConfigurationLockFile = new String(sConfigurationFileName + ".lck");

    System.out.println("beginGUIConfigurationTransaction: " + sConfigurationFileName);

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.exists()) {
      BufferedReader brBufferedReader;
      char cLastConfigurationUser[];

      cLastConfigurationUser = new char[((int) fFile.length())];

      try {
        brBufferedReader =
            new BufferedReader(new FileReader(URLDecoder.decode(sConfigurationLockFile, "UTF-8")));
        brBufferedReader.read(cLastConfigurationUser, 0, (int) (fFile.length()));
        brBufferedReader.close();

        System.out.println(
            "Last configuration user: "******"Operation on BufferedWriter failed (1)",
            "ConfigurationTransaction",
            JOptionPane.ERROR_MESSAGE);

        return 1;
      }

      if (String.copyValueOf(cLastConfigurationUser).compareTo(sUser) == 0) {
        System.out.println("File. delete: " + sConfigurationFileName);

        if (fFile.delete() == false) {
          JOptionPane.showMessageDialog(
              null, "fFile.delete failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

          return 2;
        }
      } else {
        JOptionPane.showMessageDialog(
            null,
            "The configuration is locked by "
                + String.copyValueOf(cLastConfigurationUser)
                + ".\nYou cannot change the configuration.",
            "ConfigurationTransaction",
            JOptionPane.PLAIN_MESSAGE);

        return 3;
      }
    }

    try {
      bwBufferedWriter =
          new BufferedWriter(new FileWriter(URLDecoder.decode(sConfigurationLockFile, "UTF-8")));
      bwBufferedWriter.write(sUser, 0, sUser.length());
      bwBufferedWriter.close();

      System.out.println("Configuration user written: " + sUser);
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null,
          "Operation on BufferedWriter failed (" + e + ")",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 4;
    }

    return 0;
  }
Exemplo n.º 27
0
  public CommentsCollection parse(final InputStream in, final String charsetName)
      throws IOException, UnsupportedEncodingException {
    boolean lastWasASlashR = false;
    BufferedReader br = new BufferedReader(new InputStreamReader(in, charsetName));
    CommentsCollection comments = new CommentsCollection();
    int r;

    Deque prevTwoChars = new LinkedList<Character>(Arrays.asList('z', 'z'));

    State state = State.CODE;
    LineComment currentLineComment = null;
    BlockComment currentBlockComment = null;
    StringBuffer currentContent = null;

    int currLine = 1;
    int currCol = 1;

    while ((r = br.read()) != -1) {
      char c = (char) r;
      if (c == '\r') {
        lastWasASlashR = true;
      } else if (c == '\n' && lastWasASlashR) {
        lastWasASlashR = false;
        continue;
      } else {
        lastWasASlashR = false;
      }
      switch (state) {
        case CODE:
          if (prevTwoChars.peekLast().equals('/') && c == '/') {
            currentLineComment = new LineComment();
            currentLineComment.setBeginLine(currLine);
            currentLineComment.setBeginColumn(currCol - 1);
            state = State.IN_LINE_COMMENT;
            currentContent = new StringBuffer();
          } else if (prevTwoChars.peekLast().equals('/') && c == '*') {
            currentBlockComment = new BlockComment();
            currentBlockComment.setBeginLine(currLine);
            currentBlockComment.setBeginColumn(currCol - 1);
            state = State.IN_BLOCK_COMMENT;
            currentContent = new StringBuffer();
          } else if (c == '"') {
            state = State.IN_STRING;
          } else {
            // nothing to do
          }
          break;
        case IN_LINE_COMMENT:
          if (c == '\n' || c == '\r') {
            currentLineComment.setContent(currentContent.toString());
            currentLineComment.setEndLine(currLine);
            currentLineComment.setEndColumn(currCol);
            comments.addComment(currentLineComment);
            state = State.CODE;
          } else {
            currentContent.append(c);
          }
          break;
        case IN_BLOCK_COMMENT:
          if (prevTwoChars.peekLast().equals('*')
              && c == '/'
              && !prevTwoChars.peekFirst().equals('/')) {

            // delete last character
            String content =
                currentContent.deleteCharAt(currentContent.toString().length() - 1).toString();

            if (content.startsWith("*")) {
              JavadocComment javadocComment = new JavadocComment();
              javadocComment.setContent(content.substring(1));
              javadocComment.setBeginLine(currentBlockComment.getBeginLine());
              javadocComment.setBeginColumn(currentBlockComment.getBeginColumn());
              javadocComment.setEndLine(currLine);
              javadocComment.setEndColumn(currCol + 1);
              comments.addComment(javadocComment);
            } else {
              currentBlockComment.setContent(content);
              currentBlockComment.setEndLine(currLine);
              currentBlockComment.setEndColumn(currCol + 1);
              comments.addComment(currentBlockComment);
            }
            state = State.CODE;
          } else {
            currentContent.append(c == '\r' ? '\n' : c);
          }
          break;
        case IN_STRING:
          if (!prevTwoChars.peekLast().equals('\\') && c == '"') {
            state = State.CODE;
          }
          break;
        default:
          throw new RuntimeException("Unexpected");
      }
      switch (c) {
        case '\n':
        case '\r':
          currLine += 1;
          currCol = 1;
          break;
        case '\t':
          currCol += COLUMNS_PER_TAB;
          break;
        default:
          currCol += 1;
      }
      prevTwoChars.remove();
      prevTwoChars.add(c);
    }

    if (state == State.IN_LINE_COMMENT) {
      currentLineComment.setContent(currentContent.toString());
      currentLineComment.setEndLine(currLine);
      currentLineComment.setEndColumn(currCol);
      comments.addComment(currentLineComment);
    }

    return comments;
  }
  // This uses the key to get the entire record from the web page
  // For now the fields that compose the record will be hardcoded
  // but these fields need to be checked with the advertisement
  // in future releases. It then uses the parameter passed to it
  // to decide which fields to return
  // Also put's the field values in a Hashtable that can be later accessed
  private synchronized String getRecordFields(String key, String param) {

    fieldsTable = null;

    String city = new String(key);
    displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " + "city = " + city);

    // Add the URL extension corresponding to the city.
    StringBuffer sbuf = new StringBuffer();
    for (int i = 0; i < city.length(); i++) {
      char ch = city.charAt(i);
      if (ch != ' ' && ch != '\t' && ch != '\n') sbuf.append(ch);
    }
    String formURLString = siteURLString + new String(sbuf) + ".html";

    URL infoURL = null;
    try {
      infoURL = new URL(formURLString);
    } catch (MalformedURLException e) {
      displayMessage(
          "ERROR",
          "WCNForecastWeatherQueryFn::getRecordFields " + "Malformed URL " + formURLString);
      return null;
    }
    // displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    //	   "Got URL for site: " + formURLString);
    Object content = null;
    try {
      content = infoURL.getContent();
    } catch (IOException e) {
      displayMessage(
          "ERROR",
          "WCNForecastWeatherQueryFn::getRecordFields "
              + "Can't load content from "
              + formURLString);
      return null;
    }
    InputStreamReader input = new InputStreamReader((InputStream) content);
    BufferedReader reader = new BufferedReader(input);
    sbuf.setLength(0);
    boolean stillReading = true;
    char[] cbuf = new char[100000];
    int ccount = 0;
    long startTime = System.currentTimeMillis();
    while (stillReading) {
      try {
        ccount = reader.read(cbuf);
      } catch (IOException e) {
        displayMessage(
            "ERROR",
            "WCNForecastWeatherQueryFn::getRecordFields "
                + "IO error getting content from "
                + formURLString);
        return null;
      }
      if (ccount > 0) sbuf.append(cbuf, 0, ccount);
      if (ccount < 0) stillReading = false;
    }
    // displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    //	   "Time to read content: " + ((System.currentTimeMillis() - startTime) / 1000.0) +
    //            " seconds");
    String page = sbuf.toString();
    StringBuffer day = new StringBuffer();
    StringBuffer condition = new StringBuffer();

    int pos = page.indexOf("<!-- forecast include");
    int delimit;
    pos = page.indexOf("<TR>", pos);
    pos = page.indexOf("<TR>", pos + 1);
    pos = page.indexOf("<TR>", pos + 1);
    for (int n = 0; n < 10; n++) {
      pos = page.indexOf("<TR>", pos + 1);
      int daypos = page.indexOf("<BR>", pos) + 4;
      delimit = page.indexOf("</B>", daypos);
      day.append("(" + page.substring(daypos, delimit) + (n == 9 ? ")" : ") "));
      pos = page.indexOf("<FONT", daypos);
      int condpos = page.indexOf(">", pos) + 1;
      delimit = page.indexOf("</FONT>", condpos);
      condition.append("(" + page.substring(condpos, delimit) + (n == 9 ? ")" : ") "));
    }

    // Get the parser corresponding to the initial URL
    /*
       Parser infoParser = null;
       try {
         infoParser = new Parser(infoURL,true,display);
       } catch (Exception ex) {
         infoParser = null;
       }

       if (infoParser == null)     {
         displayMessage("ERROR",  "WCNForecastWeatherQueryFn::getRecordFields " +
    	     "Cannot initialize parser for page " + formURLString );
         return null ;
       }


       if (FILE_WRITE) {
         displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    	     "Site URL: " + formURLString +
    	     " Calling writeContent()." + FILE_WRITE_EXT_NUM);
         writeContent(infoParser.getContent());
       }


       displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    	   "ready to return record fields: " + param);

       displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    	   "city is: " + key);



       String ct = null;
       StringBuffer day = new StringBuffer();
       StringBuffer condition = new StringBuffer();
       String tag = null;

       while ((tag = infoParser.nextTag("NONE")) != null) {
         ct = infoParser.getContentBetween ();
         if ((ct == null) || (ct.length() == 0)) continue;
         ct = ct.replace('\n', ' ');
         ct = ct.replace('\r', ' ');
         if (ct.toLowerCase().indexOf("sunrise:") != -1) {
           tag = infoParser.nextTag("NONE");
    while (tag != null && !tag.equalsIgnoreCase("table"))
      tag = infoParser.nextTag("NONE");
    while (tag != null && !tag.equalsIgnoreCase("span"))
      tag = infoParser.nextTag("NONE");
           for ( int n = 0; n < 5; n++ ) {
             if (tag != null) {
               infoParser.removeContentBetween();
               tag = infoParser.nextTag("NONE");
               while (tag != null && !tag.equalsIgnoreCase("/span"))
                 tag = infoParser.nextTag("NONE");
               if (tag != null) {
                 day.append(infoParser.getContentBetween() + (n == 4 ? "" : " "));
                 tag = infoParser.nextTag("IMG");
                 while (tag != null && !tag.equalsIgnoreCase("img"))
                   tag = infoParser.nextTag("IMG");
                 if (tag != null)
                   condition.append(infoParser.getParameterValue("ALT") + (n == 4 ? "" : " "));
               }
             }
             tag = infoParser.nextTag("NONE");
             while (tag != null && !tag.equalsIgnoreCase("img"))
               tag = infoParser.nextTag("NONE");
             while (tag != null && !tag.equalsIgnoreCase("span"))
               tag = infoParser.nextTag("NONE");
             System.out.println("Content after condition = " + infoParser.getContentBetween());
           }
         }
         infoParser.removeContentBetween();
       }
       */

    // If we couldn't get at least a temperature from the page, it must
    // have been an invalid URL, ie.e, a city not in CNN database or
    // incorrectly spelled or designated.
    if (day == null) {
      displayMessage(
          "ERROR",
          "WCNForecastWeatherQueryFn::getRecordFields "
              + "URL "
              + formURLString
              + " is not a valid URL. "
              + "The \"CityCountry\" or \"CityState\" key may be wrong.");
      return null;
    }

    String weather =
        WEATHER_PERF_STRING
            + " "
            + ":"
            + DAY_STRING
            + " ("
            + day.toString()
            + ") "
            + ":"
            + CONDITION_STRING
            + " ("
            + condition.toString()
            + ")";

    if (param.equalsIgnoreCase(ALL_FIELDS)) {

      fieldsTable = new Hashtable();

      if (key != null) {
        fieldsTable.put(CITY_STRING, key);
      }
      if (day != null) {
        fieldsTable.put(DAY_STRING, day);
      }
      if (condition != null) {
        fieldsTable.put(CONDITION_STRING, condition);
      }
      if (formURLString != null) {
        fieldsTable.put(WEATHER_URL_STRING, formURLString);
      }

      if (key == null) {
        key = " ";
      }

      if (formURLString == null) {
        formURLString = " ";
      }

      String reply =
          "(reply :"
              + CITY_STRING
              + " ("
              + key
              + ") "
              + ":"
              + WEATHER_STRING
              + " ("
              + weather
              + ") "
              + ":"
              + WEATHER_URL_STRING
              + " ("
              + formURLString
              + "))";
      displayMessage(
          "DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " + "Weather is: " + reply);
      return reply;
    } else if (param.equalsIgnoreCase(CITY_STRING)) {
      fieldsTable = new Hashtable();
      fieldsTable.put(CITY_STRING, key);
      return key;
    } else if (param.equalsIgnoreCase(WEATHER_STRING)) {
      fieldsTable = new Hashtable();
      fieldsTable.put(WEATHER_STRING, weather);
      return weather;
    } else if (param.equalsIgnoreCase(WEATHER_URL_STRING)) {
      fieldsTable = new Hashtable();
      fieldsTable.put(WEATHER_URL_STRING, formURLString);
      return formURLString;
    } else {
      return null;
    }
  }
Exemplo n.º 29
0
  private void run(String[] argv) throws IOException {
    int i, index;
    BufferedReader fp = null, fp_restore = null;
    String save_filename = null;
    String restore_filename = null;
    String data_filename = null;

    for (i = 0; i < argv.length; i++) {
      if (argv[i].charAt(0) != '-') break;
      ++i;
      switch (argv[i - 1].charAt(1)) {
        case 'l':
          lower = Double.parseDouble(argv[i]);
          break;
        case 'u':
          upper = Double.parseDouble(argv[i]);
          break;
        case 'y':
          y_lower = Double.parseDouble(argv[i]);
          ++i;
          y_upper = Double.parseDouble(argv[i]);
          y_scaling = true;
          break;
        case 's':
          save_filename = argv[i];
          break;
        case 'r':
          restore_filename = argv[i];
          break;
        default:
          System.err.println("unknown option");
          exit_with_help();
      }
    }

    if (!(upper > lower) || (y_scaling && !(y_upper > y_lower))) {
      System.err.println("inconsistent lower/upper specification");
      System.exit(1);
    }
    if (restore_filename != null && save_filename != null) {
      System.err.println("cannot use -r and -s simultaneously");
      System.exit(1);
    }

    if (argv.length != i + 1) exit_with_help();

    data_filename = argv[i];
    try {
      fp = new BufferedReader(new FileReader(data_filename));
    } catch (Exception e) {
      System.err.println("can't open file " + data_filename);
      System.exit(1);
    }

    /* assumption: min index of attributes is 1 */
    /* pass 1: find out max index of attributes */
    max_index = 0;

    if (restore_filename != null) {
      int idx, c;

      try {
        fp_restore = new BufferedReader(new FileReader(restore_filename));
      } catch (Exception e) {
        System.err.println("can't open file " + restore_filename);
        System.exit(1);
      }
      if ((c = fp_restore.read()) == 'y') {
        fp_restore.readLine();
        fp_restore.readLine();
        fp_restore.readLine();
      }
      fp_restore.readLine();
      fp_restore.readLine();

      String restore_line = null;
      while ((restore_line = fp_restore.readLine()) != null) {
        StringTokenizer st2 = new StringTokenizer(restore_line);
        idx = Integer.parseInt(st2.nextToken());
        max_index = Math.max(max_index, idx);
      }
      fp_restore = rewind(fp_restore, restore_filename);
    }

    while (readline(fp) != null) {
      StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
      System.out.println(line);
      st.nextToken();
      while (st.hasMoreTokens()) {
        index = Integer.parseInt(st.nextToken());
        max_index = Math.max(max_index, index);
        st.nextToken();
        num_nonzeros++;
      }
    }

    try {
      feature_max = new double[(max_index + 1)];
      feature_min = new double[(max_index + 1)];
    } catch (OutOfMemoryError e) {
      System.err.println("can't allocate enough memory");
      System.exit(1);
    }

    for (i = 0; i <= max_index; i++) {
      feature_max[i] = -Double.MAX_VALUE;
      feature_min[i] = Double.MAX_VALUE;
    }

    fp = rewind(fp, data_filename);

    /* pass 2: find out min/max value */
    while (readline(fp) != null) {
      int next_index = 1;
      double target;
      double value;

      StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
      target = Double.parseDouble(st.nextToken());
      y_max = Math.max(y_max, target);
      y_min = Math.min(y_min, target);

      while (st.hasMoreTokens()) {
        index = Integer.parseInt(st.nextToken());
        value = Double.parseDouble(st.nextToken());

        for (i = next_index; i < index; i++) {
          feature_max[i] = Math.max(feature_max[i], 0);
          feature_min[i] = Math.min(feature_min[i], 0);
        }

        feature_max[index] = Math.max(feature_max[index], value);
        feature_min[index] = Math.min(feature_min[index], value);
        next_index = index + 1;
      }

      for (i = next_index; i <= max_index; i++) {
        feature_max[i] = Math.max(feature_max[i], 0);
        feature_min[i] = Math.min(feature_min[i], 0);
      }
    }

    fp = rewind(fp, data_filename);

    /* pass 2.5: save/restore feature_min/feature_max */
    if (restore_filename != null) {
      // fp_restore rewinded in finding max_index
      int idx, c;
      double fmin, fmax;

      fp_restore.mark(2); // for reset
      if ((c = fp_restore.read()) == 'y') {
        fp_restore.readLine(); // pass the '\n' after 'y'
        StringTokenizer st = new StringTokenizer(fp_restore.readLine());
        y_lower = Double.parseDouble(st.nextToken());
        y_upper = Double.parseDouble(st.nextToken());
        st = new StringTokenizer(fp_restore.readLine());
        y_min = Double.parseDouble(st.nextToken());
        y_max = Double.parseDouble(st.nextToken());
        y_scaling = true;
      } else fp_restore.reset();

      if (fp_restore.read() == 'x') {
        fp_restore.readLine(); // pass the '\n' after 'x'
        StringTokenizer st = new StringTokenizer(fp_restore.readLine());
        lower = Double.parseDouble(st.nextToken());
        upper = Double.parseDouble(st.nextToken());
        String restore_line = null;
        while ((restore_line = fp_restore.readLine()) != null) {
          StringTokenizer st2 = new StringTokenizer(restore_line);
          idx = Integer.parseInt(st2.nextToken());
          fmin = Double.parseDouble(st2.nextToken());
          fmax = Double.parseDouble(st2.nextToken());
          if (idx <= max_index) {
            feature_min[idx] = fmin;
            feature_max[idx] = fmax;
          }
        }
      }
      fp_restore.close();
    }

    if (save_filename != null) {
      Formatter formatter = new Formatter(new StringBuilder());
      BufferedWriter fp_save = null;

      try {
        fp_save = new BufferedWriter(new FileWriter(save_filename));
      } catch (IOException e) {
        System.err.println("can't open file " + save_filename);
        System.exit(1);
      }

      if (y_scaling) {
        formatter.format("y\n");
        formatter.format("%.16g %.16g\n", y_lower, y_upper);
        formatter.format("%.16g %.16g\n", y_min, y_max);
      }
      formatter.format("x\n");
      formatter.format("%.16g %.16g\n", lower, upper);
      for (i = 1; i <= max_index; i++) {
        if (feature_min[i] != feature_max[i])
          formatter.format("%d %.16g %.16g\n", i, feature_min[i], feature_max[i]);
      }
      fp_save.write(formatter.toString());
      fp_save.close();
    }

    /* pass 3: scale */
    while (readline(fp) != null) {
      int next_index = 1;
      double target;
      double value;

      StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
      target = Double.parseDouble(st.nextToken());
      output_target(target);
      while (st.hasMoreElements()) {
        index = Integer.parseInt(st.nextToken());
        value = Double.parseDouble(st.nextToken());
        for (i = next_index; i < index; i++) output(i, 0);
        output(index, value);
        next_index = index + 1;
      }

      for (i = next_index; i <= max_index; i++) output(i, 0);
      System.out.print("\n");
    }
    if (new_num_nonzeros > num_nonzeros)
      System.err.print(
          "WARNING: original #nonzeros "
              + num_nonzeros
              + "\n"
              + "         new      #nonzeros "
              + new_num_nonzeros
              + "\n"
              + "Use -l 0 if many original feature values are zeros\n");

    fp.close();
  }
Exemplo n.º 30
0
  private void parseStream(BufferedReader in, Map replaces) throws IOException {
    int c;
    ParseUnit unit;

    unitList.clear();
    mode = NORMAL;
    buf = new StringBuffer();

    c = in.read();
    while (c >= 0) {
      switch (c) {
        case '<':
          if (mode == NORMAL && buf.length() > 0) {
            test = buf.toString();
            if (test.startsWith("<!--")) {
              mode = COMMENT;
            } else if (test.startsWith("<%--")) {
              mode = JSP_COMMENT;
            } else if (test.startsWith("<%")) {
              mode = JSP;
            } else {
              if (replaces != null) test = StringUtils.replaceStrings(test, replaces);
              // test = StringUtils.stripEnterSymbol(test);
              unitList.add(new ParseUnit(test, mode));
              buf.setLength(0);
            }
          }
          buf.append((char) c);
          break;
        case '>':
          buf.append((char) c);
          test = buf.toString();
          if (mode == NORMAL) {
            if (test.startsWith("<!--")) {
              mode = COMMENT;
            } else if (test.startsWith("<%--")) {
              mode = JSP_COMMENT;
            } else if (test.startsWith("<%")) {
              mode = JSP;
            }
          }
          switch (mode) {
            case COMMENT:
              if (!test.endsWith("-->")) {
                test = null;
              }
              break;
            case JSP_COMMENT:
              if (!test.endsWith("--%>")) {
                test = null;
              }
              break;
            case JSP:
              if (!test.endsWith("%>")) {
                test = null;
              }
              break;
            case SCRIPT_BODY:
              checkBodyEnd("</SCRIPT>", replaces);
              break;
            case STYLE_BODY:
              checkBodyEnd("</STYLE>", replaces);
              break;
          }

          if (test != null) {
            //
            if (replaces != null) test = StringUtils.replaceStrings(test, replaces);

            unit = new ParseUnit(test, mode);
            unitList.add(unit);
            buf.setLength(0);
            mode = NORMAL;
            if (unit.isStartTag()) {
              test = unit.getTagName();
              if (test.equals("script")) {
                mode = SCRIPT_BODY;
              } else if (test.equals("style")) {
                mode = STYLE_BODY;
              }
            }
          }
          break;
        default:
          buf.append((char) c);
      }
      c = in.read();
    }
    if (buf.length() > 0) {
      String temp = buf.toString();
      if (replaces != null) temp = StringUtils.replaceStrings(temp, replaces);

      unitList.add(new ParseUnit(temp, mode));
    }
    units = new ParseUnit[unitList.size()];
    unitList.toArray(units);
  }