public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
      throws SAXException {

    try {
      if (qName.equals("p") || qName.equals("description")) {
        writer.write(getText());
        accumulator.setLength(0);
      }

      if (qName.equals("description")) {
        counting = false;
      }

      if (!counting) {
        writer.write(getText());
        accumulator.setLength(0);
        writer.write("</" + qName + ">\n");
      } else {
        if (qName.equals("row")) {
          accumulator.append(" ");
        }
        if (qName.equals("p")) {
          writer.write("\n");
          accumulator.append(" ");
        }
      }
    } catch (Exception e) {
      //		    e.printStackTrace();
      throw new GrobidException("An exception occured while running Grobid.", e);
    }
  }
  // 得到的tokens包含源代码的由词法分析器分析出的所有单词,但不包含注释
  private static ArrayList<JavaScriptToken> parse(
      Reader in, ErrorReporter reporter) // 返回tokens(保存的是源文件中出现的javascript关键字和NAME,REGEXP,STRING等类型)
      throws IOException, EvaluatorException {

    CompilerEnvirons env = new CompilerEnvirons(); // 创建编译环境对象
    env.setLanguageVersion(Context.VERSION_1_7); // 设置语言版本
    Parser parser = new Parser(env, reporter); // 创建解释器对象
    parser.parse(in, null, 1); // 解释输入流
    String source = parser.getEncodedSource(); // 获得已编码的源码(词法分析阶段通常是把从源程序中识别出的各个单词的词文
    // 转换为某种内部表示
    int offset = 0;
    int length = source.length();
    ArrayList<JavaScriptToken> tokens = new ArrayList<JavaScriptToken>();
    StringBuffer sb = new StringBuffer();

    while (offset < length) {
      int tt = source.charAt(offset++); // 获取特定位置上的字符,并转化为ASCII编码
      switch (tt) {
        case Token.CONDCOMMENT: // 条件注释
        case Token.KEEPCOMMENT: // 注释
        case Token.NAME: //
        case Token.REGEXP: // 正则表达式类型
        case Token.STRING: // String类型,js程序中双引号或单引号括起来的字符串
          sb.setLength(0);
          offset = printSourceString(source, offset, sb);
          tokens.add(new JavaScriptToken(tt, sb.toString()));
          break;

        case Token.NUMBER: // Number类型
          sb.setLength(0);
          offset = printSourceNumber(source, offset, sb);
          tokens.add(new JavaScriptToken(tt, sb.toString()));
          break;

        default:
          String literal = literals.get(new Integer(tt));
          if (literal != null) { // 若不为空,说明哈希表literals中含有键new
            // Integer(tt)所对应的值
            tokens.add(new JavaScriptToken(tt, literal)); // 将此关键字保存到数组列表tokens中
          }
          break;
      }
    }

    /*
     * //begin Iterator<JavaScriptToken> iterator = tokens.iterator();
     * JavaScriptToken token; while(iterator.hasNext()) { token =
     * iterator.next();
     * System.out.println(token.getType()+"\t"+token.getValue()); } //end
     */
    return tokens;
  }
Exemplo n.º 3
0
  /**
   * Output the specified {@link Collection} in proper columns.
   *
   * @param stuff the stuff to print
   */
  public void printColumns(final Collection stuff) throws IOException {
    if ((stuff == null) || (stuff.size() == 0)) {
      return;
    }

    int width = getTermwidth();
    int maxwidth = 0;

    for (Iterator i = stuff.iterator();
        i.hasNext();
        maxwidth = Math.max(maxwidth, i.next().toString().length())) {;
    }

    StringBuffer line = new StringBuffer();

    int showLines;

    if (usePagination) showLines = getTermheight() - 1; // page limit
    else showLines = Integer.MAX_VALUE;

    for (Iterator i = stuff.iterator(); i.hasNext(); ) {
      String cur = (String) i.next();

      if ((line.length() + maxwidth) > width) {
        printString(line.toString().trim());
        printNewline();
        line.setLength(0);
        if (--showLines == 0) { // Overflow
          printString(loc.getString("display-more"));
          flushConsole();
          int c = readVirtualKey();
          if (c == '\r' || c == '\n') showLines = 1; // one step forward
          else if (c != 'q') showLines = getTermheight() - 1; // page forward

          back(loc.getString("display-more").length());
          if (c == 'q') break; // cancel
        }
      }

      pad(cur, maxwidth + 3, line);
    }

    if (line.length() > 0) {
      printString(line.toString().trim());
      printNewline();
      line.setLength(0);
    }
  }
Exemplo n.º 4
0
  /**
   * @return the clipboard content as a String (DataFlavor.stringFlavor) Code snippet adapted from
   *     jEdit (Registers.java), http://www.jedit.org. Returns null if clipboard is empty.
   */
  public static String getClipboardStringContent(Clipboard clipboard) {
    // Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    try {
      String selection =
          (String) (clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));
      if (selection == null) return null;

      boolean trailingEOL =
          (selection.endsWith("\n") || selection.endsWith(System.getProperty("line.separator")));

      // Some Java versions return the clipboard contents using the native line separator,
      // so have to convert it here , see jEdit's "registers.java"
      BufferedReader in = new BufferedReader(new StringReader(selection));
      StringBuffer buf = new StringBuffer();
      String line;
      while ((line = in.readLine()) != null) {
        buf.append(line);
        buf.append('\n');
      }
      // remove trailing \n
      if (!trailingEOL) buf.setLength(buf.length() - 1);
      return buf.toString();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Exemplo n.º 5
