KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > Utilities


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xslt.model.impl;
20
21 import java.util.Collections JavaDoc;
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
27 import org.netbeans.modules.xslt.model.Import;
28 import org.netbeans.modules.xslt.model.Include;
29 import org.netbeans.modules.xslt.model.Stylesheet;
30 import org.netbeans.modules.xslt.model.XslModel;
31
32
33 /**
34  * @author ads
35  *
36  */

37 final class Utilities {
38     
39     // should not be instantiated
40
private Utilities() {
41     }
42     
43     /*
44      * This method returns XslModels with import precedence order.
45      * This list doesn't contain any marks for comparing
46      * two models from this list, so you cannot determine
47      * from this list whether one model should preceed another model
48      * ( f.e. there can be two included models in this list
49      * and they has equal import precedence and one model
50      * could be before or after other. But there can be also
51      * imported model and included model. In the latter case included
52      * model will be before imported in this list always).
53      */

54     static LinkedHashSet JavaDoc<XslModel> getAvailibleModels( XslModel model ){
55         if ( model == null ) {
56             return new LinkedHashSet JavaDoc<XslModel>();
57         }
58         LinkedHashSet JavaDoc<XslModel> list = new LinkedHashSet JavaDoc<XslModel>();
59         list.add( model );
60         collectModels( model , list );
61         return list;
62     }
63     
64    static void collectModels( XslModel model, LinkedHashSet JavaDoc<XslModel> list ) {
65        Stylesheet stylesheet = model.getStylesheet();
66        if ( stylesheet == null ) {
67            return;
68        }
69        List JavaDoc<Include> includes = stylesheet.getChildren( Include.class );
70        for (Include include : includes) {
71            XslModel refModel;
72            try {
73                refModel = include.resolveReferencedModel();
74            }
75            catch (CatalogModelException e) {
76                // ignore exception and proceed with other models
77
continue;
78            }
79            if ( list.contains( refModel )) {
80                continue;
81            }
82            else {
83                list.add( refModel );
84                collectModels(refModel, list);
85            }
86        }
87        
88        LinkedList JavaDoc<Import> imports =
89            new LinkedList JavaDoc<Import>(stylesheet.getImports());
90        Collections.reverse( imports );
91        for (Import imprt : imports) {
92            XslModel refModel;
93            try {
94                refModel = imprt.resolveReferencedModel();
95            }
96            catch (CatalogModelException e) {
97                // ignore exception and proceed with other models
98
continue;
99            }
100            if ( list.contains( refModel )) {
101                continue;
102            }
103            else {
104                list.add( refModel );
105                collectModels(refModel, list);
106            }
107        }
108    }
109    
110    
111    static boolean equals( String JavaDoc first , String JavaDoc second ) {
112        if ( first == null ) {
113            return second == null;
114        }
115        else {
116            return first.equals(second);
117        }
118    }
119
120 }
121
Popular Tags