1 19 20 21 package org.netbeans.modules.editor.structure.api; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.swing.text.BadLocationException ; 27 28 29 35 public final class DocumentModelUtils { 36 37 47 public static DocumentElement findElement(DocumentModel model, int startOffset, String name, String type) throws BadLocationException { 48 List els = model.getDocumentElements(startOffset); 49 Iterator i = els.iterator(); 50 while(i.hasNext()) { 51 DocumentElement de = (DocumentElement)i.next(); 52 if(de.getName().equals(name) && de.getType().equals(type)) return de; 53 } 54 return null; } 56 57 61 62 public static List <DocumentElement> getDescendants(DocumentElement de) { 63 ArrayList <DocumentElement> desc = new ArrayList <DocumentElement>(); 64 Iterator children = de.getChildren().iterator(); 65 while(children.hasNext()) { 66 DocumentElement child = (DocumentElement)children.next(); 67 desc.add(child); 68 desc.addAll(getDescendants(child)); 69 } 70 return desc; 71 } 72 73 76 public static void dumpElementStructure(DocumentElement de) { 77 System.out.println("-------- ELEMENTS STRUCTURE --------"); 78 dumpElementStructure(de, 0); 79 } 80 81 private static void dumpElementStructure(DocumentElement de, int level) { 82 StringBuffer sb = new StringBuffer (); 83 for(int i = 0; i < level; i++) { 84 sb.append(' '); 85 } 86 String text = de.toString(); 87 sb.append(text); 88 System.out.println(sb.toString()); 89 90 Iterator children = de.getChildren().iterator(); 91 while(children.hasNext()) { 92 dumpElementStructure((DocumentElement)children.next(), level + 4); 93 } 94 } 95 96 98 public static void dumpModelElements(DocumentModel model) { 99 model.debugElements(); 100 } 101 102 } 103 | Popular Tags |