0
 /*
  * Filters keywords from a line of text and formats them properly.
  */
 private String keywordFilter(String line) {
   if (line == null || line.equals("")) {
     return "";
   }
   StringBuffer buf = new StringBuffer();
   Map<String, String> usedReservedWords =
       new HashMap<String, String>(); // >= Java2 only (not thread-safe)
   // Hashtable usedReservedWords = new Hashtable(); // < Java2 (thread-safe)
   int i = 0;
   char ch;
   StringBuffer temp = new StringBuffer();
   while (i < line.length()) {
     temp.setLength(0);
     ch = line.charAt(i);
     // 65-90, uppercase letters
     // 97-122, lowercase letters
     while (i < line.length() && ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))) {
       temp.append(ch);
       i++;
       if (i < line.length()) {
         ch = line.charAt(i);
       }
     }
     String tempString = temp.toString();
     if (RESERVED_WORDS.containsKey(tempString) && !usedReservedWords.containsKey(tempString)) {
       usedReservedWords.put(tempString, tempString);
       line = replace(line, tempString, (reservedWordStart + tempString + reservedWordEnd));
       i += (reservedWordStart.length() + reservedWordEnd.length());
     } else {
       i++;
     }
   }
   buf.append(line);
   return buf.toString();
 }
Exemplo n.º 6
0
  /**
   * Reads an array of strings from the TIFF file.
   *
   * @param count Number of strings to read
   * @param value Offset from which to read
   */
  protected String[] readASCIIArray(long count, long value) throws IOException {
    _raf.seek(value);

    int nstrs = 0;
    List list = new LinkedList();
    byte[] buf = new byte[(int) count];
    _raf.read(buf);
    StringBuffer strbuf = new StringBuffer();
    for (int i = 0; i < count; i++) {
      int b = buf[i];
      if (b == 0) {
        list.add(strbuf.toString());
        strbuf.setLength(0);
      } else {
        strbuf.append((char) b);
      }
    }
    /* We can't use ArrayList.toArray because that returns an
    Object[], not a String[] ... sigh. */
    String[] strs = new String[nstrs];
    ListIterator iter = list.listIterator();
    for (int i = 0; i < nstrs; i++) {
      strs[i] = (String) iter.next();
    }
    return strs;
  }
Exemplo n.º 7
0
    public void run() {
      StringBuffer data = new StringBuffer();
      Print.logDebug("Client:InputThread started");

      while (true) {
        data.setLength(0);
        boolean timeout = false;
        try {
          if (this.readTimeout > 0L) {
            this.socket.setSoTimeout((int) this.readTimeout);
          }
          ClientSocketThread.socketReadLine(this.socket, -1, data);
        } catch (InterruptedIOException ee) { // SocketTimeoutException ee) {
          // error("Read interrupted (timeout) ...");
          if (getRunStatus() != THREAD_RUNNING) {
            break;
          }
          timeout = true;
          // continue;
        } catch (Throwable t) {
          Print.logError("Client:InputThread - " + t);
          t.printStackTrace();
          break;
        }
        if (!timeout || (data.length() > 0)) {
          ClientSocketThread.this.handleMessage(data.toString());
        }
      }

      synchronized (this.threadLock) {
        this.isRunning = false;
        Print.logDebug("Client:InputThread stopped");
        this.threadLock.notify();
      }
    }
Exemplo n.º 8
0
  private int printThreadGroup(OutputSink out, ThreadGroupReference tg, int iThread) {
    out.println("Group " + tg.name() + ":");
    List<ThreadReference> tlist = tg.threads();
    int maxId = 0;
    int maxName = 0;
    for (int i = 0; i < tlist.size(); i++) {
      ThreadReference thr = tlist.get(i);
      int len = Utils.description(thr).length();
      if (len > maxId) {
        maxId = len;
      }
      String name = thr.name();
      int iDot = name.lastIndexOf('.');
      if (iDot >= 0 && name.length() > iDot) {
        name = name.substring(iDot + 1);
      }
      if (name.length() > maxName) {
        maxName = name.length();
      }
    }
    String maxNumString = String.valueOf(iThread + tlist.size());
    int maxNumDigits = maxNumString.length();
    for (int i = 0; i < tlist.size(); i++) {
      ThreadReference thr = tlist.get(i);
      char buf[] = new char[80];
      for (int j = 0; j < 79; j++) {
        buf[j] = ' ';
      }
      buf[79] = '\0';
      StringBuffer sbOut = new StringBuffer();
      sbOut.append(buf);

      // Right-justify the thread number at start of output string
      String numString = String.valueOf(iThread + i + 1);
      sbOut.insert(maxNumDigits - numString.length(), numString);
      sbOut.insert(maxNumDigits, ".");

      int iBuf = maxNumDigits + 2;
      sbOut.insert(iBuf, Utils.description(thr));
      iBuf += maxId + 1;
      String name = thr.name();
      int iDot = name.lastIndexOf('.');
      if (iDot >= 0 && name.length() > iDot) {
        name = name.substring(iDot + 1);
      }
      sbOut.insert(iBuf, name);
      iBuf += maxName + 1;
      sbOut.insert(iBuf, Utils.getStatus(thr));
      sbOut.setLength(79);
      out.println(sbOut.toString());
    }
    for (ThreadGroupReference tg0 : tg.threadGroups()) {
      if (!tg.equals(tg0)) { // TODO ref mgt
        iThread += printThreadGroup(out, tg0, iThread + tlist.size());
      }
    }
    return tlist.size();
  }
