@Override public int compareTo(Document that, boolean enforceFieldOrder) { if (that == null) return 1; if (this.size() != that.size()) { return this.size() - that.size(); } int diff = 0; if (enforceFieldOrder) { Iterator<CharSequence> thisIter = this.keySet().iterator(); // ordered Iterator<CharSequence> thatIter = that.keySet().iterator(); // ordered while (thisIter.hasNext() && thatIter.hasNext()) { String thisKey = thisIter.next().toString(); String thatKey = thatIter.next().toString(); diff = thisKey.compareTo(thatKey); if (diff != 0) return diff; diff = compare(this.get(thisKey), that.get(thatKey)); if (diff != 0) return diff; } if (thisIter.hasNext()) return 1; if (thatIter.hasNext()) return -1; } else { // We don't care about order, so just go through by this Document's fields ... for (Map.Entry<CharSequence, Value> entry : fields.entrySet()) { CharSequence key = entry.getKey(); diff = compare(this.get(key), that.get(key)); if (diff != 0) return diff; } if (that.size() > this.size()) return 1; } return 0; }
public int compare(Document doc1, Document doc2) { for (DocumentComparatorOrderBy orderBy : _columns) { String value1 = doc1.get(orderBy.getName()); String value2 = doc2.get(orderBy.getName()); if (!orderBy.isAsc()) { String temp = value1; value1 = value2; value2 = temp; } int result = 0; if ((value1 != null) && (value2 != null)) { if (orderBy.isCaseSensitive()) { result = value1.compareTo(value2); } else { result = value1.compareToIgnoreCase(value2); } } if (result != 0) { return result; } } return 0; }
/** Tests Element.is() and Element.as(). */ public void testIsAndAs() { assertTrue(Node.is(Document.get())); JavaScriptObject text = Document.get().createTextNode("foo"); assertTrue(Node.is(text)); // Node.is(null) is allowed and should return false. assertFalse(Node.is(null)); }
private void runQuery() { boolean inc = true; Document documentIn = null, documentOut = null; Set<String> queryKeys = query.getKeys(); Set<String> projectionKeys = projection.getKeys(); Iterator<Document> it = collection.documents.iterator(); while (it.hasNext()) { inc = true; documentIn = it.next(); for (String key : queryKeys) if (!documentIn.containsKey(key) || !documentIn.get(key).equals(query.get(key))) { inc = false; break; } if (!inc) continue; documentOut = documentIn; for (String key : projectionKeys) { if (documentOut.containsKey(key)) { System.out.println("projKey: (" + key.getClass() + ") " + key); System.out.println( "projVal: (" + projection.get(key).getClass() + ") " + projection.get(key)); if ((projection.get(key) instanceof Long && (Long) projection.get(key) == 0) || (projection.get(key) instanceof Boolean && (Boolean) projection.get(key) == false)) documentOut.remove(key); } } documents.add(documentOut); } }
/** childNodes, hasChildNodes. */ public void testChildNodeList() { Document doc = Document.get(); BodyElement body = doc.getBody(); // <div>foo<button/>bar</div> DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); ButtonElement btn0 = doc.createButtonElement(); Text txt1 = doc.createTextNode("bar"); body.appendChild(div); div.appendChild(txt0); div.appendChild(btn0); div.appendChild(txt1); NodeList<Node> children = div.getChildNodes(); assertEquals(3, children.getLength()); assertEquals(txt0, children.getItem(0)); assertEquals(btn0, children.getItem(1)); assertEquals(txt1, children.getItem(2)); assertEquals(3, div.getChildCount()); assertEquals(txt0, div.getChild(0)); assertEquals(btn0, div.getChild(1)); assertEquals(txt1, div.getChild(2)); assertFalse(txt0.hasChildNodes()); assertTrue(div.hasChildNodes()); }
/** setAttribute, getAttribute, hasAttributes, hasAttribute. */ public void testAttributes() { Document doc = Document.get(); DivElement div = doc.createDivElement(); div.setAttribute("id", "myId"); assertEquals("myId", div.getAttribute("id")); }
public void addItems(Iterable<TItemInput> items, boolean top) { TableSectionElement tbody = Document.get().createTBodyElement(); for (TItemInput item : items) tbody.appendChild(codec_.getRowForItem(item)); if (top) addToTop(tbody); else getElement().appendChild(tbody); codec_.onRowsChanged(tbody); }
public static Video wrap(VideoElement e) { assert Document.get().getBody().isOrHasChild(e); Video v = new Video(e); v.maybeInitMediaEvents(); // The two lines below seems to be good practice to avoid memory leaks... v.onAttach(); RootPanel.detachOnWindowClose(v); return v; }
/** nodeType. */ public void testNodeType() { Document doc = Document.get(); DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); assertEquals(Node.DOCUMENT_NODE, doc.getNodeType()); assertEquals(Node.ELEMENT_NODE, div.getNodeType()); assertEquals(Node.TEXT_NODE, txt0.getNodeType()); }
public S screenCenter() { int width = this.outerWidth(); int height = this.outerHeight(); int left = (Window.getClientWidth() - width) >> 1; int top = (Window.getClientHeight() - height) >> 1; int computedLeft = Math.max(Window.getScrollLeft() + left, 0) - Document.get().getBodyOffsetLeft(); int computedTop = Math.max(Window.getScrollTop() + top, 0) - Document.get().getBodyOffsetTop(); Element element = this.getElement(); element.getStyle().setPropertyPx("left", computedLeft); element.getStyle().setPropertyPx("top", computedTop); element.getStyle().setPosition(Position.ABSOLUTE); return (S) this; }
private HeadElement getHead() { if (head == null) { Element elt = Document.get().getElementsByTagName("head").getItem(0); assert elt != null : "The host HTML page does not have a <head> element" + " which is required by StyleInjector"; head = HeadElement.as(elt); } return head; }
private void printHits(PrintWriter out, ScoreDoc[] hits, IndexSearcher searcher) throws IOException { out.println(hits.length + " total results\n"); for (int i = 0; i < hits.length; i++) { if (i < 10 || (i > 94 && i < 105)) { Document d = searcher.doc(hits[i].doc); out.println(i + " " + d.get(ID_FIELD)); } } }
private void checkHits(ScoreDoc[] hits, int expectedCount, IndexSearcher searcher) throws IOException { assertEquals("total results", expectedCount, hits.length); for (int i = 0; i < hits.length; i++) { if (i < 10 || (i > 94 && i < 105)) { Document d = searcher.doc(hits[i].doc); assertEquals("check " + i, String.valueOf(i), d.get(ID_FIELD)); } } }
/** nodeName, nodeValue. */ public void testNodeNameAndValue() { Document doc = Document.get(); DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); assertEquals("div", div.getNodeName().toLowerCase()); assertEquals("foo", txt0.getNodeValue()); txt0.setNodeValue("bar"); assertEquals("bar", txt0.getNodeValue()); }
/** appendChild, insertBefore, removeChild, replaceChild. */ public void testAppendRemoveReplace() { Document doc = Document.get(); BodyElement body = doc.getBody(); // <div>foo<button/>bar</div> DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); ButtonElement btn0 = doc.createButtonElement(); Text txt1 = doc.createTextNode("bar"); body.appendChild(div); div.appendChild(txt0); div.appendChild(btn0); div.appendChild(txt1); // appendChild, insertBefore ButtonElement btn1 = doc.createButtonElement(); // <div>foo<btn0/>bar<btn1/></div> div.appendChild(btn1); assertEquals(btn1, div.getLastChild()); // <div>foo<button/>bar<button/></div> div.insertBefore(btn1, txt1); assertEquals(4, div.getChildNodes().getLength()); assertEquals(btn1, div.getChildNodes().getItem(2)); // removeChild // <div>foo<btn0/>bar</div> (back to original) div.removeChild(btn1); assertEquals(3, div.getChildNodes().getLength()); // replaceChild // <div>foo<btn1/>bar</div> div.replaceChild(btn1, btn0); assertEquals(btn1, txt0.getNextSibling()); assertEquals(btn1, txt1.getPreviousSibling()); // insertAfter // <div>foo<btn1/><btn0/>bar</div> div.insertAfter(btn0, btn1); assertEquals(btn0, btn1.getNextSibling()); // insertFirst // <div><btn1/>foo<btn0/>bar</div> div.insertFirst(btn1); assertEquals(btn1, div.getFirstChild()); // removeFromParent // <div>foo<btn0/>bar</div> btn1.removeFromParent(); assertNull(btn1.getParentElement()); assertEquals(txt0, div.getFirstChild()); }
@Override public int compareToUsingSimilarFields(Document that) { if (that == null) return 1; int diff = 0; // We don't care about order, so just go through by this Document's fields ... for (Map.Entry<CharSequence, Value> entry : fields.entrySet()) { CharSequence key = entry.getKey(); diff = compareNonNull(this.get(key), that.get(key)); if (diff != 0) return diff; } return 0; }
private static String _getSearchEngineId(Document document) { String entryClassName = document.get("entryClassName"); Indexer indexer = IndexerRegistryUtil.getIndexer(entryClassName); String searchEngineId = indexer.getSearchEngineId(); if (_log.isDebugEnabled()) { _log.debug("Search engine ID for " + indexer.getClass() + " is " + searchEngineId); } return searchEngineId; }
/** getParentElement. */ public void testGetParentDoesntCycle() { Element element = Document.get().getBody(); int i = 0; while (i < 10 && element != null) { element = element.getParentElement(); i++; } // If we got here we looped "forever" or passed, as no exception was thrown. if (i == 10) { fail("Cyclic parent structure detected."); } // If we get here, we pass, because we encountered no errors going to the // top of the parent hierarchy. }
public FastSelectTable( ItemCodec<TItemInput, TItemOutput, TItemOutput2> codec, String selectedClassName, boolean focusable, boolean allowMultiSelect) { codec_ = codec; selectedClassName_ = selectedClassName; allowMultiSelect_ = allowMultiSelect; table_ = Document.get().createTableElement(); if (focusable) table_.setTabIndex(0); table_.setCellPadding(0); table_.setCellSpacing(0); table_.setBorder(0); table_.getStyle().setCursor(Cursor.DEFAULT); setElement(table_); addMouseDownHandler( new MouseDownHandler() { public void onMouseDown(MouseDownEvent event) { if (event.getNativeButton() != NativeEvent.BUTTON_LEFT) return; event.preventDefault(); NativeWindow.get().focus(); DomUtils.setActive(getElement()); Element cell = getEventTargetCell((Event) event.getNativeEvent()); if (cell == null) return; TableRowElement row = (TableRowElement) cell.getParentElement(); if (codec_.isValueRow(row)) handleRowClick(event, row); } }); addClickHandler( new ClickHandler() { public void onClick(ClickEvent event) { event.preventDefault(); } }); addKeyDownHandler( new KeyDownHandler() { public void onKeyDown(KeyDownEvent event) { handleKeyDown(event); } }); }
private void write(String key, Document document, DocumentType type, ValueList valueList) { Set<String> keySet = type.types.keySet(); for (String keyName : keySet) { ElementType elType = type.types.get(keyName); Object value = document.get(keyName); if (value == null) { valueList.append((String) null); continue; } if (elType.isBasicType()) { valueList.append(value); // for now only datatypes can be indexed indexMap = allIndices.get(keyName); if (indexMap != null) { setIndex(keyName, value, key); } } else if ((elType.type == ElementType.TYPE_REFERENCE) || (elType.type == ElementType.TYPE_REFERENCE_ARRAY)) { String dbid = writeReference( key, (Document) value, elType.nestedType, elType.reference, elType.referenceKey); valueList.append(dbid); } else if (value instanceof Document) { writeDocument(key, (Document) value, elType.nestedType, valueList, scratchList); } else if (value instanceof Document[]) { scratchList.clear(); writeList.clear(); for (int j = 0; j < ((Document[]) value).length; j++) { writeDocument(key, ((Document[]) value)[j], elType.nestedType, scratchList, writeList); } valueList.append(scratchList); } else if (elType.type == ElementType.TYPE_STRING_ARRAY) { writeStringArray((String[]) value); } else if ((elType.type == ElementType.TYPE_INTEGER_ARRAY) || (elType.type == ElementType.TYPE_INTEGER_WRAPPER_ARRAY)) { writeIntArray(value, elType.type); } else if ((elType.type == ElementType.TYPE_LONG_ARRAY) || (elType.type == ElementType.TYPE_LONG_WRAPPER_ARRAY)) { writeLongArray(value, elType.type); } else if ((elType.type == ElementType.TYPE_DOUBLE_ARRAY) || (elType.type == ElementType.TYPE_DOUBLE_WRAPPER_ARRAY)) { writeDoubleArray(value, elType.type); } else { // if (type == ElementType.TYPE_BACK_REFERENCE) { valueList.append(value); } } }
private String writeReference( String key, Document document, DocumentType type, String reference, String refKey) { String referenceKey = document.get(refKey).toString(); NodeReference refGlobal = getReferenceGlobal(reference); refGlobal.appendSubscript(referenceKey); if (refGlobal.exists()) { return referenceKey; } referenceTempList.clear(); NodeReference oldIndexGlobal = indexGlobal; indexGlobal = getIndexGlobal(reference); write(key, document, type, referenceTempList); // document.setDBID(name,key); indexGlobal = oldIndexGlobal; refGlobal.set(referenceTempList); return referenceKey; // document.getDBID(); }
/** hasParentElement, getParentElement. */ public void testNodeParentElement() { Document doc = Document.get(); BodyElement body = doc.getBody(); DivElement div = doc.createDivElement(); Text text = doc.createTextNode("foo"); // An unattached node should have no parent element. assertFalse(text.hasParentElement()); assertFalse(div.hasParentElement()); assertNull(text.getParentElement()); assertNull(div.getParentElement()); // Test attached cases. body.appendChild(div); div.appendChild(text); assertTrue(div.hasParentElement()); assertTrue(text.hasParentElement()); assertEquals(body, div.getParentElement()); assertEquals(div, text.getParentElement()); }
/** ownerDocument. */ @DoNotRunWith(Platform.HtmlUnitBug) public void testOwnerDocument() { Document doc = Document.get(); BodyElement body = doc.getBody(); // <div>foo<button/>bar</div> DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); ButtonElement btn0 = doc.createButtonElement(); Text txt1 = doc.createTextNode("bar"); body.appendChild(div); div.appendChild(txt0); div.appendChild(btn0); div.appendChild(txt1); // ownerDocument assertEquals(doc, div.getOwnerDocument()); assertEquals(doc, txt0.getOwnerDocument()); }
/** isOrHasChild. */ public void testIsOrHasChild() { Document doc = Document.get(); DivElement div = doc.createDivElement(); DivElement childDiv = doc.createDivElement(); Text text = doc.createTextNode("foo"); assertFalse(div.isOrHasChild(childDiv)); assertFalse(div.isOrHasChild(text)); assertFalse(childDiv.isOrHasChild(text)); assertTrue(div.isOrHasChild(div)); assertTrue(text.isOrHasChild(text)); div.appendChild(childDiv); childDiv.appendChild(text); assertTrue(div.isOrHasChild(childDiv)); assertTrue(div.isOrHasChild(text)); assertFalse(childDiv.isOrHasChild(div)); assertFalse(text.isOrHasChild(childDiv)); assertFalse(text.isOrHasChild(div)); BodyElement body = doc.getBody(); body.appendChild(div); assertTrue(body.isOrHasChild(body)); assertTrue(body.isOrHasChild(div)); assertTrue(body.isOrHasChild(childDiv)); assertTrue(body.isOrHasChild(text)); assertTrue(div.isOrHasChild(div)); assertTrue(div.isOrHasChild(childDiv)); assertTrue(div.isOrHasChild(text)); assertFalse(childDiv.isOrHasChild(div)); assertFalse(text.isOrHasChild(div)); }
/** getParentNode, firstChild, lastChild, nextSibling, previousSibling. */ public void testParentAndSiblings() { Document doc = Document.get(); BodyElement body = doc.getBody(); // <div>foo<button/>bar</div> DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); ButtonElement btn0 = doc.createButtonElement(); Text txt1 = doc.createTextNode("bar"); body.appendChild(div); div.appendChild(txt0); div.appendChild(btn0); div.appendChild(txt1); assertEquals(div, btn0.getParentNode()); assertEquals(txt0, div.getFirstChild()); assertEquals(txt1, div.getLastChild()); assertEquals(btn0, txt0.getNextSibling()); assertEquals(btn0, txt1.getPreviousSibling()); assertEquals(null, txt0.getPreviousSibling()); assertEquals(null, txt1.getNextSibling()); }
private StyleElement createElement(String contents) { StyleElement style = Document.get().createStyleElement(); style.setPropertyString("language", "text/css"); setContents(style, contents); return style; }
// private static int[] oldToNew(IndexReader reader, Searcher searcher) throws IOException { private static DocScore[] newToOld(IndexReader reader, Searcher searcher) throws IOException { int readerMax = reader.maxDoc(); DocScore[] newToOld = new DocScore[readerMax]; // use site, an indexed, un-tokenized field to get boost // byte[] boosts = reader.norms("site"); TODO MC /* TODO MC */ Document docMeta; Pattern includes = Pattern.compile("\\|"); String value = NutchConfiguration.create().get(INCLUDE_EXTENSIONS_KEY, ""); String includeExtensions[] = includes.split(value); Hashtable<String, Boolean> validExtensions = new Hashtable<String, Boolean>(); for (int i = 0; i < includeExtensions.length; i++) { validExtensions.put(includeExtensions[i], true); System.out.println("extension boosted " + includeExtensions[i]); } /* TODO MC */ for (int oldDoc = 0; oldDoc < readerMax; oldDoc++) { float score; if (reader.isDeleted(oldDoc)) { // score = 0.0f; score = -1f; // TODO MC } else { // score = Similarity.decodeNorm(boosts[oldDoc]); TODO MC /* TODO MC */ docMeta = searcher.doc(oldDoc); if (validExtensions.get(docMeta.get("subType")) == null) { // searched extensions will have higher scores score = -0.5f; } else { score = Integer.parseInt(docMeta.get("inlinks")); /* if (score==0) { score=0.001f; // TODO MC - to not erase } */ } /* TODO MC */ // System.out.println("Score for old document "+oldDoc+" is "+score+" and type // "+docMeta.get("subType")); // TODO MC debug remove } DocScore docScore = new DocScore(); docScore.doc = oldDoc; docScore.score = score; newToOld[oldDoc] = docScore; } System.out.println("Sorting " + newToOld.length + " documents."); Arrays.sort(newToOld); // HeapSorter.sort(newToOld); // TODO MC - due to the lack of space /* TODO MC int[] oldToNew = new int[readerMax]; for (int newDoc = 0; newDoc < readerMax; newDoc++) { DocScore docScore = newToOld[newDoc]; //oldToNew[docScore.oldDoc] = docScore.score > 0.0f ? newDoc : -1; // TODO MC oldToNew[docScore.oldDoc] = newDoc; // TODO MC } */ /* TODO MC * for (int newDoc = 0; newDoc < readerMax; newDoc++) { DocScore docScore = newToOld[newDoc]; System.out.println("Score for new document "+newDoc+" is "+docScore.score); // TODO MC debug remove } * TODO MC */ // return oldToNew; TODO MC return newToOld; // TODO MC }
public boolean accept(Object object) { Document doc = (Document) object; return (Float.parseFloat(doc.get(EARQ.fScore)) >= scoreLimit); }
public void testBinaryFieldInIndex() throws Exception { Fieldable binaryFldStored = new Field("binaryStored", binaryValStored.getBytes(), Field.Store.YES); Fieldable binaryFldCompressed = new Field("binaryCompressed", binaryValCompressed.getBytes(), Field.Store.COMPRESS); Fieldable stringFldStored = new Field( "stringStored", binaryValStored, Field.Store.YES, Field.Index.NO, Field.TermVector.NO); Fieldable stringFldCompressed = new Field( "stringCompressed", binaryValCompressed, Field.Store.COMPRESS, Field.Index.NO, Field.TermVector.NO); try { // binary fields with store off are not allowed new Field("fail", binaryValCompressed.getBytes(), Field.Store.NO); fail(); } catch (IllegalArgumentException iae) {; } Document doc = new Document(); doc.add(binaryFldStored); doc.add(binaryFldCompressed); doc.add(stringFldStored); doc.add(stringFldCompressed); /** test for field count */ assertEquals(4, doc.fields.size()); /** add the doc to a ram index */ RAMDirectory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); writer.addDocument(doc); writer.close(); /** open a reader and fetch the document */ IndexReader reader = IndexReader.open(dir); Document docFromReader = reader.document(0); assertTrue(docFromReader != null); /** fetch the binary stored field and compare it's content with the original one */ String binaryFldStoredTest = new String(docFromReader.getBinaryValue("binaryStored")); assertTrue(binaryFldStoredTest.equals(binaryValStored)); /** fetch the binary compressed field and compare it's content with the original one */ String binaryFldCompressedTest = new String(docFromReader.getBinaryValue("binaryCompressed")); assertTrue(binaryFldCompressedTest.equals(binaryValCompressed)); /** fetch the string field and compare it's content with the original one */ String stringFldStoredTest = docFromReader.get("stringStored"); assertTrue(stringFldStoredTest.equals(binaryValStored)); /** fetch the compressed string field and compare it's content with the original one */ String stringFldCompressedTest = docFromReader.get("stringCompressed"); assertTrue(stringFldCompressedTest.equals(binaryValCompressed)); /** delete the document from index */ reader.deleteDocument(0); assertEquals(0, reader.numDocs()); reader.close(); }
public void click() { NativeEvent clickEvent = Document.get().createClickEvent(1, 0, 0, 0, 0, false, false, false, false); DomEvent.fireNativeEvent(clickEvent, hasHandlers_); }