private boolean checkDocument() { while (true) { if (this.document != panel.getDocument() || textInlineMap == null) { this.document = panel.getDocument(); textInlineMap = null; this.dotInfo = null; this.markInfo = null; this.lastSelectionRange = null; try { this.docRange = (DocumentRange) panel.getDocument(); this.docTraversal = (DocumentTraversal) panel.getDocument(); if (this.document != null && this.createMaps()) { return true; } try { Thread.sleep(10); } catch (InterruptedException e) { return false; } } catch (ClassCastException cce) { XRLog.layout(Level.WARNING, "Document instance cannot create ranges: no selection possible"); return false; } } return true; } }
public String resolve(String infileAddress, String content, String mimeType) throws AddressException{ try{ // get the start and end of the text to find String pieces[] = infileAddress.split("\\.\\.\\."); if(pieces == null || pieces.length != 2){ throw new AddressException("Invalid quote address: " + infileAddress); } String start = pieces[0]; String end = pieces[1]; //System.out.println("start="+start); //System.out.println("end="+end); // parse content into a DOM Document document = parseDocument(content); // now find the beginning and end of the ranges that // match this text Node body = document.getElementsByTagName("body").item(0); Range range = ((DocumentRange)document).createRange(); DocumentTraversal traverse = (DocumentTraversal)document; NodeFilter filter = null; NodeIterator nodes = traverse.createNodeIterator(body, NodeFilter.SHOW_CDATA_SECTION | NodeFilter.SHOW_TEXT, filter, true); boolean results = findString(range, START, nodes, start); if(results == false){ throw new AddressException("Unable to find start of quote range: " + start); } // jump the node iterator backwards one; this is for cases // where the start and end text are in the same node nodes.previousNode(); //System.out.println("nodes="+nodes); results = findString(range, END, nodes, end); if(results == false){ throw new AddressException("Unable to find end of quote range: " + end); } // get the fragment represented by this range DocumentFragment fragment = range.cloneContents(); // serialize fragment into string //System.out.println("fragment="+fragment); return serialize((Node)fragment); }catch(Exception e){ e.printStackTrace(); throw new AddressException(e); } }