Exemplo n.º 9
0
  static List<String> computeNGramShingles(String line, int n) {

    List<String> result = new ArrayList<String>(n);

    String[] circularQueue = new String[n];
    StringTokenizer st = new StringTokenizer(line);

    int index = 0;
    int circularQueueSize = 0;

    StringBuffer strBuf = new StringBuffer();

    while (st.hasMoreElements()) {
      String token = st.nextToken();
      if (circularQueueSize == n) {
        strBuf.setLength(0);
        for (int pn = 0; pn < n; pn++) {
          if (pn > 0) {
            strBuf.append(" ");
          }
          strBuf.append(circularQueue[(index + pn) % n]);
        }
        result.add(strBuf.toString());
        index = (index + 1) % n;
        circularQueueSize--;
      }
      circularQueue[(index + circularQueueSize) % n] = token;
      if (circularQueueSize < n) {
        circularQueueSize++;
      }
    }

    if (circularQueueSize == n) {
      strBuf.setLength(0);
      for (int pn = 0; pn < n; pn++) {
        if (pn > 0) {
          strBuf.append(" ");
        }
        strBuf.append(circularQueue[(index + pn) % n]);
      }
      result.add(strBuf.toString());
    }

    return result;
  }
Exemplo n.º 10
0
 private void processLocaleArray(
     String[] ar, String key, String appName, LanguagePreferences p, StringBuffer sb) {
   for (int i = 0; i < ar.length; i++) {
     sb.setLength(0);
     sb.append(key);
     sb.append(new Integer(i).toString());
     String val = LanguageResourceFinder.getResource(appName, sb.toString(), p);
     if (val != null) ar[i] = val;
   }
 }
  /**
   * Creates a new <code>DictionaryNameFactory</code>.
   *
   * @param file the file from which the names can be read.
   * @param nameFactory the name factory from which names will be retrieved if the list of read
   *     names has been exhausted.
   */
  public DictionaryNameFactory(File file, NameFactory nameFactory) throws IOException {
    this.names = new ArrayList();
    this.nameFactory = nameFactory;

    Reader reader = new FileReader(file);

    try {
      StringBuffer buffer = new StringBuffer();

      while (true) {
        // Read the next character.
        int c = reader.read();

        // Is it a valid identifier character?
        if (c != -1
            && (buffer.length() == 0
                ? Character.isJavaIdentifierStart((char) c)
                : Character.isJavaIdentifierPart((char) c))) {
          // Append it to the current identifier.
          buffer.append((char) c);
        } else {
          // Did we collect a new identifier?
          if (buffer.length() > 0) {
            // Add the completed name to the list of names, if it's
            // not in it yet.
            String name = buffer.toString();
            if (!names.contains(name)) {
              names.add(name);
            }

            // Clear the buffer.
            buffer.setLength(0);
          }

          // Is this the beginning of a comment line?
          if (c == COMMENT_CHARACTER) {
            // Skip all characters till the end of the line.
            do {
              c = reader.read();
            } while (c != -1 && c != '\n' && c != '\r');
          }

          // Is this the end of the file?
          if (c == -1) {
            // Just return.
            return;
          }
        }
      }
    } finally {
      reader.close();
    }
  }
Exemplo n.º 12
0
 private static void setupType(
     JaxType currentType,
     StringBuffer currentTypeName,
     ClassDoc containingClass,
     JAXDoclet<?> doclet)
     throws InvalidJaxTypeException {
   if (currentTypeName.length() == 0) {
     throw new InvalidJaxTypeException();
   }
   currentType.typeName = currentTypeName.toString();
   currentType.type = resolveType(currentType.typeName, containingClass, doclet);
   currentTypeName.setLength(0);
 }
Exemplo n.º 13
0
 public static <T> String collectionToCommaString(Collection<T> list) {
   if (CollectionUtils.isEmpty(list)) return "";
   StringBuffer result = new StringBuffer();
   for (T item : list) {
     if (item != null) {
       result.append(item.toString() + ",");
     }
   }
   if (result.length() >= 1) {
     result.setLength(result.length() - 1);
   }
   return result.toString();
 }
Exemplo n.º 14
0
 public static StringBuffer buildSaveBuffer(XMLElement auctionsData, XMLElement deletedData) {
   synchronized (_saveBuf) {
     _saveBuf.setLength(0);
     _saveBuf.append("<?xml version=\"1.0\"?>\n\n");
     _saveBuf.append(Constants.XML_SAVE_DOCTYPE);
     _saveBuf.append('\n');
     _saveBuf.append("<jbidwatcher format=\"0101\">\n");
     auctionsData.toStringBuffer(_saveBuf, 1);
     if (deletedData != null) {
       deletedData.toStringBuffer(_saveBuf, 1);
     }
     _saveBuf.append("</jbidwatcher>");
   }
   return _saveBuf;
 }
Exemplo n.º 15
0
  /**
   * parse: break the input String into fields
   *
   * @return java.util.Iterator containing each field from the original as a String, in order.
   */
  public List parse(String line) {
    StringBuffer sb = new StringBuffer();
    list.clear(); // recycle to initial state
    int i = 0;

    if (line.length() == 0) {
      list.add(line);
      return list;
    }

    do {
      sb.setLength(0);
      if (i < line.length() && line.charAt(i) == '"') i = advQuoted(line, sb, ++i); // skip quote
      else i = advPlain(line, sb, i);
      list.add(sb.toString());
      i++;
    } while (i < line.length());

    return list;
  }
Exemplo n.º 16
0
  private void checkBodyEnd(String endTag, Map replaces) {
    int index = test.length() - endTag.length();
    if (index >= 0) {
      test = test.substring(index);
      if (test.equalsIgnoreCase(endTag)) {
        if (index > 0) {

          buf.setLength(index);
          String temp = buf.toString();
          if (replaces != null) temp = StringUtils.replaceStrings(temp, replaces);

          unitList.add(new ParseUnit(temp, mode));
        }
        mode = NORMAL;
      } else {
        test = null;
      }
    } else {
      test = null;
    }
  }
