2016-05-29 61 views
1

我正在處理一個小項目,graphipedia(用於導入wikipedia轉儲),並且我正在使用stax分析器來導入wikiquote轉儲。StringBuilder - 追加方法在某個點停止工作

在該過程的某個時刻,我已經閱讀了一些文本字符(在< text>和</text>之間),並且該代碼對StringBuilder變量執行追加方法,但由於某些原因,追加不會向StringBuilder變量添加一個字符。

下面是代碼:

package org.graphipedia.dataimport; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.LinkedList; 
import java.util.List; 

import javax.xml.stream.XMLInputFactory; 
import javax.xml.stream.XMLStreamException; 
import javax.xml.stream.XMLStreamReader; 
import javax.xml.stream.events.XMLEvent; 

import org.codehaus.stax2.XMLInputFactory2; 

public abstract class SimpleStaxParser { 

    private static final String STDIN_FILENAME = "-"; 
    private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory2.newInstance(); 

    private final List<String> interestingElements; 

    public SimpleStaxParser(List<String> interestingElements) { 
     this.interestingElements = interestingElements; 
    } 

    protected abstract void handleElement(String element, String value); 

    public void parse(String fileName) throws IOException, XMLStreamException { 
     if (STDIN_FILENAME.equals(fileName)) { 
      parse(System.in); 
     } else { 
      parse(new FileInputStream(fileName)); 
     } 
    } 

    private void parse(InputStream inputStream) throws IOException, XMLStreamException { 
     XMLStreamReader reader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream, "UTF-8"); 
     try { 
      parseElements(reader); 
     } finally { 
      reader.close(); 
      inputStream.close(); 
     } 
    } 

    private void parseElements(XMLStreamReader reader) throws XMLStreamException { 
     LinkedList<String> elementStack = new LinkedList<String>(); 
     StringBuilder textBuffer = new StringBuilder(); 

     while (reader.hasNext()) { 
      switch (reader.next()) { 
      case XMLEvent.START_ELEMENT: 
       elementStack.push(reader.getName().getLocalPart()); 
       textBuffer.setLength(0); 
       break; 
      case XMLEvent.END_ELEMENT: 
       String element = elementStack.pop(); 
       if (isInteresting(element)) { 
        handleElement(element, textBuffer.toString().trim()); 
       } 
       break; 
      case XMLEvent.CHARACTERS: 
       if (isInteresting(elementStack.peek())) { 
        textBuffer.append(reader.getText()); 
       } 
       break; 
      } 
     } 
    } 

    private boolean isInteresting(String element) { 
     return interestingElements.contains(element); 
    } 

} 

那是給我一些麻煩的線,這是一個:

textBuffer.append(reader.getText()); 

在該行,reader.getText()返回如下:

lo que pasó, pasó por una razón... 

'''Neo''': ¿Y qué razón es esa? 

'''Smith''': Yo lo maté, señor Anderson, lo vi morir... Con cierta satisfacción, debo decir. Y luego algo pasó. Algo que sabía que era imposible, pero aún así pasó: usted me destruyó, señor Anderson... Después, cuando supe las reglas, entendí lo que debí haber hecho, pero no lo hice. No podía, fui obligado a quedarme, fui obligado a desobedecer... Y ahora aquí estoy por su culpa, señor Anderson. Por su culpa, ya no soy un agente de este sistema. Por su culpa cambié, me desconecté. Un hombre libre por decir algo, como usted, aparentemente libre. 

'''Neo''': ¡Felicidades! 

'''Smith''': Gracias... Pero, como sabrá, las apariencias engañan, lo cual me regresa a la razón por la que estoy aquí. No estamos aquí por ser libres. Estamos aquí por no ser libres. No hay razón de escapatoria, ni propósitos de negación. Porque, como sabemos, sin propósitos, no existiríamos... 

'''Clones''': Propósito fue lo que nos creó... propósito lo que nos conecta, propósito lo que nos impulsa, lo que nos guía, lo que nos controla, es el propósito lo que define, propósito lo que nos une. 

'''Smith''': Estamos aquí por culpa suya, señor Anderson. Estamos aquí para quitarle lo que trató de quitarnos a nosotros ¡Propósito! 

[[Categoría:Películas]] 

[[en:The Matrix (franchise)]] 
[[sl:Matrica]] 

在執行append方法之前,textBuffer變量的計數值爲30643,一個capa城市的64254,並添加文本的長度爲1352

其數據解析器工作可以在看到:https://es.wikiquote.org/w/index.php?title=The_Matrix&action=edit(太大了,在這裏張貼)


的步驟重現此問題: 獲取dump,下載graphipedia,解壓縮並使用maven(mvn package)構建它,並從Eclipse或類似ide運行ExtractLinks,以便正確調試代碼。

+0

對不起,向我解釋了我自己刪除了我的評論, – Turo

+0

你能發佈樣本數據嗎? – Turo

+0

我爲示例數據添加了一個鏈接。將它插入我的問題太大了。 – chomp

回答

0

那是一個很大的錯誤我,因爲我不知道Eclipse在調試模式下如何處理String變量。 Append工作得很好,但字符串太大而無法看到它,並且在這種情況下,Eclipse顯示字符串,直到某個點後,顯示「...」。

我選擇了textBuffer變量的值,以便改變它,試圖查看實際值,並且該值正好在那裏,即從第一個字符到最後一個文件的完整字符串。

非常感謝@turo幫助我的巨大努力。

+0

有關更多信息,請參閱http://stackoverflow.com/questions/2873949/viewing-complete-strings-while-debugging-in-eclipse – chomp

0

發現的星座,但無法重現:

改變一下代碼:

 case XMLEvent.CHARACTERS: 
      if (isInteresting(elementStack.peek())) { 
       int sizeBefore = textBuffer.length(); 
       String text = reader.getText(); 
       int textSize = text.length(); 
       textBuffer.append(text); 
       int sizeAfter = textBuffer.length(); 
       assert sizeBefore + textSize == sizeAfter : "Error occured " + sizeBefore + " " + textSize + " :" 
         + text; 
       if (textSize == 1352) { 
        System.out.println(
          "Size before " + sizeBefore + " textSize " + textSize + " sizeafter " + sizeAfter); 
       } 
      } 
      break;     

沒有得到Asse田,但輸出

Parsing pages and extracting links... 
Size before 30643 textSize 1352 sizeafter 31995 
...........Size before 0 textSize 1352 sizeafter 1352 
...Size before 83963 textSize 1352 sizeafter 85315 
. 
15309 pages parsed in 0 seconds. 
+0

這是從我身邊@turo發生的新手錯誤,它引發了這個問題。對不起,非常感謝您的幫助! – chomp