KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > structure > api > DocumentModelUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.editor.structure.api;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27
28
29 /**
30  * An utilitity class containing various methods simplifying work with the DocumentElements.
31  *
32  * @author Marek Fukala
33  * @version 1.0
34  */

35 public final class DocumentModelUtils {
36
37     /** Returns and element starting on the specified position with specied name and type.
38      * If any of these conditions is not true (there isn't any element on the offset, or
39      * there is an element, but the name or the type does't match) it returns null.
40      *
41      * @param startOffset - offset of the searched element
42      * @param name - name of the element
43      * @param type - type of the element
44      *
45      * @return the element or null, when there is not such an element.
46      */

47     public static DocumentElement findElement(DocumentModel model, int startOffset, String JavaDoc name, String JavaDoc type) throws BadLocationException JavaDoc {
48         List JavaDoc els = model.getDocumentElements(startOffset);
49         Iterator JavaDoc 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; //no such element found
55
}
56     
57     /** Returns a list of all document elements which are descendants of the givent DocumentElement.
58      *
59      * @return list of document elements descendants or empty list if there isn't any descendant.
60      */

61     
62     public static List JavaDoc<DocumentElement> getDescendants(DocumentElement de) {
63         ArrayList JavaDoc<DocumentElement> desc = new ArrayList JavaDoc<DocumentElement>();
64         Iterator JavaDoc 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     /** Dumps a tree like view of document element's children.
74      * To see a hierarchical view of the entire model use dumpElementStructure(model.getRootElement());
75      */

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 JavaDoc sb = new StringBuffer JavaDoc();
83         for(int i = 0; i < level; i++) {
84             sb.append(' ');
85         }
86         String JavaDoc text = de.toString();
87         sb.append(text);
88         System.out.println(sb.toString());
89         
90         Iterator JavaDoc children = de.getChildren().iterator();
91         while(children.hasNext()) {
92             dumpElementStructure((DocumentElement)children.next(), level + 4);
93         }
94     }
95     
96     /** Dumps a list of existing elements in the model.
97      * This method is mainly used for testing purposes.*/

98     public static void dumpModelElements(DocumentModel model) {
99         model.debugElements();
100     }
101     
102 }
103
Popular Tags