Exemplo n.º 17
0
 //
 // Advanced the input by one character
 //
 protected void advance() throws java.io.IOException {
   next_char = inp.read();
   switch (next_char) {
     case -1: // EOF
       if (char_num == 0) {
         char_num = -1;
         break;
       }
       next_char = '\n';
       // pass thru
     case '\n': // a new line
       line_num++;
       char_num = 0;
       break;
     default:
       line.append((char) next_char);
       char_num++;
       return;
   }
   line.setLength(0);
 }
  /**
   * Parse end of element from document. Collects character data to the end tag and returns it with
   * whitespace stripped. Throws an exception if a start tag is seen before an end tag, or if the
   * end tag seen does not match the expected name.
   *
   * @param tag element name expected
   * @return content text with whitespace stripped
   * @throws IOException if error reading document
   * @throws XmlPullParserException if expected element not found, or if other parse error
   */
  protected String parseEndTag(String tag) throws IOException, XmlPullParserException {
    m_buffer.setLength(0);
    while (true) {
      switch (m_parser.next()) {
        case XmlPullParser.CONTENT:
          m_buffer.append(m_parser.readContent());
          break;

        case XmlPullParser.END_TAG:
          m_parser.readEndTag(m_endTag);
          if (m_endTag.getLocalName().equals(tag)) {
            return m_buffer.toString().trim();
          }
          // fall through for error handling

        case XmlPullParser.START_TAG:
        case XmlPullParser.END_DOCUMENT:
          throw new XmlPullParserException("Missing expected end tag " + tag);
      }
    }
  }
Exemplo n.º 19
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.º 20
0
  public void itemStateChanged(Item item) {
    if (item == tGrpList) {
      int index = tGrpList.getSelectedIndex();
      if (index == tGrpList.size() - 1) {
        f.set(grpFIndex, tGroup);
      }
      // tGroup.setString(group(index));
    }

    // if (item==tGroup) {
    //    updateChoise(tGroup.getString(), tGrpList);
    // }

    if (item == tTranspList) {
      int index = tTranspList.getSelectedIndex();
      if (index == tTranspList.size() - 1) return;

      String transport = tTranspList.getString(index);

      String jid = tJid.getString();
      StringBuffer jidBuf = new StringBuffer(jid);

      int at = jid.indexOf('@');
      if (at < 0) at = tJid.size();

      jidBuf.setLength(at);
      jidBuf.append('@');
      jidBuf.append(transport);
      tJid.setString(jidBuf.toString());
    }
    if (item == tJid) {
      String s1 = tJid.getString();
      int at = tJid.getString().indexOf('@');
      try {
        updateChoise(s1.substring(at + 1), tTranspList);
      } catch (Exception e) {
      }
    }
  }
Exemplo n.º 21
0
  private void processLocaleInfo() {
    if (_updateLocale) {
      _updateLocale = false;
      LanguagePreferences p = getPage().getLanguagePreferences();
      String appName = getPage().getApplicationName();
      StringBuffer key = new StringBuffer("HtmlCalendar.month.short.0");
      boolean shortOK = (LanguageResourceFinder.getResource(appName, key.toString(), p) != null);
      key.setLength(0);
      key.append("HtmlCalendar.month.long.0");
      boolean longOK = (LanguageResourceFinder.getResource(appName, key.toString(), p) != null);

      if (shortOK) {
        processLocaleArray(_monthShortNames, "HtmlCalendar.month.short.", appName, p, key);
        processLocaleArray(_dayShortNames, "HtmlCalendar.day.short.", appName, p, key);
      }

      if (longOK) {
        processLocaleArray(_monthLongNames, "HtmlCalendar.month.long.", appName, p, key);
        processLocaleArray(_dayLongNames, "HtmlCalendar.day.long.", appName, p, key);
      }
    }
  }
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
      throws SAXException {
    try {
      // we output the remaining text
      if (!counting) {
        writer.write(getText());
        accumulator.setLength(0);
      }
      if (!counting) {
        writer.write("<" + qName);

        int length = atts.getLength();

        // Process each attribute
        for (int i = 0; i < length; i++) {
          // Get names and values for each attribute
          String name = atts.getQName(i);
          String value = atts.getValue(i);

          if ((name != null) && (value != null)) {
            writer.write(" " + name + "=\"" + value + "\"");
          }
        }

        writer.write(">");
      }

      if (qName.equals("description")) {
        offset = 0;
        counting = true;
      } else if (qName.equals("patent-document")) {
        counting = false;
      }
    } catch (Exception e) {
      //		    e.printStackTrace();
      throw new GrobidException("An exception occured while running Grobid.", e);
    }
  }
Exemplo n.º 23
0
 private static String FilterID(String id) {
   if (id == null) {
     return null;
   } else {
     StringBuffer newID = new StringBuffer();
     int st = 0;
     for (int i = 0; i < id.length(); i++) {
       char ch = Character.toLowerCase(id.charAt(i));
       if (Character.isLetterOrDigit(ch)) {
         newID.append(ch);
         st = 1;
       } else if (st == 1) {
         newID.append("_");
         st = 0;
       } else {
         // ignore char
       }
     }
     while ((newID.length() > 0) && (newID.charAt(newID.length() - 1) == '_')) {
       newID.setLength(newID.length() - 1);
     }
     return newID.toString();
   }
 }
