001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.requirements.xmlprovider;
043:
044: import java.util.HashMap;
045: import java.util.List;
046:
047: import org.dom4j.Attribute;
048: import org.dom4j.Branch;
049: import org.dom4j.Document;
050: import org.dom4j.Element;
051: import org.dom4j.Node;
052:
053: import org.netbeans.modules.uml.core.metamodel.core.foundation.IConfigManager;
054: import org.netbeans.modules.uml.core.requirementsframework.IRequirement;
055: import org.netbeans.modules.uml.core.requirementsframework.IRequirementSource;
056: import org.netbeans.modules.uml.core.requirementsframework.ISatisfier;
057: import org.netbeans.modules.uml.core.requirementsframework.Satisfier;
058: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
059: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
060: import org.netbeans.modules.uml.core.support.umlutils.ETList;
061: import org.netbeans.modules.uml.ui.support.ProductHelper;
062: import org.netbeans.modules.uml.ui.support.applicationmanager.IProduct;
063: import org.dom4j.IDResolver;
064:
065: /**
066: * @author josephg
067: *
068: */
069: public class ReqUtils {
070: private static HashMap<String, Document> m_ReqSourcesMap = new HashMap<String, Document>();
071: private static String m_ReqProxyFile = "";
072: private static Document m_Doc;
073:
074: public static String getConfigDir() {
075: String dir = "";
076:
077: IProduct product = ProductHelper.getProduct();
078: if (product != null) {
079: IConfigManager configMgr = product.getConfigManager();
080: if (configMgr != null) {
081: String loc = configMgr.getDefaultConfigLocation();
082: if (loc.length() > 0) {
083: dir = loc;
084: }
085: }
086: }
087: return dir;
088: }
089:
090: public static Document getXMLDoc(
091: IRequirementSource requirementSource) {
092: String location = requirementSource.getLocation();
093: String id = requirementSource.getID();
094:
095: Document doc = m_ReqSourcesMap.get(id);
096:
097: if (doc == null) {
098: if (location.length() > 0) {
099: IDResolver resolver = new IDResolver();
100: resolver.setGlobalID("id");
101: doc = XMLManip.getDOMDocument(location, resolver);
102:
103: if (doc != null) {
104: m_ReqSourcesMap.put(id, doc);
105: }
106: }
107: }
108:
109: return doc;
110: }
111:
112: public static boolean loadXMLDoc(String file) {
113: if (m_Doc == null) {
114: if (file.length() > 0)
115: m_Doc = XMLManip.getDOMDocument(file);
116:
117: if (m_Doc == null)
118: return false;
119: }
120: return true;
121: }
122:
123: public static void addSatisfierChildElements(
124: IRequirement requirement,
125: IRequirementSource requirementSource) {
126: String file = null;
127:
128: if (m_ReqProxyFile.length() > 0) {
129: file = m_ReqProxyFile;
130: } else {
131: String configDir = getConfigDir();
132:
133: if (configDir.length() > 0) {
134: file = configDir + "RequirementSources.etrp";
135: m_ReqProxyFile = file;
136: }
137: }
138:
139: Document reqProxyDoc = XMLManip.getDOMDocument(file);
140:
141: if (reqProxyDoc != null) {
142: String requirementSourceId = requirementSource.getID();
143:
144: StringBuffer pattern = new StringBuffer(
145: "RequirementProxies/RequirementProxy[@source='");
146: pattern.append(requirementSourceId);
147: pattern.append("']");
148:
149: Node requirementProxyNode = reqProxyDoc
150: .selectSingleNode(pattern.toString());
151:
152: if (requirementProxyNode != null) {
153: String id = requirement.getID();
154:
155: pattern = new StringBuffer("Requirement[@id='");
156: pattern.append(id);
157: pattern.append("']");
158:
159: Node requirementNode = requirementProxyNode
160: .selectSingleNode(pattern.toString());
161:
162: if (requirementNode != null) {
163: Node satisfiersNode = requirementNode
164: .selectSingleNode("Satisfiers");
165:
166: if (satisfiersNode != null) {
167: List satisfierNodeList = null;
168: if (satisfiersNode instanceof Branch) {
169: satisfierNodeList = ((Branch) satisfiersNode)
170: .content();
171: }
172:
173: if (satisfierNodeList != null) {
174: ETList<ISatisfier> satisfiers = new ETArrayList<ISatisfier>();
175:
176: int count = satisfierNodeList.size();
177:
178: for (int index = 0; index < count; index++) {
179: Node satisfierNode = (Node) satisfierNodeList
180: .get(index);
181:
182: if (satisfierNode instanceof Element) {
183: Element satisfierElement = (Element) satisfierNode;
184: Attribute nameAttribute = satisfierElement
185: .attribute("name");
186: Attribute xmiidAttribute = satisfierElement
187: .attribute("xmiid");
188: Attribute projectIdAttribute = satisfierElement
189: .attribute("projectid");
190: Attribute projectNameAttribute = satisfierElement
191: .attribute("projectname");
192:
193: String name = "";
194: if (nameAttribute != null)
195: name = nameAttribute.getValue();
196:
197: String xmiid = "";
198: if (xmiidAttribute != null)
199: xmiid = xmiidAttribute
200: .getValue();
201:
202: String projectId = "";
203: if (projectIdAttribute != null)
204: projectId = projectIdAttribute
205: .getValue();
206:
207: String projectName = "";
208: if (projectNameAttribute != null)
209: projectName = projectNameAttribute
210: .getValue();
211:
212: ISatisfier satisfier = new Satisfier();
213:
214: satisfier.setName(name);
215: satisfier.setXMIID(xmiid);
216: satisfier.setProjectID(projectId);
217: satisfier
218: .setProjectName(projectName);
219:
220: satisfiers.add(satisfier);
221:
222: }
223: }
224:
225: requirement.setSatisfiers(satisfiers);
226: }
227: }
228: }
229: }
230: }
231: }
232: }
|