Thread: [Jreepad-CVS] jreepad/src/jreepad JreepadViewer.java, 1.45, 1.46 JreepadNode.java, 1.21, 1.22 find.
Brought to you by:
danstowell
From: PeWu <pe...@us...> - 2007-01-20 13:01:24
|
Update of /cvsroot/jreepad/jreepad/src/jreepad In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv6732/src/jreepad Modified Files: JreepadViewer.java JreepadNode.java find.java Log Message: refactoring: moved XML and HJT readers from JreepadNode to separate classes Index: find.java =================================================================== RCS file: /cvsroot/jreepad/jreepad/src/jreepad/find.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** find.java 18 Jan 2007 09:37:27 -0000 1.5 --- find.java 20 Jan 2007 13:01:19 -0000 1.6 *************** *** 36,39 **** --- 36,41 ---- import java.util.Vector; + import jreepad.io.AutoDetectReader; + import jreepad.io.JreepadReader; import jreepad.io.JreepadWriter; import jreepad.io.TreepadWriter; *************** *** 232,236 **** try { ! root = new JreepadNode(new InputStreamReader(new FileInputStream(userFile), encoding), false); } catch(IOException err) --- 234,240 ---- try { ! InputStream in = new FileInputStream(userFile); ! JreepadReader reader = new AutoDetectReader(encoding, false); ! root = reader.read(in); } catch(IOException err) Index: JreepadViewer.java =================================================================== RCS file: /cvsroot/jreepad/jreepad/src/jreepad/JreepadViewer.java,v retrieving revision 1.45 retrieving revision 1.46 diff -C2 -d -r1.45 -r1.46 *** JreepadViewer.java 18 Jan 2007 09:37:27 -0000 1.45 --- JreepadViewer.java 20 Jan 2007 13:01:19 -0000 1.46 *************** *** 47,50 **** --- 47,52 ---- import java.lang.reflect.*; + import jreepad.io.AutoDetectReader; + import jreepad.io.JreepadReader; import jreepad.io.JreepadWriter; import jreepad.io.TreepadWriter; *************** *** 357,361 **** getPrefs().openLocation = firstTimeFile; content.remove(theJreepad); ! theJreepad = new JreepadView(new JreepadNode(new InputStreamReader(new FileInputStream(getPrefs().openLocation), getPrefs().getEncoding()), getPrefs().autoDetectHtmlArticles)); getPrefs().saveLocation = getPrefs().exportLocation = getPrefs().importLocation = getPrefs().openLocation; content.add(theJreepad); --- 359,367 ---- getPrefs().openLocation = firstTimeFile; content.remove(theJreepad); ! ! InputStream in = new FileInputStream(getPrefs().openLocation); ! JreepadReader reader = new AutoDetectReader(getPrefs().getEncoding(), getPrefs().autoDetectHtmlArticles); ! theJreepad = new JreepadView(reader.read(in)); ! getPrefs().saveLocation = getPrefs().exportLocation = getPrefs().importLocation = getPrefs().openLocation; content.add(theJreepad); *************** *** 1622,1626 **** getPrefs().openLocation = f; content.remove(theJreepad); ! theJreepad = new JreepadView(new JreepadNode(new InputStreamReader(new FileInputStream(getPrefs().openLocation), getPrefs().getEncoding()), getPrefs().autoDetectHtmlArticles)); getPrefs().saveLocation = getPrefs().openLocation; content.add(theJreepad); --- 1628,1636 ---- getPrefs().openLocation = f; content.remove(theJreepad); ! ! InputStream in = new FileInputStream(getPrefs().openLocation); ! JreepadReader reader = new AutoDetectReader(getPrefs().getEncoding(), getPrefs().autoDetectHtmlArticles); ! theJreepad = new JreepadView(reader.read(in)); ! getPrefs().saveLocation = getPrefs().openLocation; content.add(theJreepad); *************** *** 1825,1829 **** { case FILE_FORMAT_HJT: ! theJreepad.addChild(new JreepadNode(new InputStreamReader(new FileInputStream(getPrefs().importLocation), getPrefs().getEncoding()), getPrefs().autoDetectHtmlArticles)); break; case FILE_FORMAT_TEXT: --- 1835,1841 ---- { case FILE_FORMAT_HJT: ! InputStream in = new FileInputStream(getPrefs().importLocation); ! JreepadReader reader = new AutoDetectReader(getPrefs().getEncoding(), getPrefs().autoDetectHtmlArticles); ! theJreepad.addChild(reader.read(in)); break; case FILE_FORMAT_TEXT: Index: JreepadNode.java =================================================================== RCS file: /cvsroot/jreepad/jreepad/src/jreepad/JreepadNode.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** JreepadNode.java 18 Jan 2007 09:37:27 -0000 1.21 --- JreepadNode.java 20 Jan 2007 13:01:19 -0000 1.22 *************** *** 18,22 **** */ - package jreepad; --- 18,21 ---- *************** *** 29,33 **** import java.util.Comparator; import java.util.Enumeration; - import java.util.Stack; import javax.swing.tree.DefaultMutableTreeNode; --- 28,31 ---- *************** *** 73,409 **** undoMgr = new UndoManager(); } - public JreepadNode(InputStreamReader treeInputStream, boolean autoDetectHtmlArticles) throws IOException - { - undoMgr = new UndoManager(); - constructFromInputStream(treeInputStream, autoDetectHtmlArticles); - } - private void constructFromInputStream(InputStreamReader treeInputStream, boolean autoDetectHtmlArticles) throws IOException - { - int lineNum = 2; - BufferedReader bReader = new BufferedReader(treeInputStream); - - Stack nodeStack = new Stack(); - nodeStack.push(this); - - String currentLine = bReader.readLine(); // Read the first line, check for treepadness - - if(currentLine.startsWith("<?xml version=\"1.0\"")) - { - // Try and find out what character encoding to use - int encPos = currentLine.indexOf("encoding="); - String xmlEncoding; - if(encPos==-1) - xmlEncoding = ""; // getPrefs().getEncoding(); - else - { - xmlEncoding = currentLine.substring(encPos+10); - encPos = xmlEncoding.indexOf("\""); - if(encPos==-1) - encPos = xmlEncoding.indexOf("'"); - if(encPos==-1) - xmlEncoding = ""; // getPrefs().getEncoding(); - else - xmlEncoding = xmlEncoding.substring(0, encPos); - //System.out.println("Start of XML loading: decided on the following character encoding: " + xmlEncoding); - //System.out.println(" ...but unfortunately can't do anything about that. Using encoding " + treeInputStream.getEncoding()); - constructFromXmlInputStream(bReader); - } - } - else if((currentLine.toLowerCase().startsWith("<treepad") && currentLine.endsWith(">")) ) - { - constructFromHjtInputStream(treeInputStream, autoDetectHtmlArticles, - 1, bReader, lineNum, nodeStack); - } - else if((currentLine.toLowerCase().startsWith("<hj-treepad") && currentLine.endsWith(">")) ) - constructFromHjtInputStream(treeInputStream, autoDetectHtmlArticles, - 2, bReader, lineNum, nodeStack); - else - { - System.out.println("First line of file does not indicate a recognised format:\n" + currentLine + "\n"); - throw new IOException("First line of file does not indicate a recognised format:\n\n" + currentLine); - } - - - - } - - private void constructFromXmlInputStream(BufferedReader bReader) throws IOException - { - String currentLine; - String currentXmlContent = ""; - int nodeTagOffset = 0; - - // Spool until we're at the very first node - while((currentLine = bReader.readLine())!=null && (nodeTagOffset = currentXmlContent.indexOf("<node"))==-1 - && currentXmlContent.indexOf('>', nodeTagOffset)==-1) - currentXmlContent += (currentLine + "\n"); - - if(currentLine != null) - currentXmlContent += (currentLine + "\n"); - - //System.out.println("XMLparse: I've spooled to the first node and content is now: " + currentXmlContent); - - // So currentXmlContent now contains all of the opening tag, including its attributes etc - // Strip off anything BEFORE the node opening - currentXmlContent = currentXmlContent.substring(nodeTagOffset); - - //System.out.println("XMLparse: I've stripped anything before the first node and content is now: " + currentXmlContent); - - recursiveCreateFromXmlStream(bReader, currentXmlContent, 0); - } - - // This function should return any XML string content that remains unprocessed - private String recursiveCreateFromXmlStream(BufferedReader bReader, String currentXmlContent, int depth) throws IOException - { - - // System.out.println("XMLparse recursive: depth "+depth); - - // String currentXmlContent should BEGIN with the <node> tag. This is assumed, and if not true may cause problems! - String currentLine; - int titleOffset, typeOffset, startTagOffset, endTagOffset; - String title, typeString, content; - JreepadNode babyNode; - removeAllChildren(); - this.content = ""; - - // Extract the attributes - titleOffset = currentXmlContent.indexOf("title="); - typeOffset = currentXmlContent.indexOf("type="); - if(titleOffset!=-1) - title = currentXmlContent.substring(titleOffset+7, currentXmlContent.indexOf('"', titleOffset+7)); - else - title = "<Untitled node>"; - if(typeOffset!=-1) - typeString = currentXmlContent.substring(typeOffset+6, currentXmlContent.indexOf('"', typeOffset+6)); - else - typeString = "text/plain"; - - if(typeString.equals("text/csv")) - this.articleMode = ARTICLEMODE_CSV; - else if(typeString.equals("text/html")) - this.articleMode = ARTICLEMODE_HTML; - else if(typeString.equals("text/textile")) - this.articleMode = ARTICLEMODE_TEXTILEHTML; - else if(typeString.equals("application/x-jreepad-softlink")) - this.articleMode = ARTICLEMODE_SOFTLINK; - else - this.articleMode = ARTICLEMODE_ORDINARY; - - this.title = xmlUnescapeChars(title); - - // OK, so we've interpreted the attributes etc. Now we need to trim the opening tag away - currentXmlContent = currentXmlContent.substring(currentXmlContent.indexOf('>')+1); - - //System.out.println("XMLparse: I've stripped off the <node> tag and content is now: " + currentXmlContent); - - boolean readingContent = true; // Once the baby nodes come in, we're not interested in adding any more to the content - while((currentLine = bReader.readLine())!=null) - { - //System.out.println("XMLparserecursive: Here's a line: " + currentLine); - currentLine += "\n"; // We want to keep the newlines, but the BufferedReader doesn't give us them - - // We're reading CONTENT into the current node. - currentXmlContent += currentLine; - - // System.out.println("\n\nThe content that I'm currently trying to process is:\n"+currentXmlContent); - - // Look out for <node which tells us we're starting a child - startTagOffset = currentXmlContent.indexOf("<node"); - // Look out for </node> which tells us we're finishing this node and returning to the parent - endTagOffset = currentXmlContent.indexOf("</node>"); - - while(!(startTagOffset==-1 || endTagOffset==-1)) - { - if(startTagOffset==-1 || endTagOffset<startTagOffset) - { - // Process the nearest end tag - if(readingContent) - this.content += xmlUnescapeChars(currentXmlContent.substring(0, endTagOffset)); - String returnFromBaby = currentXmlContent.substring(endTagOffset+7); - //System.out.println("\n\nBaby intends to return:"+returnFromBaby); - return returnFromBaby; - } - else - { - if(readingContent) - this.content += xmlUnescapeChars(currentXmlContent.substring(0, startTagOffset)); - - // Having found a child node, we want to STOP adding anything to the current node's content (e.g. newlines...) - readingContent = false; - - // Process the nearest start tag - babyNode = new JreepadNode(); - //System.out.println("\n\nJust before passing to baby: content is:\n"+currentXmlContent); - currentXmlContent = babyNode.recursiveCreateFromXmlStream(bReader, - currentXmlContent.substring(startTagOffset), depth+1); - //System.out.println("\n\nJust after passing to baby: content is:\n"+currentXmlContent); - add(babyNode); - } - - startTagOffset = currentXmlContent.indexOf("<node"); - endTagOffset = currentXmlContent.indexOf("</node>"); - } - - } // End while - - - // Just make sure we haven't wasted any content... - endTagOffset = currentXmlContent.indexOf('<'); - if(readingContent && (endTagOffset != -1)) - this.content += xmlUnescapeChars(currentXmlContent.substring(0, endTagOffset)); - // System.out.println("THE MAIN WHILE LOOP HAS ENDED. SPARE CONTENT:\n" + currentXmlContent); - return ""; - } - - - private void constructFromHjtInputStream(InputStreamReader treeInputStream, boolean autoDetectHtmlArticles, - int hjtFileFormat, BufferedReader bReader, int lineNum, Stack nodeStack) throws IOException - { - int depthMarker; - JreepadNode babyNode; - String dtLine, nodeLine, titleLine, depthLine; - StringBuffer currentContent; - String currentLine; // = bReader.readLine(); // Read the first line, check for treepadness - dtLine = "dt=text"; - - while( (hjtFileFormat == 2 || (dtLine = bReader.readLine())!=null) - && (nodeLine = bReader.readLine())!=null && - (titleLine = bReader.readLine())!=null && (depthLine = bReader.readLine())!=null) - { - // Read "dt=text" [or error] - NB THE OLDER FORMAT DOESN'T INCLUDE THIS LINE SO WE SKIP IT - if(dtLine.equals("") && nodeLine.startsWith("<bmarks>")) - throw new IOException("This is not a Treepad-Lite-compatible file!\n\nFiles created in more advanced versions of Treepad\ncontain features that are not available in Jreepad."); - - if(hjtFileFormat != 2) - if(! (dtLine.toLowerCase().startsWith("dt=text"))) - throw new IOException("Unrecognised node dt format at line " + lineNum + ": " + dtLine); - // Read "<node>" [or error] - if(! (nodeLine.toLowerCase().startsWith("<node>"))) - throw new IOException("Unrecognised node format at line " + (lineNum+1) + ": " + nodeLine); - - lineNum += 4; - - // Read THE CONTENT! [loop until we find "<end node> 5P9i0s8y19Z"] - currentContent = new StringBuffer(); - while((currentLine = bReader.readLine())!=null && !currentLine.equals("<end node> 5P9i0s8y19Z")) - { - currentContent.append(currentLine + "\n"); - lineNum++; - } - - // Now, having established the content and the title and the depth, we'll create the child - babyNode = new JreepadNode(titleLine, currentContent.substring(0, Math.max(currentContent.length()-1,0))); - // babyNode = new JreepadNode(titleLine, currentContent.substring(0, Math.max(currentContent.length()-2,0)), - // (JreepadNode)(nodeStack.peek())); - - // Turn it into a HTML-mode node if it matches "<html> ... </html>" - String compareContent = babyNode.getContent().toLowerCase().trim(); - int newArticleMode = (autoDetectHtmlArticles && compareContent.startsWith("<html>") && compareContent.endsWith("</html>")) - ? ARTICLEMODE_HTML - : ARTICLEMODE_ORDINARY; - - if(depthLine.equals("0")) - { - this.title = titleLine; - this.content = currentContent.substring(0, Math.max(currentContent.length()-1,0)); - this.articleMode = newArticleMode; - } - else - { - babyNode.setArticleMode(newArticleMode); - depthMarker = Integer.parseInt(depthLine); - while(nodeStack.size()>depthMarker) - nodeStack.pop(); - - ((JreepadNode)(nodeStack.peek())).add(babyNode); - nodeStack.push(babyNode); - } - } - - } // End of constructor from HJT FileInputStream - - /* - private void OLDVERSIONconstructFromInputStream(InputStreamReader treeInputStream, boolean autoDetectHtmlArticles) throws IOException - { - int lineNum = 2; - int depthMarker; - BufferedReader bReader = new BufferedReader(treeInputStream); - JreepadNode babyNode; - children = new Vector(); - - Stack nodeStack = new Stack(); - nodeStack.push(this); - - int hjtFileFormat = -1; - - String dtLine, nodeLine, titleLine, depthLine; - StringBuffer currentContent; - String currentLine = bReader.readLine(); // Read the first line, check for treepadness - if((currentLine.toLowerCase().startsWith("<treepad") && currentLine.endsWith(">")) ) - hjtFileFormat = 1; - else if((currentLine.toLowerCase().startsWith("<hj-treepad") && currentLine.endsWith(">")) ) - hjtFileFormat = 2; - else - throw new IOException("\"<Treepad>\" tag not found at beginning of file!\n(This can be caused by having the wrong character set specified.)"); - - dtLine = "dt=text"; - - while( (hjtFileFormat == 2 || (dtLine = bReader.readLine())!=null) - && (nodeLine = bReader.readLine())!=null && - (titleLine = bReader.readLine())!=null && (depthLine = bReader.readLine())!=null) - { - // Read "dt=text" [or error] - NB THE OLDER FORMAT DOESN'T INCLUDE THIS LINE SO WE SKIP IT - if(dtLine.equals("") && nodeLine.startsWith("<bmarks>")) - throw new IOException("This is not a Treepad-Lite-compatible file!\n\nFiles created in more advanced versions of Treepad\ncontain features that are not available in Jreepad."); - - if(hjtFileFormat != 2) - if(! (dtLine.toLowerCase().startsWith("dt=text"))) - throw new IOException("Unrecognised node dt format at line " + lineNum + ": " + dtLine); - // Read "<node>" [or error] - if(! (nodeLine.toLowerCase().startsWith("<node>"))) - throw new IOException("Unrecognised node format at line " + (lineNum+1) + ": " + nodeLine); - - lineNum += 4; - - // Read THE CONTENT! [loop until we find "<end node> 5P9i0s8y19Z"] - currentContent = new StringBuffer(); - while((currentLine = bReader.readLine())!=null && !currentLine.equals("<end node> 5P9i0s8y19Z")) - { - currentContent.append(currentLine + "\n"); - lineNum++; - } - - // Now, having established the content and the title and the depth, we'll create the child - babyNode = new JreepadNode(titleLine, currentContent.substring(0, Math.max(currentContent.length()-1,0)), - (JreepadNode)(nodeStack.peek())); - // babyNode = new JreepadNode(titleLine, currentContent.substring(0, Math.max(currentContent.length()-2,0)), - // (JreepadNode)(nodeStack.peek())); - - // Turn it into a HTML-mode node if it matches "<html> ... </html>" - String compareContent = babyNode.getContent().toLowerCase().trim(); - int newArticleMode = (autoDetectHtmlArticles && compareContent.startsWith("<html>") && compareContent.endsWith("</html>")) - ? ARTICLEMODE_HTML - : ARTICLEMODE_ORDINARY; - - if(depthLine.equals("0")) - { - this.title = titleLine; - this.content = currentContent.substring(0, Math.max(currentContent.length()-1,0)); - this.articleMode = newArticleMode; - } - else - { - babyNode.setArticleMode(newArticleMode); - depthMarker = Integer.parseInt(depthLine); - while(nodeStack.size()>depthMarker) - nodeStack.pop(); - - ((JreepadNode)(nodeStack.peek())).addChild(babyNode); - nodeStack.push(babyNode); - } - } - - } // End of constructor from FileInputStream - */ public String toString() --- 71,74 ---- |