Exemplo n.º 24
0
 /** @return JSON representation of cart contents */
 public String toJson() {
   StringBuffer json = new StringBuffer();
   // Append the current system time to check for latest async calls
   json.append("{\"cart generated\":" + System.currentTimeMillis() + ",");
   json.append("\"total\":\"" + getCartTotal() + "\",");
   // Generate json array of json objects by iterating through the contents
   json.append("\"items\":[");
   for (Iterator<Item> I = contents.values().iterator(); I.hasNext(); ) {
     Item item = I.next();
     // int itemQuantity = contents.get(item).intValue();
     json.append("{");
     json.append("\"item code\":\"" + item.getCode() + "\",");
     json.append("\"name\":\"");
     json.append(item.getName()).append("\"},");
     // json.append("\"quantity\":");
     // json.append(itemQuantity).append("},");
   }
   // Remove , if it exists at the end of json for correcting the syntax
   if (json.toString().endsWith(",")) {
     json.setLength(json.length() - 1);
   }
   json.append("]}");
   return json.toString();
 }
Exemplo n.º 25
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);
  }
Exemplo n.º 26
0
  /**
   * 按条件查询多条数据
   *
   * @param conditions 查询条件
   * @param pageNo 页号
   * @param rowsPerPage 每页的行数
   * @return Collection
   * @throws Exception
   */
  public Collection findByConditions(String conditions, int pageNo, int rowsPerPage)
      throws Exception {
    StringBuffer buffer = new StringBuffer(200);
    // 拼SQL语句
    buffer.append("SELECT ");
    buffer.append("LineCode,");
    buffer.append("StatMonth,");
    buffer.append("PowerClass,");
    buffer.append("ElectricQuantity,");
    buffer.append("PointerQuantity,");
    buffer.append("SanXiaFee,");
    buffer.append("Surcharge,");
    buffer.append("SumFee,");
    buffer.append("ValidStatus,");
    buffer.append("Flag,");
    buffer.append("Remark,");
    buffer.append("TransLoss,");
    buffer.append("LineLoss,");
    buffer.append("UnPointerQuantity,");
    buffer.append("RateCode,");
    buffer.append("AdjustRate,");
    buffer.append("FarmUseScale,");
    buffer.append("FarmUsePrice,");
    buffer.append("FarmUseQuantity,");
    buffer.append("FarmUseFee,");
    buffer.append("ProductScale,");
    buffer.append("ProductPrice,");
    buffer.append("ProductQuantity,");
    buffer.append("ProductFee,");
    buffer.append("DenizenScale,");
    buffer.append("DenizenPrice,");
    buffer.append("DenizenQuantity,");
    buffer.append("DenizenFee,");
    buffer.append("UnDenizenScale,");
    buffer.append("UnDenizenPrice,");
    buffer.append("UnDenizenQuantity,");
    buffer.append("UnDenizenFee,");
    buffer.append("IndustryScale,");
    buffer.append("IndustryPrice,");
    buffer.append("IndustryQuantity,");
    buffer.append("IndustryFee,");
    buffer.append("BizScale,");
    buffer.append("BizPrice,");
    buffer.append("BizQuantity,");
    buffer.append("BizFee,");
    buffer.append("PowerRateFee,");
    buffer.append("UpCompany,");
    buffer.append("PowerFee,");
    buffer.append("InputDate,");
    buffer.append("Kv,");
    buffer.append("Wholesaletype,");
    buffer.append("WorkNum,");
    buffer.append("UnWorkNum,");
    buffer.append("OtherSurcharge,");
    buffer.append("DifferenceQuantity,");
    buffer.append("UnTransLoss,");
    buffer.append("UnLineLoss ");
    buffer.append("FROM LwWholeSaleSummary WHERE ");
    buffer.append(conditions);
    boolean supportPaging = false; // 数据库是否支持分页
    if (pageNo > 0) {
      // 对Oracle优化
      if (dbManager
          .getConnection()
          .getMetaData()
          .getDatabaseProductName()
          .equalsIgnoreCase("Oracle")) {
        buffer.insert(0, "SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM (");
        buffer.append(
            ") row_ WHERE rownum <= "
                + rowsPerPage * pageNo
                + ") WHERE rownum_ > "
                + rowsPerPage * (pageNo - 1));
        supportPaging = true;
      } else if (dbManager
          .getConnection()
          .getMetaData()
          .getDatabaseProductName()
          .equalsIgnoreCase("DB2")) {
        String sql = buffer.toString();
        buffer.setLength(0);
        buffer.append("select * from ( select rownumber() over(");
        int orderByIndex = sql.toLowerCase().indexOf("order by");
        if (orderByIndex > 0) {
          buffer.append(sql.substring(orderByIndex));
        }
        buffer.append(") as rownumber_,");
        buffer.append(sql.substring(6));
        buffer.append(" ) as temp_ where rownumber_");
        buffer.append(
            " between " + (rowsPerPage * (pageNo - 1) + 1) + " and " + rowsPerPage * pageNo);
        supportPaging = true;
      }
    }
    if (logger.isDebugEnabled()) {
      logger.debug(buffer.toString());
    }
    ResultSet resultSet = dbManager.executeQuery(buffer.toString());
    int count = 0;
    if (supportPaging == false && pageNo > 1) {
      dbManager.locate(resultSet, rowsPerPage * (pageNo - 1));
    }

    // 定义返回结果集合
    Collection collection = new ArrayList(rowsPerPage);
    LwWholeSaleSummaryDto lwWholeSaleSummaryDto = null;
    while (resultSet.next()) {
      if (supportPaging == false && pageNo > 0) {
        count++;
        if (count > rowsPerPage) {
          break;
        }
      }

      lwWholeSaleSummaryDto = new LwWholeSaleSummaryDto();
      lwWholeSaleSummaryDto.setLineCode(dbManager.getString(resultSet, "LineCode"));
      lwWholeSaleSummaryDto.setStatMonth(dbManager.getString(resultSet, "StatMonth"));
      lwWholeSaleSummaryDto.setPowerClass(dbManager.getString(resultSet, "PowerClass"));
      lwWholeSaleSummaryDto.setElectricQuantity(dbManager.getDouble(resultSet, "ElectricQuantity"));
      lwWholeSaleSummaryDto.setPointerQuantity(dbManager.getDouble(resultSet, "PointerQuantity"));
      lwWholeSaleSummaryDto.setSanXiaFee(dbManager.getDouble(resultSet, "SanXiaFee"));
      lwWholeSaleSummaryDto.setSurcharge(dbManager.getDouble(resultSet, "Surcharge"));
      lwWholeSaleSummaryDto.setSumFee(dbManager.getDouble(resultSet, "SumFee"));
      lwWholeSaleSummaryDto.setValidStatus(dbManager.getString(resultSet, "ValidStatus"));
      lwWholeSaleSummaryDto.setFlag(dbManager.getString(resultSet, "Flag"));
      lwWholeSaleSummaryDto.setRemark(dbManager.getString(resultSet, "Remark"));
      lwWholeSaleSummaryDto.setTransLoss(dbManager.getDouble(resultSet, "TransLoss"));
      lwWholeSaleSummaryDto.setLineLoss(dbManager.getDouble(resultSet, "LineLoss"));
      lwWholeSaleSummaryDto.setUnPointerQuantity(
          dbManager.getDouble(resultSet, "UnPointerQuantity"));
      lwWholeSaleSummaryDto.setRateCode(dbManager.getDouble(resultSet, "RateCode"));
      lwWholeSaleSummaryDto.setAdjustRate(dbManager.getDouble(resultSet, "AdjustRate"));
      lwWholeSaleSummaryDto.setFarmUseScale(dbManager.getDouble(resultSet, "FarmUseScale"));
      lwWholeSaleSummaryDto.setFarmUsePrice(dbManager.getDouble(resultSet, "FarmUsePrice"));
      lwWholeSaleSummaryDto.setFarmUseQuantity(dbManager.getDouble(resultSet, "FarmUseQuantity"));
      lwWholeSaleSummaryDto.setFarmUseFee(dbManager.getDouble(resultSet, "FarmUseFee"));
      lwWholeSaleSummaryDto.setProductScale(dbManager.getDouble(resultSet, "ProductScale"));
      lwWholeSaleSummaryDto.setProductPrice(dbManager.getDouble(resultSet, "ProductPrice"));
      lwWholeSaleSummaryDto.setProductQuantity(dbManager.getDouble(resultSet, "ProductQuantity"));
      lwWholeSaleSummaryDto.setProductFee(dbManager.getDouble(resultSet, "ProductFee"));
      lwWholeSaleSummaryDto.setDenizenScale(dbManager.getDouble(resultSet, "DenizenScale"));
      lwWholeSaleSummaryDto.setDenizenPrice(dbManager.getDouble(resultSet, "DenizenPrice"));
      lwWholeSaleSummaryDto.setDenizenQuantity(dbManager.getDouble(resultSet, "DenizenQuantity"));
      lwWholeSaleSummaryDto.setDenizenFee(dbManager.getDouble(resultSet, "DenizenFee"));
      lwWholeSaleSummaryDto.setUnDenizenScale(dbManager.getDouble(resultSet, "UnDenizenScale"));
      lwWholeSaleSummaryDto.setUnDenizenPrice(dbManager.getDouble(resultSet, "UnDenizenPrice"));
      lwWholeSaleSummaryDto.setUnDenizenQuantity(
          dbManager.getDouble(resultSet, "UnDenizenQuantity"));
      lwWholeSaleSummaryDto.setUnDenizenFee(dbManager.getDouble(resultSet, "UnDenizenFee"));
      lwWholeSaleSummaryDto.setIndustryScale(dbManager.getDouble(resultSet, "IndustryScale"));
      lwWholeSaleSummaryDto.setIndustryPrice(dbManager.getDouble(resultSet, "IndustryPrice"));
      lwWholeSaleSummaryDto.setIndustryQuantity(dbManager.getDouble(resultSet, "IndustryQuantity"));
      lwWholeSaleSummaryDto.setIndustryFee(dbManager.getDouble(resultSet, "IndustryFee"));
      lwWholeSaleSummaryDto.setBizScale(dbManager.getDouble(resultSet, "BizScale"));
      lwWholeSaleSummaryDto.setBizPrice(dbManager.getDouble(resultSet, "BizPrice"));
      lwWholeSaleSummaryDto.setBizQuantity(dbManager.getDouble(resultSet, "BizQuantity"));
      lwWholeSaleSummaryDto.setBizFee(dbManager.getDouble(resultSet, "BizFee"));
      lwWholeSaleSummaryDto.setPowerRateFee(dbManager.getDouble(resultSet, "PowerRateFee"));
      lwWholeSaleSummaryDto.setUpCompany(dbManager.getString(resultSet, "UpCompany"));
      lwWholeSaleSummaryDto.setPowerFee(dbManager.getDouble(resultSet, "PowerFee"));
      lwWholeSaleSummaryDto.setInputDate(dbManager.getString(resultSet, "InputDate"));
      lwWholeSaleSummaryDto.setKv(dbManager.getString(resultSet, "Kv"));
      lwWholeSaleSummaryDto.setWholesaletype(dbManager.getString(resultSet, "Wholesaletype"));
      lwWholeSaleSummaryDto.setWorkNum(dbManager.getDouble(resultSet, "WorkNum"));
      lwWholeSaleSummaryDto.setUnWorkNum(dbManager.getDouble(resultSet, "UnWorkNum"));
      lwWholeSaleSummaryDto.setOtherSurcharge(dbManager.getDouble(resultSet, "OtherSurcharge"));
      lwWholeSaleSummaryDto.setDifferenceQuantity(
          dbManager.getString(resultSet, "DifferenceQuantity"));
      lwWholeSaleSummaryDto.setUnTransLoss(dbManager.getDouble(resultSet, "UnTransLoss"));
      lwWholeSaleSummaryDto.setUnLineLoss(dbManager.getDouble(resultSet, "UnLineLoss"));
      collection.add(lwWholeSaleSummaryDto);
    }
    resultSet.close();
    return collection;
  }
