/** * Parses the HTML document, calling methods of the provided * callback. This method must be multithread - safe. * @param reader The reader to read the HTML document from * @param a_callback The callback that is notifyed about the presence * of HTML elements in the document. * @param ignoreCharSet If thrue, any charset changes during parsing * are ignored. * @throws java.io.IOException */ public void parse(Reader reader, HTMLEditorKit.ParserCallback a_callback, boolean ignoreCharSet ) throws IOException { callBack = a_callback; if (gnu == null || !dtd.equals(gnu.getDTD())) { gnu = new gnuParser(dtd); } gnu.parse(reader); callBack.handleEndOfLineString(gnu.getEndOfLineSequence()); try { callBack.flush(); } catch (BadLocationException ex) { // Convert this into the supported type of exception. throw new IOException(ex.getMessage()); } }
/** * Parses the HTML document, calling methods of the provided callback. This * method must be multithread - safe. * * @param reader The reader to read the HTML document from * @param a_callback The callback that is notifyed about the presence of HTML * elements in the document. * @param ignoreCharSet If thrue, any charset changes during parsing are * ignored. * @throws java.io.IOException */ public void parse(Reader reader, HTMLEditorKit.ParserCallback a_callback, boolean ignoreCharSet) throws IOException { callBack = a_callback; gnu.parse(reader); callBack.handleEndOfLineString(gnu.getEndOfLineSequence()); try { callBack.flush(); } catch (BadLocationException ex) { // Convert this into the supported type of exception. throw new IOException(ex.getMessage()); } }
public void testSpecialStart_Calls() { final Marker specialMarker = new Marker(); doc = new HTMLDocument() { public ParserCallback getReader(int pos) { return new HTMLReader(0) { protected void addSpecialElement(final Tag tag, final MutableAttributeSet attr) { specialMarker.setOccurred(); ArrayList callInfo = new ArrayList(); callInfo.add(tag); callInfo.add(attr); specialMarker.setAuxiliary(callInfo); } }; } }; reader = (HTMLReader)doc.getReader(0); String text = "precontent"; Tag tag = Tag.HTML; SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("aaaa", "bbbb"); reader.addSpecialElement(tag, attr); assertTrue(specialMarker.isOccurred()); ArrayList callInfo = (ArrayList)specialMarker.getAuxiliary(); assertEquals(tag, callInfo.get(0)); assertEquals(attr, callInfo.get(1)); }
public void testHTMLReaderIntIntIntTag_PushPopParameter_BlockParagraph() throws Exception { // trailing specs cutting for block and paragraph tags if (!isHarmony()) { return; } final String str = "<P>link</P>"; init(); editable = false; ParserCallback reader = doc.getReader(0, 2, 3, Tag.P); SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute(StyleConstants.NameAttribute, Tag.S.toString()); doc.insertString(0, "0000", attr); assertFalse(insertMarker.isOccurred()); parse(str, reader); reader.flush(); assertTrue(insertMarker.isOccurred()); ElementSpec[] specs = (ElementSpec[])insertMarker.getAuxiliary(); String specsDescr = "cpeoeosnsnsnsoco"; assertEquals(specsDescr.length()/2, specs.length); insertMarker.reset(); for (int i = 0; i < specs.length; i++) { checkSpecType(specsDescr, i, (ElementSpec)specs[i]); } }
public void testBlockOpen_ImpliedAttribute() { SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("aaaa", "bbbb"); attr.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED, Boolean.TRUE); reader.blockOpen(Tag.B, attr); assertEquals(0, reader.charAttr.getAttributeCount()); assertEquals(1, reader.parseBuffer.size()); final ElementSpec spec = (ElementSpec)reader.parseBuffer.get(0); final AttributeSet specAttr = spec.getAttributes(); assertEquals(2, specAttr.getAttributeCount()); checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B); checkAttributes(specAttr, "aaaa", "bbbb"); assertSpec(spec, ElementSpec.StartTagType, ElementSpec.OriginateDirection, 0, null); assertNotSame(attr, specAttr); assertEquals(2, attr.getAttributeCount()); checkAttributes(attr, StyleConstants.NameAttribute, Tag.B); checkAttributes(attr, "aaaa", "bbbb"); }
public void testPreContent_Calls() { final Marker contentMarker = new Marker(); doc = new HTMLDocument() { public ParserCallback getReader(int pos) { return new HTMLReader(0) { protected void addContent(char[] data, int offs, int length) { contentMarker.setOccurred(); ArrayList callInfo = new ArrayList(); callInfo.add(data); callInfo.add(new Integer(offs)); callInfo.add(new Integer(length)); contentMarker.setAuxiliary(callInfo); }; }; } }; reader = (HTMLReader)doc.getReader(0); String text = "precontent"; reader.preContent(text.toCharArray()); assertTrue(contentMarker.isOccurred()); ArrayList callInfo = (ArrayList)contentMarker.getAuxiliary(); assertEquals(text, new String((char[])callInfo.get(0))); assertEquals(new Integer(0), callInfo.get(1)); assertEquals(text.length(), ((Integer)callInfo.get(2)).intValue()); }
private void checkConstructorTagParameter(final Tag tag, final String str, final int numSpecs) throws Exception { init(); editable = false; ParserCallback reader = doc.getReader(0, 0, 0, tag); SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute(StyleConstants.NameAttribute, Tag.B.toString()); doc.insertString(0, "0000", attr); assertFalse("no inserts", insertMarker.isOccurred()); parse(str, reader); reader.flush(); if (numSpecs == 0 && isHarmony()) { assertFalse("inserted", insertMarker.isOccurred()); } else { assertTrue("inserted", insertMarker.isOccurred()); ElementSpec[] specs = (ElementSpec[])insertMarker.getAuxiliary(); assertEquals("number of specs inserted", numSpecs, specs.length); insertMarker.reset(); } }
private void checkConstructorPopPushParameter(final Tag tag, final String str, final int pop, final int push, final String specsDescr) throws Exception { init(); editable = false; ParserCallback reader = doc.getReader(0, pop, push, tag); SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute(StyleConstants.NameAttribute, Tag.S.toString()); doc.insertString(0, "0000", attr); assertFalse(insertMarker.isOccurred()); parse(str, reader); reader.flush(); assertTrue(insertMarker.isOccurred()); ElementSpec[] specs = (ElementSpec[])insertMarker.getAuxiliary(); assertEquals(specsDescr.length()/2, specs.length); insertMarker.reset(); for (int i = 0; i < specs.length; i++) { checkSpecType(specsDescr, i, (ElementSpec)specs[i]); } }
private void checkConstructorOffsetPopPushParameter(final Tag tag, final String str, final int offset, final int pop, final int push, final String specsDescr) throws Exception { init(); ParserCallback reader = doc.getReader(offset, pop, push, tag); SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute(StyleConstants.NameAttribute, Tag.S.toString()); editable = false; insertMarker.reset(); parse(str, reader); reader.flush(); assertTrue(insertMarker.isOccurred()); ElementSpec[] specs = (ElementSpec[])insertMarker.getAuxiliary(); assertEquals(specsDescr.length()/2, specs.length); insertMarker.reset(); for (int i = 0; i < specs.length; i++) { checkSpecType(specsDescr, i, (ElementSpec)specs[i]); } }
private void createContentMarkersInstrumentedReader(final Marker addContentMarker, final Marker preContentMarker, final Marker textAreaMarker) { doc = new HTMLDocument() { public ParserCallback getReader(int pos) { return new HTMLReader(pos) { protected void addContent(char[] data, int offset, int length, boolean createImpliedPIfNecessary) { addContentMarker.setOccurred(); super.addContent(data, offset, length, createImpliedPIfNecessary); }; protected void preContent(char[] data) { preContentMarker.setOccurred(); super.preContent(data); } protected void textAreaContent(char[] data) { textAreaMarker.setOccurred(); super.textAreaContent(data); } }; } }; reader = (HTMLReader)doc.getReader(0); }
public String verify(String html, String trace) throws Exception { out.setLength(0); HTMLEditorKit.ParserCallback callback = this; ParserDelegator delegator = new ParserDelegator(); delegator.parse(new StringReader(html), callback, true); String ou = out.toString(); if (trace != null) { if (!ou.equals(trace)) { System.err.println("Unable to parse '" + html + "':"); System.err.println(" expected: '" + trace + "',"); System.out.println(" returned: '" + ou + "'."); throw new Exception("'" + html + "' -> '" + ou + "' expected '" + trace + "'" ); } } return ou; }
protected final void handleStartTag(TagElement tag) { SimpleAttributeSet attributes = gnu.getAttributes(); if (tag.fictional()) attributes.addAttribute(ParserCallback.IMPLIED, Boolean.TRUE); callBack.handleStartTag(tag.getHTMLTag(), attributes, hTag.where.startPosition ); }
public void testPreStartEnd_PreContentCalls() { final Marker preContentMarker = new Marker(); doc = new HTMLDocument() { public ParserCallback getReader(int pos) { return new HTMLReader(0) { protected void preContent(char[] data) { preContentMarker.setOccurred(); preContentMarker.setAuxiliary(data); super.preContent(data); } }; } }; reader = (HTMLReader)doc.getReader(0); SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("aaaa", "bbbb"); action = reader.new PreAction(); action.start(Tag.PRE, attr); String text = "data"; assertFalse(preContentMarker.isOccurred()); reader.handleText(text.toCharArray(), 0); assertTrue(preContentMarker.isOccurred()); assertEquals(text, new String((char [])preContentMarker.getAuxiliary())); action.end(Tag.PRE); preContentMarker.reset(); reader.handleText(text.toCharArray(), 0); assertFalse(preContentMarker.isOccurred()); }
private void checkConstructorOffsetParameter(final Tag tag, final String str, final int offset) throws Exception { insertMarker.reset(); doc = new HTMLDocument() { protected void insert(int offset, ElementSpec[] data) throws BadLocationException { insertMarker.setOccurred(); insertMarker.setAuxiliary(new Integer(offset)); } }; ParserCallback reader = doc.getReader(offset, 0, 0, tag); parse(str, reader); reader.flush(); assertTrue(insertMarker.isOccurred()); assertEquals(new Integer(offset), insertMarker.getAuxiliary()); }