| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.export.services; |
| |
| import java.io.File; |
| import java.io.UnsupportedEncodingException; |
| import java.net.URLDecoder; |
| import java.util.List; |
| import java.util.regex.Matcher; |
| import java.util.regex.Pattern; |
| |
| import org.eclipse.epf.common.utils.XMLUtil; |
| import org.w3c.dom.Document; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.Node; |
| import org.w3c.dom.NodeList; |
| |
| |
| /** |
| * Encapsulates a method library using a DOM document. |
| * |
| * @author Jinhua Xi |
| * @since 1.0 |
| */ |
| public class LibraryDocument { |
| |
| public static final String TAG_methodPlugins = "methodPlugins"; //$NON-NLS-1$ |
| |
| public static final String TAG_predefinedConfigurations = "predefinedConfigurations"; //$NON-NLS-1$ |
| |
| public static final String TAG_resourceDescriptors = "resourceDescriptors"; //$NON-NLS-1$ |
| |
| public static final String TAG_resourceSubManagers = "subManagers"; //$NON-NLS-1$ |
| |
| public static final String ATTR_href = "href"; //$NON-NLS-1$ |
| |
| public static final String ATTR_id = "id"; //$NON-NLS-1$ |
| |
| public static final String ATTR_uri = "uri"; //$NON-NLS-1$ |
| |
| public static final String ATTR_guid = "guid"; //$NON-NLS-1$ |
| |
| public static final String exportFile = "export.xmi"; //$NON-NLS-1$ |
| |
| public static final String libraryFile = "library.xmi"; //$NON-NLS-1$ |
| |
| protected File libFile; |
| |
| protected Document document; |
| |
| protected Element libTag = null; |
| |
| protected Element resTag = null; |
| |
| public LibraryDocument(File libFile) throws Exception { |
| this.libFile = libFile; |
| init(); |
| } |
| |
| private void init() throws Exception { |
| this.document = XMLUtil.loadXml(libFile); |
| |
| Element root = document.getDocumentElement(); |
| NodeList nodes = root.getElementsByTagName("com.ibm.uma:MethodLibrary"); //$NON-NLS-1$ |
| if (nodes != null && nodes.getLength() > 0) { |
| libTag = (Element) nodes.item(0); |
| } |
| |
| nodes = root |
| .getElementsByTagName("com.ibm.uma.resourcemanager:ResourceManager"); //$NON-NLS-1$ |
| if (nodes != null && nodes.getLength() > 0) { |
| resTag = (Element) nodes.item(0); |
| } |
| } |
| |
| public Document getDocument() { |
| return this.document; |
| } |
| |
| public File getFile() { |
| return libFile; |
| } |
| |
| public Element getLibTag() { |
| return libTag; |
| } |
| |
| public String getLibraryName() { |
| return libTag.getAttribute("name"); //$NON-NLS-1$ |
| } |
| |
| public String getLibraryGuid() { |
| return libTag.getAttribute("guid"); //$NON-NLS-1$ |
| } |
| |
| public Element getResourceTag() { |
| return resTag; |
| } |
| |
| public void removePlugin(Element node) { |
| libTag.removeChild(node); |
| } |
| |
| public void removeConfiguration(Element node) { |
| libTag.removeChild(node); |
| } |
| |
| public void removeResourceDescriptor(Element node) { |
| resTag.removeChild(node); |
| } |
| |
| public NodeList getPlugins() { |
| return libTag.getElementsByTagName(TAG_methodPlugins); |
| } |
| |
| public NodeList getConfigurations() { |
| return libTag.getElementsByTagName(TAG_predefinedConfigurations); |
| } |
| |
| public NodeList getResourceDescriptors() { |
| return resTag.getElementsByTagName(TAG_resourceDescriptors); |
| } |
| |
| public NodeList getResourceSubManagers() { |
| return resTag.getElementsByTagName(TAG_resourceSubManagers); |
| } |
| |
| public void addPlugin(Element node) { |
| libTag.appendChild(getValidNode(node)); |
| } |
| |
| public void addConfiguration(Element node) { |
| libTag.appendChild(getValidNode(node)); |
| } |
| |
| public void addResource(Element node) { |
| resTag.appendChild(getValidNode(node)); |
| } |
| |
| public Node getValidNode(Node node) { |
| if (node.getOwnerDocument() == document) { |
| return node; |
| } |
| |
| return document.importNode(node, true); |
| } |
| |
| /** |
| * remove plugins by guid |
| * |
| * @param removeList |
| * List a list of guids |
| */ |
| public void removePlugins(List removeList) { |
| // RATLC00382319 - Importing a package of plgins twice generates 2 |
| // identical plugins |
| // remove the node will cause the node list to shrink, so don't increase |
| // the index |
| NodeList nodes = getPlugins(); |
| int i = 0; |
| while (i < nodes.getLength()) { |
| Element node = (Element) nodes.item(i); |
| String guid = node.getAttribute("xmi:id"); //$NON-NLS-1$ |
| if (removeList.contains(guid)) { |
| libTag.removeChild(node); |
| } else { |
| i++; |
| } |
| } |
| } |
| |
| public void removeConfigurations(List removeList) { |
| // remove the unneeded configurations |
| NodeList nodes = getConfigurations(); |
| int i = 0; |
| while (i < nodes.getLength()) { |
| Element node = (Element) nodes.item(i); |
| String guid = node.getAttribute("xmi:id"); //$NON-NLS-1$ |
| if (removeList.contains(guid)) { |
| libTag.removeChild(node); |
| } else { |
| i++; |
| } |
| } |
| } |
| |
| public void removeResourceEntries(List removeList) { |
| NodeList nodes = getResourceDescriptors(); |
| int i = 0; |
| while (i < nodes.getLength()) { |
| Element node = (Element) nodes.item(i); |
| String guid = node.getAttribute(ATTR_id); |
| String uri = node.getAttribute(ATTR_uri); |
| if (removeList.contains(guid)) { |
| resTag.removeChild(node); |
| |
| // check the plugin xmi file, if exists, delete the folder |
| File plugn_file = getFileFromUri(uri); |
| if (plugn_file.exists()) { |
| // delete the folder ??? |
| } |
| } else { |
| i++; |
| } |
| } |
| |
| // also remove the sub managers |
| nodes = getResourceSubManagers(); |
| i = 0; |
| while (i < nodes.getLength()) { |
| Element node = (Element) nodes.item(i); |
| String guid = getSubManagerBaseGuid(node.getAttribute(ATTR_href)); |
| if (removeList.contains(guid)) { |
| resTag.removeChild(node); |
| } else { |
| i++; |
| } |
| } |
| } |
| |
| public File getFileFromUri(String uri) { |
| try { |
| uri = URLDecoder.decode(uri, "UTF-8"); //$NON-NLS-1$ |
| } catch (UnsupportedEncodingException e) { |
| e.printStackTrace(); |
| } |
| int i = uri.indexOf("#"); //$NON-NLS-1$ |
| if (i > 0) { |
| uri = uri.substring(0, i); |
| } |
| |
| return new File(libFile.getParentFile(), uri); |
| } |
| |
| public void save() throws Exception { |
| saveAs(libFile.getAbsolutePath()); |
| } |
| |
| public void saveAs(String filePathName) throws Exception { |
| XMLUtil.saveDocument(this.document, filePathName); |
| } |
| |
| public boolean isConfigSpecsOnly() { |
| NodeList nodes = getResourceDescriptors(); |
| |
| if (nodes == null || nodes.getLength() == 0) { |
| return true; |
| } |
| |
| for (int i = 0; i < nodes.getLength(); i++) { |
| Element node = (Element) nodes.item(i); |
| String uri = node.getAttribute(LibraryDocument.ATTR_uri); |
| |
| // check if the resource files are there |
| File plugn_file = getFileFromUri(uri); |
| if (!plugn_file.exists()) { |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| // static hlper methods ///////////////////////////////////// |
| public static String getSubManagerBaseGuid(String href) { |
| final Pattern p = Pattern.compile( |
| "uma://(.*?)#(.*?)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); //$NON-NLS-1$ |
| Matcher m = p.matcher(href); |
| if (m.find()) { |
| return m.group(1); |
| } |
| |
| return href; |
| } |
| |
| public static String getChildValue(Element tag, String childTagName) { |
| NodeList nodes = tag.getChildNodes(); |
| if (nodes == null || nodes.getLength() == 0) { |
| return ""; //$NON-NLS-1$ |
| } |
| |
| int size = nodes.getLength(); |
| for (int i = 0; i < size; i++) { |
| Node node = nodes.item(i); |
| if ((node instanceof Element) |
| && ((Element) node).getTagName().equals(childTagName)) { |
| return getNodeText((Element) node); |
| } |
| } |
| |
| return ""; //$NON-NLS-1$ |
| } |
| |
| /** |
| * text of a leaf node, without child element |
| * |
| * @param tag |
| * @return |
| */ |
| public static String getNodeText(Element tag) { |
| |
| NodeList nodes = tag.getChildNodes(); |
| if (nodes == null || nodes.getLength() == 0) { |
| return ""; //$NON-NLS-1$ |
| } |
| |
| int size = nodes.getLength(); |
| StringBuffer buffer = new StringBuffer(); |
| for (int i = 0; i < size; i++) { |
| Node node = nodes.item(i); |
| if (node.getNodeType() == Node.TEXT_NODE) { |
| buffer.append(node.getNodeValue()); |
| } |
| } |
| |
| return buffer.toString(); |
| } |
| |
| public static String getGuidFromHref(String href) { |
| int i = href.indexOf("#"); //$NON-NLS-1$ |
| if (i > 0) { |
| return href.substring(i + 1); |
| } |
| |
| return href; |
| } |
| |
| public ConfigurationSpec getConfigurationSpec(Element config) { |
| ConfigurationSpec spec = new ConfigurationSpec(); |
| spec.guid = config.getAttribute("xmi:id"); //$NON-NLS-1$ |
| spec.name = config.getAttribute("name"); //$NON-NLS-1$ |
| spec.brief_desc = config.getAttribute("briefDescription"); //$NON-NLS-1$ |
| |
| // get plugins |
| NodeList nodes = config.getElementsByTagName("methodPluginSelection"); //$NON-NLS-1$ |
| if (nodes != null) { |
| for (int i = 0; i < nodes.getLength(); i++) { |
| Element node = (Element) nodes.item(i); |
| String guid = getGuidFromHref(node.getAttribute(ATTR_href)); |
| spec.pluginIds.add(guid); |
| } |
| } |
| |
| // get packages |
| nodes = config.getElementsByTagName("methodPackageSelection"); //$NON-NLS-1$ |
| if (nodes != null) { |
| for (int i = 0; i < nodes.getLength(); i++) { |
| Element node = (Element) nodes.item(i); |
| String guid = getGuidFromHref(node.getAttribute(ATTR_href)); |
| spec.packageIds.add(guid); |
| } |
| } |
| |
| // get views |
| nodes = config.getElementsByTagName("processViews"); //$NON-NLS-1$ |
| if (nodes != null) { |
| for (int i = 0; i < nodes.getLength(); i++) { |
| Element node = (Element) nodes.item(i); |
| String guid = getGuidFromHref(node.getAttribute(ATTR_href)); |
| spec.viewIds.add(guid); |
| } |
| } |
| |
| return spec; |
| } |
| } |