Exemplo n.º 27
0
  /** Performs the subsequent ReTrace operations. */
  public void execute() throws IOException {
    // Read the mapping file.
    MappingReader mappingReader = new MappingReader(mappingFile);
    mappingReader.pump(this);

    StringBuffer expressionBuffer = new StringBuffer(regularExpression.length() + 32);
    char[] expressionTypes = new char[32];
    int expressionTypeCount = 0;
    int index = 0;
    while (true) {
      int nextIndex = regularExpression.indexOf('%', index);
      if (nextIndex < 0
          || nextIndex == regularExpression.length() - 1
          || expressionTypeCount == expressionTypes.length) {
        break;
      }

      expressionBuffer.append(regularExpression.substring(index, nextIndex));
      expressionBuffer.append('(');

      char expressionType = regularExpression.charAt(nextIndex + 1);
      switch (expressionType) {
        case 'c':
          expressionBuffer.append(REGEX_CLASS);
          break;

        case 'C':
          expressionBuffer.append(REGEX_CLASS_SLASH);
          break;

        case 'l':
          expressionBuffer.append(REGEX_LINE_NUMBER);
          break;

        case 't':
          expressionBuffer.append(REGEX_TYPE);
          break;

        case 'f':
          expressionBuffer.append(REGEX_MEMBER);
          break;

        case 'm':
          expressionBuffer.append(REGEX_MEMBER);
          break;

        case 'a':
          expressionBuffer.append(REGEX_ARGUMENTS);
          break;
      }

      expressionBuffer.append(')');

      expressionTypes[expressionTypeCount++] = expressionType;

      index = nextIndex + 2;
    }

    expressionBuffer.append(regularExpression.substring(index));

    Pattern pattern = Pattern.compile(expressionBuffer.toString());

    // Read the stack trace file.
    LineNumberReader reader =
        new LineNumberReader(
            stackTraceFile == null
                ? (Reader) new InputStreamReader(System.in)
                : (Reader) new BufferedReader(new FileReader(stackTraceFile)));

    try {
      StringBuffer outLine = new StringBuffer(256);
      List extraOutLines = new ArrayList();

      String className = null;

      // Read the line in the stack trace.
      while (true) {
        String line = reader.readLine();
        if (line == null) {
          break;
        }

        Matcher matcher = pattern.matcher(line);

        if (matcher.matches()) {
          int lineNumber = 0;
          String type = null;
          String arguments = null;

          // Figure out a class name, line number, type, and
          // arguments beforehand.
          for (int expressionTypeIndex = 0;
              expressionTypeIndex < expressionTypeCount;
              expressionTypeIndex++) {
            int startIndex = matcher.start(expressionTypeIndex + 1);
            if (startIndex >= 0) {
              String match = matcher.group(expressionTypeIndex + 1);

              char expressionType = expressionTypes[expressionTypeIndex];
              switch (expressionType) {
                case 'c':
                  className = originalClassName(match);
                  break;

                case 'C':
                  className = originalClassName(ClassUtil.externalClassName(match));
                  break;

                case 'l':
                  lineNumber = Integer.parseInt(match);
                  break;

                case 't':
                  type = originalType(match);
                  break;

                case 'a':
                  arguments = originalArguments(match);
                  break;
              }
            }
          }

          // Actually construct the output line.
          int lineIndex = 0;

          outLine.setLength(0);
          extraOutLines.clear();

          for (int expressionTypeIndex = 0;
              expressionTypeIndex < expressionTypeCount;
              expressionTypeIndex++) {
            int startIndex = matcher.start(expressionTypeIndex + 1);
            if (startIndex >= 0) {
              int endIndex = matcher.end(expressionTypeIndex + 1);
              String match = matcher.group(expressionTypeIndex + 1);

              // Copy a literal piece of input line.
              outLine.append(line.substring(lineIndex, startIndex));

              char expressionType = expressionTypes[expressionTypeIndex];
              switch (expressionType) {
                case 'c':
                  className = originalClassName(match);
                  outLine.append(className);
                  break;

                case 'C':
                  className = originalClassName(ClassUtil.externalClassName(match));
                  outLine.append(ClassUtil.internalClassName(className));
                  break;

                case 'l':
                  lineNumber = Integer.parseInt(match);
                  outLine.append(match);
                  break;

                case 't':
                  type = originalType(match);
                  outLine.append(type);
                  break;

                case 'f':
                  originalFieldName(className, match, type, outLine, extraOutLines);
                  break;

                case 'm':
                  originalMethodName(
                      className, match, lineNumber, type, arguments, outLine, extraOutLines);
                  break;

                case 'a':
                  arguments = originalArguments(match);
                  outLine.append(arguments);
                  break;
              }

              // Skip the original element whose processed version
              // has just been appended.
              lineIndex = endIndex;
            }
          }

          // Copy the last literal piece of input line.
          outLine.append(line.substring(lineIndex));

          // Print out the main line.
          System.out.println(outLine);

          // Print out any additional lines.
          for (int extraLineIndex = 0; extraLineIndex < extraOutLines.size(); extraLineIndex++) {
            System.out.println(extraOutLines.get(extraLineIndex));
          }
        } else {
          // Print out the original line.
          System.out.println(line);
        }
      }
    } catch (IOException ex) {
      throw new IOException("Can't read stack trace (" + ex.getMessage() + ")");
    } finally {
      if (stackTraceFile != null) {
        try {
          reader.close();
        } catch (IOException ex) {
          // This shouldn't happen.
        }
      }
    }
  }
