001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.model.impl;
020:
021: import java.util.Collections;
022: import java.util.LinkedHashSet;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
027: import org.netbeans.modules.xslt.model.Import;
028: import org.netbeans.modules.xslt.model.Include;
029: import org.netbeans.modules.xslt.model.Stylesheet;
030: import org.netbeans.modules.xslt.model.XslModel;
031:
032: /**
033: * @author ads
034: *
035: */
036: final class Utilities {
037:
038: // should not be instantiated
039: private Utilities() {
040: }
041:
042: /*
043: * This method returns XslModels with import precedence order.
044: * This list doesn't contain any marks for comparing
045: * two models from this list, so you cannot determine
046: * from this list whether one model should preceed another model
047: * ( f.e. there can be two included models in this list
048: * and they has equal import precedence and one model
049: * could be before or after other. But there can be also
050: * imported model and included model. In the latter case included
051: * model will be before imported in this list always).
052: */
053: static LinkedHashSet<XslModel> getAvailibleModels(XslModel model) {
054: if (model == null) {
055: return new LinkedHashSet<XslModel>();
056: }
057: LinkedHashSet<XslModel> list = new LinkedHashSet<XslModel>();
058: list.add(model);
059: collectModels(model, list);
060: return list;
061: }
062:
063: static void collectModels(XslModel model,
064: LinkedHashSet<XslModel> list) {
065: Stylesheet stylesheet = model.getStylesheet();
066: if (stylesheet == null) {
067: return;
068: }
069: List<Include> includes = stylesheet.getChildren(Include.class);
070: for (Include include : includes) {
071: XslModel refModel;
072: try {
073: refModel = include.resolveReferencedModel();
074: } catch (CatalogModelException e) {
075: // ignore exception and proceed with other models
076: continue;
077: }
078: if (list.contains(refModel)) {
079: continue;
080: } else {
081: list.add(refModel);
082: collectModels(refModel, list);
083: }
084: }
085:
086: LinkedList<Import> imports = new LinkedList<Import>(stylesheet
087: .getImports());
088: Collections.reverse(imports);
089: for (Import imprt : imports) {
090: XslModel refModel;
091: try {
092: refModel = imprt.resolveReferencedModel();
093: } catch (CatalogModelException e) {
094: // ignore exception and proceed with other models
095: continue;
096: }
097: if (list.contains(refModel)) {
098: continue;
099: } else {
100: list.add(refModel);
101: collectModels(refModel, list);
102: }
103: }
104: }
105:
106: static boolean equals(String first, String second) {
107: if (first == null) {
108: return second == null;
109: } else {
110: return first.equals(second);
111: }
112: }
113:
114: }
|