1 16 package com.blandware.atleap.common.parsers.html; 17 18 import com.blandware.atleap.common.Constants; 19 20 import java.io.Reader ; 21 import java.io.Writer ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 26 27 34 class PlainTextParser extends HTMLParser { 35 36 48 public PlainTextParser(Reader reader, Writer writer, boolean lookingForEncoding, 49 InputStream stream) { 50 super(reader, writer, lookingForEncoding, stream); 51 } 52 53 protected void addText(String text) throws IOException { 54 if (!inStyle && !inScript) { 55 if (lookingForEncoding) { 56 resultedChars.write(text); 57 } else { 58 output.write(text); 59 } 60 } 61 } 62 63 protected void addSpace() throws IOException { 64 if (!inStyle && !inScript) { 65 String space = afterTag ? Constants.EOL : " "; 66 if (lookingForEncoding) { 67 resultedChars.write(space); 68 } else { 69 output.write(space); 70 } 71 } 72 } 73 74 protected void processTag(String tagName, HashMap attributes, 75 boolean closing) throws IOException { 76 checkoutAttribute(attributes, "title"); 77 if (tagName.equalsIgnoreCase("<script")) { 78 inScript = true; 79 } else if (tagName.equalsIgnoreCase("</script")) { 80 inScript = false; 81 } else if (tagName.equalsIgnoreCase("<img")) { 84 checkoutAttribute(attributes, "alt"); 85 } else if (tagName.equalsIgnoreCase("<meta")) { 86 checkoutNameContentPair(attributes, "keywords"); 87 checkoutNameContentPair(attributes, "description"); 88 checkoutNameContentPair(attributes, "copyright"); 89 checkoutNameContentPair(attributes, "publisher"); 90 checkoutNameContentPair(attributes, "author"); 91 if (lookingForEncoding) { 92 checkoutEncodingChange(attributes); 93 } 94 } else if (tagName.equalsIgnoreCase("</head") 95 || tagName.equalsIgnoreCase("<body")) { 96 if (!headFinished) { 97 lookingForEncoding = false; 99 headFinished = true; 100 if (initialStream instanceof RewindableInputStreamWrapper) { 101 RewindableInputStreamWrapper stream = (RewindableInputStreamWrapper) initialStream; 102 stream.stopRemembering(); 103 output.write(resultedChars.toCharArray()); 104 resultedChars = null; 105 } 106 } 107 } 108 } 109 110 115 protected void considerText(String text) throws IOException { 116 addText(text); 117 } 118 119 124 protected void considerSpace() throws IOException { 125 addSpace(); 126 } 127 } 128 | Popular Tags |