Exemplo n.º 28
0
    private Object parse_substring() throws InvalidSyntaxException {
      StringBuffer sb = new StringBuffer(filterChars.length - pos);

      List operands = new ArrayList(10);

      parseloop:
      while (true) {
        char c = filterChars[pos];

        switch (c) {
          case ')':
            {
              if (sb.length() > 0) {
                operands.add(sb.toString());
              }

              break parseloop;
            }

          case '(':
            {
              throw new InvalidSyntaxException(
                  "Invalid value: " + filterstring.substring(pos), filterstring);
            }

          case '*':
            {
              if (sb.length() > 0) {
                operands.add(sb.toString());
              }

              sb.setLength(0);

              operands.add(null);
              pos++;

              break;
            }

          case '\\':
            {
              pos++;
              c = filterChars[pos];
              /* fall through into default */
            }

          default:
            {
              sb.append(c);
              pos++;
              break;
            }
        }
      }

      int size = operands.size();

      if (size == 0) {
        return "";
      }

      if (size == 1) {
        Object single = operands.get(0);

        if (single != null) {
          return single;
        }
      }

      return operands.toArray(new String[size]);
    }
Exemplo n.º 29
0
  /**
   * Tokenizes a command string into a list.
   *
   * @throws Exception if the command cannot be tokenized
   */
  protected static LinkedList _tokenizeCommand(String cmd) throws Exception {

    LinkedList tokens = new LinkedList();
    int startIndex = 0;
    int dQuoteAt = cmd.indexOf('"');
    int sQuoteAt = cmd.indexOf('\'');

    if (dQuoteAt == -1 && sQuoteAt == -1) {
      StringTokenizer st = new StringTokenizer(cmd.trim());
      while (st.hasMoreTokens()) {
        tokens.add(st.nextToken());
      }
      return tokens;
    }

    char[] chArray = cmd.trim().toCharArray();

    int endIndex = 0;
    boolean inQuotes = false;
    char c = 0;
    char lastc = 0;
    char lastqc = 0;
    StringBuffer sb = new StringBuffer(80);

    while (endIndex < chArray.length) {
      c = chArray[endIndex];
      if (!Character.isWhitespace(c)) {
        if (c == '"' || c == '\'') {
          if (inQuotes && lastc != '\\' && lastqc == c) {
            tokens.add(sb.toString());
            inQuotes = false;
            sb.setLength(0);
          } else if (!inQuotes) {
            inQuotes = true;
            lastqc = c;
          } else {
            sb.append(c);
          }
        } else if (c == '\\') {
          if (lastc == '\\') sb.append(c);
        } else {
          sb.append(c);
        }
      } else {
        if (inQuotes) {
          sb.append(c);
        } else {
          if (sb.length() > 0) {
            tokens.add(sb.toString());
            sb.setLength(0);
          }
        }
      }
      lastc = c;
      ++endIndex;
    }

    if (inQuotes) {
      throw new Exception(
          WDExUtil.formatMessage(WDExConstants.UNTERMINATED_STRING, WDUtil.toArray(cmd)));
    }
    return tokens;
  }
