public int getTextIndex() { return EventQueueWait.exec(new Callable<Integer>() { @Override public Integer call() throws Exception { String href = getText(); int hRefIndex = 0; int current = 0; JEditorPane editor = (JEditorPane) parent.getComponent(); HTMLDocument document = (HTMLDocument) editor.getDocument(); Iterator iterator = document.getIterator(Tag.A); while (iterator.isValid()) { if (current++ >= index) { return hRefIndex; } String attribute = ((HTMLDocument) ((JEditorPane) parent.getComponent()).getDocument()) .getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset()); if (attribute != null && attribute.equals(href)) { hRefIndex++; } iterator.next(); } return -1; } }); }
private List<IJavaElement> selectByProperties(final ArrayList<IJavaElement> r, JSONObject o) { final Properties p; if (o.has("select")) { String spec = o.getString("select"); if (!spec.startsWith("text=") && !spec.startsWith("link=")) { int pos = Integer.parseInt(spec); return Arrays.asList((IJavaElement) new JEditorPanePosJavaElement(this, pos)); } p = parseSelectProperties(spec); } else { p = PropertyHelper.asProperties(o); } EventQueueWait.exec(new Runnable() { @Override public void run() { fillElements(Tag.A, r, new PropertyPredicate(p)); } }); return r; }
@Override public void handleStartTag(HTML.Tag tag, MutableAttributeSet att, int pos) { if (tag.equals(HTML.Tag.A)) { // <a href=...> tag String attribute = (String) att.getAttribute(HTML.Attribute.HREF); if (attribute != null) { addReferencedDocument(attribute); } } else if (tag.equals(HTML.Tag.TITLE)) { this.inTitle = true; } }
/** * Finds the named frame inside this document. * * @param target the name to look for * * @return the frame if there is a matching frame, <code>null</code> * otherwise */ private Element findFrame(String target) { ElementIterator i = new ElementIterator(this); Element next = null; while ((next = i.next()) != null) { AttributeSet atts = next.getAttributes(); if (atts.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.FRAME) { String name = (String) atts.getAttribute(HTML.Attribute.NAME); if (name != null && name.equals(target)) break; } } return next; }
/** * Called when an end tag is seen for one of the types of tags associated * with this Action. */ public void end(HTML.Tag t) { if (t == HTML.Tag.OPTION) { option = null; } else { if (t == HTML.Tag.TEXTAREA) { inTextArea = false; } else if (t == HTML.Tag.SELECT) { selectModel = null; numOptions = 0; } // Finish the element. super.end(t); } }
public void start(HTML.Tag tag, MutableAttributeSet atts) { pushCharacterStyle(); charAttr.addAttribute(tag, atts.copyAttributes()); StyleSheet styleSheet = getStyleSheet(); // TODO: Add other tags here. if (tag == HTML.Tag.FONT) { String color = (String) atts.getAttribute(HTML.Attribute.COLOR); if (color != null) styleSheet.addCSSAttribute(charAttr, CSS.Attribute.COLOR, color); String face = (String) atts.getAttribute(HTML.Attribute.FACE); if (face != null) styleSheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_FAMILY, face); String size = (String) atts.getAttribute(HTML.Attribute.SIZE); if (size != null) styleSheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_SIZE, size); } }
/** * Called when an end tag is seen for one of the types of tags associated * with this Action. */ public void end(HTML.Tag t) { // We read in all the stylesheets that are embedded or referenced // inside the header. if (styles != null) { int numStyles = styles.size(); for (int i = 0; i < numStyles; i++) { String style = (String) styles.get(i); getStyleSheet().addRule(style); } } super.end(t); }
/** * This is a callback from the parser that should be routed to the * appropriate handler for the tag. * * @param t the HTML.Tag that was encountered * @param a the attribute set * @param pos the position at which the tag was encountered */ public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) { if (t == insertTag) insertTagEncountered = true; if (shouldInsert()) { TagAction action = (TagAction) tagToAction.get(t); if (action != null) { action.start(t, a); action.end(t); } } }
/** * Adds the given text that was encountered in a <PRE> element. * This adds synthesized lines to hold the text runs. * * @param data the text */ protected void preContent(char[] data) { int start = 0; for (int i = 0; i < data.length; i++) { if (data[i] == '\n') { addContent(data, start, i - start + 1); blockClose(HTML.Tag.IMPLIED); MutableAttributeSet atts = new SimpleAttributeSet(); atts.addAttribute(CSS.Attribute.WHITE_SPACE, "pre"); blockOpen(HTML.Tag.IMPLIED, atts); start = i + 1; } } if (start < data.length) { // Add remaining last line. addContent(data, start, data.length - start); } }
/** * Instructs the parse buffer to create a block element with the given * attributes. * * @param t the tag that requires opening a new block * @param attr the attribute set for the new block */ protected void blockOpen(HTML.Tag t, MutableAttributeSet attr) { if (inImpliedParagraph()) blockClose(HTML.Tag.IMPLIED); // Push the new tag on top of the stack. parseStack.push(t); DefaultStyledDocument.ElementSpec element; AbstractDocument.AttributeContext ctx = getAttributeContext(); AttributeSet copy = attr.copyAttributes(); copy = ctx.addAttribute(copy, StyleConstants.NameAttribute, t); element = new DefaultStyledDocument.ElementSpec(copy, DefaultStyledDocument.ElementSpec.StartTagType); parseBuffer.addElement(element); }
/** * Adds content that is specified in the attribute set. * * @param t the HTML.Tag * @param a the attribute set specifying the special content */ protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a) { if (t != HTML.Tag.FRAME && ! inParagraph()) { blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet()); } a.addAttribute(StyleConstants.NameAttribute, t); // The two spaces are required because some special elements like HR // must be broken. At least two characters are needed to break into the // two parts. DefaultStyledDocument.ElementSpec spec = new DefaultStyledDocument.ElementSpec(a.copyAttributes(), DefaultStyledDocument.ElementSpec.ContentType, new char[] {' '}, 0, 1 ); parseBuffer.add(spec); }
/** * Replaces the children of the given element with the contents of * the string. The document must have an HTMLEditorKit.Parser set. * This will be seen as at least two events, n inserts followed by a remove. * * @param elem - the brance element whose children will be replaced * @param htmlText - the string to be parsed and assigned to element. * @throws BadLocationException * @throws IOException * @throws IllegalArgumentException - if elem is a leaf * @throws IllegalStateException - if an HTMLEditorKit.Parser has not been set */ public void setInnerHTML(Element elem, String htmlText) throws BadLocationException, IOException { if (elem.isLeaf()) throw new IllegalArgumentException("Element is a leaf"); int start = elem.getStartOffset(); int end = elem.getEndOffset(); HTMLEditorKit.ParserCallback reader = getInsertingReader( end, 0, 0, HTML.Tag.BODY, elem); // TODO charset getParser().parse(new StringReader(htmlText), reader, true); // Remove the previous content remove(start, end - start); }
/** * Replaces the given element in the parent with the string. When replacing a * leaf, this will attempt to make sure there is a newline present if one is * needed. This may result in an additional element being inserted. This will * be seen as at least two events, n inserts followed by a remove. The * HTMLEditorKit.Parser must be set. * * @param elem - the branch element whose parent will be replaced * @param htmlText - the string to be parsed and assigned to elem * @throws BadLocationException * @throws IOException * @throws IllegalStateException - if parser is not set */ public void setOuterHTML(Element elem, String htmlText) throws BadLocationException, IOException { // Remove the current element: int start = elem.getStartOffset(); int end = elem.getEndOffset(); remove(start, end-start); HTMLEditorKit.ParserCallback reader = getInsertingReader( start, 0, 0, HTML.Tag.BODY, elem); // TODO charset getParser().parse(new StringReader(htmlText), reader, true); }
/** * @author Kevin Pearson * @throws IOException */ @Test( description="Tests if a user can input a user name and click enter to log in", groups="uitest" ) public void testLogin() throws IOException { String user = "integrationuser"; RemoteWebDriver driver = drivers.get(); driver.get(TestServers.getHouse()); loginWithUserName(driver, user); String expectedNewPageUrl = TestServers.getHouse() + user + "/"; Assert.assertEquals(driver.getCurrentUrl(), expectedNewPageUrl); WebElement userInfo = driver.findElementByClassName(IndexPage.USER_SECTION_CLASS); List<WebElement> spans = userInfo.findElements(By.tagName(Tag.SPAN.toString())); Assert.assertFalse(spans.isEmpty()); WebElement userDisplaySpan = spans.get(0); // the first span should show the user name Assert.assertEquals(userDisplaySpan.getText(), user); }
/** * Scrape the site. * * @throws IOException */ @Override public void scrape() throws IOException { logger.info("Start scraping"); Cache cache = getCache(); Map<String, Page> front = cache.retrievePage(getBase()); if (front.keySet().isEmpty()) { scrapePage(cache); front = cache.retrievePage(getBase()); } // Calculate the number of datasets Page p = front.get(getDefaultLang()); String datasets = p.getContent(); Elements rows = Jsoup.parse(datasets).getElementsByTag(HTML.Tag.TR.toString()); logger.info("Found {} datasets on page", String.valueOf(rows.size())); logger.info("Done scraping"); }
/** * Generate one dataset * * @param store RDF store * @param front front * @param row HTML row * @param i number * @param lang language * @throws MalformedURLException * @throws RepositoryException */ private void generateDataset(Storage store, URL front, Element row, int i, String lang) throws MalformedURLException, RepositoryException { URL u = makeDatasetURL(String.valueOf(i)); IRI dataset = store.getURI(u.toString()); logger.debug("Generating dataset {}", dataset.toString()); Elements cells = row.getElementsByTag(Tag.TD.toString()); String desc = cells.get(0).text(); String title = desc; store.add(dataset, RDF.TYPE, DCAT.DATASET); store.add(dataset, DCTERMS.LANGUAGE, MDR_LANG.MAP.get(lang)); store.add(dataset, DCTERMS.TITLE, title, lang); store.add(dataset, DCTERMS.DESCRIPTION, desc, lang); store.add(dataset, DCTERMS.IDENTIFIER, makeHashId(u.toString())); store.add(dataset, DCAT.LANDING_PAGE, front); Elements link = cells.get(1).getElementsByTag(Tag.A.toString()); generateDist(store, dataset, front, link, i, lang); }
/** * Generate DCAT datasets. * * @param store RDF store * @param id * @param page * @throws MalformedURLException * @throws RepositoryException */ @Override public void generateDataset(Storage store, String id, Map<String, Page> page) throws MalformedURLException, RepositoryException { String[] langs = getAllLangs(); for (String lang : langs) { Page p = page.getOrDefault(lang, new Page()); String html = p.getContent(); URL front = p.getUrl(); Elements rows = Jsoup.parse(html).body().getElementsByTag(Tag.TR.toString()); int i = 0; for (Element row : rows) { generateDataset(store, front, row, i, lang); i++; } } }
/** * Scrape the site. * * @throws IOException */ @Override public void scrape() throws IOException { logger.info("Start scraping"); Cache cache = getCache(); Map<String, Page> front = cache.retrievePage(getBase()); if (front.keySet().isEmpty()) { scrapePage(); front = cache.retrievePage(getBase()); } // Calculate the number of datasets Page p = front.get(getDefaultLang()); String datasets = p.getContent(); // first row is a header Elements rows = Jsoup.parse(datasets).getElementsByTag(HTML.Tag.TR.toString()); logger.info("Found {} datasets on page", String.valueOf(rows.size() -1 )); logger.info("Done scraping"); }
/** * Generate DCAT datasets. * * @param store RDF store * @param id * @param page * @throws MalformedURLException * @throws RepositoryException */ @Override public void generateDataset(Storage store, String id, Map<String, Page> page) throws MalformedURLException, RepositoryException { String[] langs = getAllLangs(); for (String lang : langs) { Page p = page.getOrDefault(lang, new Page()); String html = p.getContent(); URL front = p.getUrl(); Elements rows = Jsoup.parse(html).body().getElementsByTag(Tag.TR.toString()); // first row is a table header rows.remove(0); for (Element row : rows) { generateDataset(store, front, row, lang); } } }
/** * Create description from paragraphs * * @param el HTML element containing p * @param title title to be used as default * @return description */ private String buildDesc(Element el, String title) { if (el == null) { logger.warn("No {} element", MODAL_BODY); return title; } Elements paras = el.getElementsByTag(Tag.P.toString()); if (paras != null) { StringBuilder buf = new StringBuilder(); for (Element para : paras) { buf.append(para.text()).append('\n'); } if (buf.length() > 0) { return buf.toString(); } } return title; }
public void testSetInnerHTML_Specs2() throws Exception { htmlDoc.setParser(new ParserDelegator()); htmlDoc.setEditable(false); Element root = htmlDoc.getDefaultRootElement(); Element body = root.getElement(0); Element p = body.getElement(0); htmlDoc.setInnerHTML(p, "<a>link</a>"); Marker insertMarker = htmlDoc.getInsertMarker(); assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1)); ElementSpec[] specs = (ElementSpec[])(getInsertInfo(insertMarker).get(0)); insertMarker.reset(); assertEquals(4, specs.length); assertSpec(specs[0], ElementSpec.ContentType, ElementSpec.OriginateDirection, 0, "link".toCharArray()); AttributeSet specAttr = specs[0].getAttributes(); assertEquals(2, specAttr.getAttributeCount()); checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.CONTENT); assertSpec(specs[1], ElementSpec.ContentType, ElementSpec.OriginateDirection, 0, new char[]{'\n'}); checkEndTagSpec(specs[2]); checkEndTagSpec(specs[3]); }
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(); } }
public void testAddSpecialElement_FrameImpliedBlockOpenCheck() { Tag specialTag = Tag.FRAME; SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("aaaa", "bbbb"); reader.addSpecialElement(specialTag , attr); assertEquals(0, reader.charAttr.getAttributeCount()); assertEquals(1, reader.parseBuffer.size()); assertFalse(createMarker.isOccurred()); assertFalse(insertMarker.isOccurred()); ElementSpec spec = (ElementSpec)reader.parseBuffer.get(0); assertSpec(spec, ElementSpec.ContentType, ElementSpec.OriginateDirection, 0, new char[] {' '}); AttributeSet specAttr = spec.getAttributes(); assertEquals(2, specAttr.getAttributeCount()); checkAttributes(specAttr, StyleConstants.NameAttribute, specialTag); checkAttributes(specAttr, "aaaa", "bbbb"); }
public void testFlush_Insert_PushPopTag_Wierd() throws Exception { final String text = "tag"; editable = false; SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT); reader = (HTMLReader)doc.getReader(1000, -15, 330, Tag.HTML); reader.parseBuffer.add(new ElementSpec(attr, ElementSpec.EndTagType)); reader.parseBuffer.add(new ElementSpec(attr, ElementSpec.StartTagType)); reader.parseBuffer.add(new ElementSpec(new SimpleAttributeSet(), ElementSpec.ContentType, text.toCharArray(), 0, 3)); reader.parseBuffer.add(new ElementSpec(attr, ElementSpec.EndTagType)); reader.parseBuffer.add(new ElementSpec(attr, ElementSpec.EndTagType)); reader.parseBuffer.add(new ElementSpec(attr, ElementSpec.StartTagType)); assertEquals(6, reader.parseBuffer.size()); reader.flush(); assertEquals(0, reader.parseBuffer.size()); assertFalse(createMarker.isOccurred()); assertTrue(insertMarker.isOccurred()); assertEquals(6, ((ElementSpec[])insertMarker.getAuxiliary()).length); }
public void testAddSpecialElement_AllTagsImpliedBlockOpenCheck() { final Tag[] allTags = HTML.getAllTags(); for (int i = 0; i < allTags.length; i++) { final Tag tag = allTags[i]; if (Tag.FRAME.equals(tag)) { continue; } init(); SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("aaaa", "bbbb"); reader.addSpecialElement(tag , attr); assertEquals(2, reader.parseBuffer.size()); reader.blockClose(Tag.TABLE); assertEquals(5, reader.parseBuffer.size()); } }
public void testCharacterStart_vs_HandleStart() { SimpleAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("aaaa", "bbbb"); action = reader.new CharacterAction(); reader.charAttr.addAttribute("bbbb", "aaaa"); reader.handleStartTag(Tag.B, attr, 0); assertEquals(0, reader.parseBuffer.size()); assertEquals(3, reader.charAttr.getAttributeCount()); checkAttributes(reader.charAttr, "bbbb", "aaaa"); checkAttributes(reader.charAttr, Tag.B, attr); checkAttributes(reader.charAttr, CSS.Attribute.FONT_WEIGHT, "bold"); reader.popCharacterStyle(); assertEquals(1, reader.charAttr.getAttributeCount()); checkAttributes(reader.charAttr, "bbbb", "aaaa"); }
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 testHandleText_ContentMethodsCalls_Body() throws Exception { final Marker addContentMarker = new Marker(); final Marker preContentMarker = new Marker(); final Marker textAreaMarker = new Marker(); createContentMarkersInstrumentedReader(addContentMarker, preContentMarker, textAreaMarker); String text = "data"; reader.handleText(text.toCharArray(), 0); assertFalse(addContentMarker.isOccurred()); assertFalse(preContentMarker.isOccurred()); assertFalse(textAreaMarker.isOccurred()); reader.handleStartTag(Tag.BODY, new SimpleAttributeSet(), 0); reader.handleText(text.toCharArray(), 0); assertTrue(addContentMarker.isOccurred()); assertFalse(preContentMarker.isOccurred()); assertFalse(textAreaMarker.isOccurred()); addContentMarker.reset(); reader.handleEndTag(Tag.BODY, 0); reader.handleText(text.toCharArray(), 0); assertTrue(addContentMarker.isOccurred()); assertFalse(preContentMarker.isOccurred()); assertFalse(textAreaMarker.isOccurred()); addContentMarker.reset(); }
public void testInsertAfterEnd_Specs2() throws Exception { htmlDoc.setParser(new ParserDelegator()); htmlDoc.setEditable(false); Element root = htmlDoc.getDefaultRootElement(); Element body = root.getElement(0); Element p = body.getElement(0); htmlDoc.insertAfterEnd(p, "<a>link</a>"); Marker insertMarker = htmlDoc.getInsertMarker(); assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1)); ElementSpec[] specs = (ElementSpec[])(getInsertInfo(insertMarker).get(0)); insertMarker.reset(); assertEquals(2, specs.length); checkEndTagSpec(specs[0]); assertSpec(specs[1], ElementSpec.ContentType, ElementSpec.OriginateDirection, 0, "link".toCharArray()); AttributeSet specAttr = specs[1].getAttributes(); assertEquals(2, specAttr.getAttributeCount()); checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.CONTENT); }