Exemplo n.º 30
0
  public static void main(String[] args) {
    // String inFile = "C:/My Documents/HMM/Chunk/wsj_15_18_train.log";
    String inFile = chunkDir + "wsj_15_18_train.log";
    // String featureFile = "C:/My Documents/HMM/Chunk/chunk features.txt";
    String featureFile = chunkDir + "chunk features.txt";

    try {
      BufferedReader reader = new BufferedReader(new FileReader(inFile));
      PrintStream writer = new PrintStream(new FileOutputStream(featureFile));
      String line;
      String prevToken = "";
      String prevPOS = "";
      String prevTag = "";
      String currentToken = "";
      String currentPOS = "";
      String currentTag = "";
      String nextToken = "";
      String nextPOS = "";
      String nextTag = "";
      String followingToken = "";
      String followingPOS = "";
      String followingTag = "";
      StringBuffer features = new StringBuffer(200);

      boolean inGroup = false;
      boolean firstToken = true;
      while ((line = reader.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(line);
        int count = st.countTokens();
        if (count == 0) {
          // blank line -- end of sentence
          followingToken = "";
          followingPOS = "";
          followingTag = "";
        } else if (count >= 3) {
          // token
          followingToken = st.nextToken();
          followingPOS = st.nextToken();
          followingTag = st.nextToken();
        } else {
          System.out.println("Error:  invalid input line: " + line);
        }
        if (currentToken != "") {
          features.setLength(0);
          features.append("prevPOS=" + prevPOS + " ");
          features.append("currPOS=" + currentPOS + " ");
          features.append("nextPOS=" + nextPOS + " ");
          if (nextToken == "") features.append("POS012=" + currentPOS + ":: ");
          else features.append("POS012=" + currentPOS + ":" + nextPOS + ":" + followingPOS + " ");
          features.append("prevTag=" + prevTag + " ");
          features.append("currWord=" + currentToken + " ");
          features.append("W-1W0=" + prevToken + ":" + currentToken + " ");
          features.append("W0W1=" + currentToken + ":" + nextToken + " ");
          features.append(currentTag);
          writer.println(features);
        }
        prevToken = currentToken;
        prevPOS = currentPOS;
        prevTag = currentTag;
        currentToken = nextToken;
        currentPOS = nextPOS;
        currentTag = nextTag;
        nextToken = followingToken;
        nextPOS = followingPOS;
        nextTag = followingTag;
      }
    } catch (IOException e) {
      System.out.println(e);
    }
  }