Author: rob.stryker(a)jboss.com
Date: 2012-08-25 05:35:49 -0400 (Sat, 25 Aug 2012)
New Revision: 43234
Added:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainer.java
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainerInitializer.java
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorations.java
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorationsManager.java
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/IMemento.java
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/XMLMemento.java
Modified:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/Messages.java
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/messages.properties
Log:
JBIDE-12481 add some classes from jmx / as.classpath to common
Modified:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/Messages.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/Messages.java 2012-08-25
06:14:56 UTC (rev 43233)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/Messages.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -20,6 +20,9 @@
public static String SAXValidator_UnableToInstantiateMessage;
public static String XMLUtilities_IOExceptionMessage;
public static String XMLUtilities_SAXExceptionMessage;
+ public static String AbstractClasspathContainer_error_loading_container;
+ public static String ClasspathDecorationsManager_unexpected_exception;
+
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Copied:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainer.java
(from rev 43203,
trunk/as/plugins/org.jboss.ide.eclipse.as.classpath.core/src/org/jboss/ide/eclipse/as/classpath/core/jee/AbstractClasspathContainer.java)
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainer.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainer.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -0,0 +1,178 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+
+package org.jboss.tools.common.core.classpath;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.IAccessRule;
+import org.eclipse.jdt.core.IClasspathAttribute;
+import org.eclipse.jdt.core.IClasspathContainer;
+import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.jboss.tools.common.core.CommonCorePlugin;
+import org.jboss.tools.common.core.Messages;
+
+/**
+ *
+ * @author Rob Stryker <rob.stryker(a)redhat.com>
+ *
+ */
+public abstract class AbstractClasspathContainer implements IClasspathContainer {
+
+ public static final String LIB_FOLDER = "lib"; //$NON-NLS-1$
+ public static final String LIB_SOURCE_FOLDER = "libsrc"; //$NON-NLS-1$
+ public final static String CLASSPATH_CONTAINER_PREFIX =
"org.jboss.ide.eclipse.as.classpath.core"; //$NON-NLS-1$
+
+ protected IClasspathEntry[] entries;
+ protected IPath path;
+ protected String description;
+ protected String libFolder;
+ protected IJavaProject javaProject;
+
+ protected static ClasspathDecorationsManager decorations;
+ static {
+
+ decorations = new ClasspathDecorationsManager();
+ }
+
+ public AbstractClasspathContainer(IPath path, String description,
+ String libFolder, IJavaProject project) {
+ this.path = path;
+ this.description = description;
+ this.libFolder = libFolder;
+ this.javaProject = project;
+ }
+
+ public IClasspathEntry[] getClasspathEntries() {
+ if (entries == null) {
+ entries = computeEntries();
+ }
+ return entries;
+ }
+
+ public String getDescription() {
+ return this.description;
+ }
+
+ public int getKind() {
+ return IClasspathContainer.K_APPLICATION;
+ }
+
+ public IPath getPath() {
+ return this.path;
+ }
+
+ protected IClasspathEntry[] computeEntries() {
+ ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
+
+ String baseDir = getBaseDir();
+ if (baseDir == null)
+ return new IClasspathEntry[0];
+
+ File libDir = new File(baseDir
+ + "/" + LIB_FOLDER + "/" + getLibFolder());//$NON-NLS-1$
//$NON-NLS-2$
+ File libSrcDir = new File(baseDir
+ + "/" + LIB_SOURCE_FOLDER + "/" + getLibFolder());//$NON-NLS-1$
//$NON-NLS-2$
+
+ // Lists every modules in the lib dir
+ File[] jars = libDir.listFiles(new FileFilter() {
+ public boolean accept(File file) {
+ return (file.toString().endsWith(".jar"));//$NON-NLS-1$
+ }
+ });
+
+ if (jars != null) {
+ for (int i = 0; i < jars.length; i++) {
+ File jarFile = jars[i];
+ String jarFileName = jarFile.getName();
+ File jarSrcFile = new File(libSrcDir, jarFileName);
+
+ IPath entryPath = new Path(jarFile.toString());
+
+ IPath sourceAttachementPath = null;
+ IPath sourceAttachementRootPath = null;
+
+ final ClasspathDecorations dec
+ = decorations.getDecorations( getDecorationManagerKey(getPath().toString()),
entryPath.toString() );
+
+
+ IClasspathAttribute[] attrs = {};
+ if( dec != null ) {
+ sourceAttachementPath = dec.getSourceAttachmentPath();
+ sourceAttachementRootPath = dec.getSourceAttachmentRootPath();
+ attrs = dec.getExtraAttributes();
+ } else if (jarSrcFile.exists()) {
+ sourceAttachementPath = new Path(jarSrcFile.toString());
+ sourceAttachementRootPath = new Path("/");//$NON-NLS-1$
+ }
+
+ IAccessRule[] access = {};
+ IClasspathEntry entry = JavaCore.newLibraryEntry( entryPath, sourceAttachementPath,
+ sourceAttachementRootPath, access, attrs, false );
+ entries.add(entry);
+ }
+ }
+
+ return entries.toArray(new IClasspathEntry[entries.size()]);
+ }
+
+ protected String getLibFolder() {
+ return this.libFolder;
+ }
+
+ protected String getBaseDir() {
+ try {
+ URL installURL = FileLocator.toFileURL(CommonCorePlugin
+ .getDefault().getBundle().getEntry("/")); //$NON-NLS-1$
+ return installURL.getFile().toString();
+ } catch (IOException ioe) {
+ // LOG THE ERROR (one day)
+ IStatus status = new Status(IStatus.ERROR, CommonCorePlugin.PLUGIN_ID,
+ Messages.AbstractClasspathContainer_error_loading_container, ioe);
+ CommonCorePlugin.getDefault().getLog().log(status);
+ }
+ return null;
+ }
+
+ public static String getDecorationManagerKey( String container){
+ return container;
+ }
+
+ protected static ClasspathDecorationsManager getDecorationsManager() {
+ return decorations;
+ }
+
+ public void install() {
+ entries = computeEntries();
+ IJavaProject[] javaProjects = new IJavaProject[] {javaProject};
+ final IClasspathContainer[] conts = new IClasspathContainer[] { this };
+ try {
+ JavaCore.setClasspathContainer(path, javaProjects, conts, null);
+ } catch (CoreException e) {
+ CommonCorePlugin.getDefault().logError(e);
+ }
+ }
+
+ public abstract void refresh();
+
+}
Copied:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainerInitializer.java
(from rev 43203,
trunk/as/plugins/org.jboss.ide.eclipse.as.classpath.core/src/org/jboss/ide/eclipse/as/classpath/core/jee/AbstractClasspathContainerInitializer.java)
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainerInitializer.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/AbstractClasspathContainerInitializer.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -0,0 +1,115 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+
+package org.jboss.tools.common.core.classpath;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.core.ClasspathContainerInitializer;
+import org.eclipse.jdt.core.IClasspathAttribute;
+import org.eclipse.jdt.core.IClasspathContainer;
+import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+
+/**
+ *
+ * @author Rob Stryker <rob.stryker(a)redhat.com>
+ *
+ */
+public abstract class AbstractClasspathContainerInitializer extends
+ ClasspathContainerInitializer {
+
+ protected IJavaProject javaProject;
+ public AbstractClasspathContainerInitializer() {
+ }
+
+ /**
+ * Description of the Method
+ *
+ * @param containerPath
+ * Description of the Parameter
+ * @param project
+ * Description of the Parameter
+ * @exception CoreException
+ * Description of the Exception
+ */
+ public void initialize(IPath containerPath, IJavaProject project)
+ throws CoreException {
+ this.javaProject = project;
+ int size = containerPath.segmentCount();
+ if (size > 0) {
+ AbstractClasspathContainer container = createClasspathContainer(containerPath);
+ JavaCore.setClasspathContainer(containerPath,
+ new IJavaProject[] { project },
+ new IClasspathContainer[] { container }, null);
+ }
+ }
+
+ /**
+ * Description of the Method
+ *
+ * @param path
+ * Description of the Parameter
+ * @return Description of the Return Value
+ */
+ protected abstract AbstractClasspathContainer createClasspathContainer(
+ IPath path);
+
+ /**
+ * Gets the classpathContainerID attribute of the
+ * AbstractClasspathContainerInitializer object
+ *
+ * @return The classpathContainerID value
+ */
+ protected abstract String getClasspathContainerID();
+
+ public boolean canUpdateClasspathContainer(IPath containerPath,
+ IJavaProject project) {
+ return true;
+ }
+
+ public void requestClasspathContainerUpdate(final IPath containerPath,
+ final IJavaProject project, final IClasspathContainer sg)
+
+ throws CoreException
+
+ {
+ String key = AbstractClasspathContainer
+ .getDecorationManagerKey(containerPath.toString());
+
+ IClasspathEntry[] entries = sg.getClasspathEntries();
+ ClasspathDecorationsManager decorations = AbstractClasspathContainer
+ .getDecorationsManager();
+ decorations.clearAllDecorations(key);
+
+ for (int i = 0; i < entries.length; i++) {
+ final IClasspathEntry entry = entries[i];
+
+ final IPath srcpath = entry.getSourceAttachmentPath();
+ final IPath srcrootpath = entry.getSourceAttachmentRootPath();
+ final IClasspathAttribute[] attrs = entry.getExtraAttributes();
+ final String eid = entry.getPath().toString();
+ final ClasspathDecorations dec = new ClasspathDecorations();
+
+ dec.setSourceAttachmentPath(srcpath);
+ dec.setSourceAttachmentRootPath(srcrootpath);
+ dec.setExtraAttributes(attrs);
+
+ decorations.setDecorations(key, eid, dec);
+ }
+ decorations.save();
+ final IClasspathContainer container = JavaCore.getClasspathContainer(
+ containerPath, project);
+ ((AbstractClasspathContainer) container).refresh();
+ }
+
+}
Copied:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorations.java
(from rev 43203,
trunk/as/plugins/org.jboss.ide.eclipse.as.classpath.core/src/org/jboss/ide/eclipse/as/classpath/core/xpl/ClasspathDecorations.java)
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorations.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorations.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -0,0 +1,65 @@
+/******************************************************************************
+ * Copyright (c) 2005 BEA Systems, Inc.
+ * 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:
+ * Konstantin Komissarchik - initial API and implementation
+ ******************************************************************************/
+
+package org.jboss.tools.common.core.classpath;
+
+import java.util.ArrayList;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.core.IClasspathAttribute;
+import org.eclipse.jdt.core.JavaCore;
+
+/**
+ * @author <a href="mailto:kosta@bea.com">Konstantin
Komissarchik</a>
+ */
+
+public final class ClasspathDecorations {
+ private IPath sourceAttachmentPath;
+ private IPath sourceAttachmentRootPath;
+ private ArrayList extraAttributes = new ArrayList();
+
+ public IPath getSourceAttachmentPath() {
+ return this.sourceAttachmentPath;
+ }
+
+ public void setSourceAttachmentPath(final IPath sourceAttachmentPath) {
+ this.sourceAttachmentPath = sourceAttachmentPath;
+ }
+
+ public IPath getSourceAttachmentRootPath() {
+ return this.sourceAttachmentRootPath;
+ }
+
+ public void setSourceAttachmentRootPath(final IPath sourceAttachmentRootPath) {
+ this.sourceAttachmentRootPath = sourceAttachmentRootPath;
+ }
+
+ public IClasspathAttribute[] getExtraAttributes() {
+ final IClasspathAttribute[] array = new IClasspathAttribute[this.extraAttributes
+ .size()];
+
+ return (IClasspathAttribute[]) this.extraAttributes.toArray(array);
+ }
+
+ public void setExtraAttributes(final IClasspathAttribute[] attrs) {
+ for (int i = 0; i < attrs.length; i++) {
+ this.extraAttributes.add(attrs[i]);
+ }
+ }
+
+ public void addExtraAttribute(final String name, final String value) {
+ final IClasspathAttribute attr = JavaCore.newClasspathAttribute(name,
+ value);
+
+ this.extraAttributes.add(attr);
+ }
+
+}
Copied:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorationsManager.java
(from rev 43203,
trunk/as/plugins/org.jboss.ide.eclipse.as.classpath.core/src/org/jboss/ide/eclipse/as/classpath/core/xpl/ClasspathDecorationsManager.java)
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorationsManager.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/classpath/ClasspathDecorationsManager.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -0,0 +1,316 @@
+/******************************************************************************
+ * Copyright (c) 2005 BEA Systems, Inc.
+ * 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:
+ * Konstantin Komissarchik - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.common.core.classpath;
+
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.jdt.core.IClasspathAttribute;
+import org.jboss.tools.common.core.CommonCorePlugin;
+import org.jboss.tools.common.core.Messages;
+import org.osgi.service.prefs.BackingStoreException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+
+/**
+ * @author <a href="mailto:kosta@bea.com">Konstantin
Komissarchik</a>
+ */
+
+public final class ClasspathDecorationsManager {
+ private static final String CLASSPATH_PREFERENCES = "classpathPreferences";
//$NON-NLS-1$
+ private static final String SEPARATOR = System
+ .getProperty("line.separator"); //$NON-NLS-1$
+ private final HashMap decorations;
+
+ public ClasspathDecorationsManager() {
+ this.decorations = read();
+ }
+
+ private IEclipsePreferences getEclipsePreferences() {
+ IEclipsePreferences node = (IEclipsePreferences) Platform
+ .getPreferencesService().getRootNode()
+ .node(InstanceScope.SCOPE).node(CommonCorePlugin.PLUGIN_ID);
+ return node;
+ }
+
+ private String getPreferences() {
+ return getEclipsePreferences().get(CLASSPATH_PREFERENCES, null);
+ }
+
+ public ClasspathDecorations getDecorations(final String key,
+ final String entry) {
+ final HashMap submap = (HashMap) this.decorations.get(key);
+
+ if (submap == null) {
+ return null;
+ }
+
+ return (ClasspathDecorations) submap.get(entry);
+ }
+
+ public void setDecorations(final String key, final String entry,
+ final ClasspathDecorations dec) {
+ HashMap submap = (HashMap) this.decorations.get(key);
+
+ if (submap == null) {
+ submap = new HashMap();
+ this.decorations.put(key, submap);
+ }
+
+ submap.put(entry, dec);
+ }
+
+ public void clearAllDecorations(final String key) {
+ this.decorations.remove(key);
+ }
+
+ public void save() {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("<classpath>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+ for (Iterator itr1 = decorations.entrySet().iterator(); itr1.hasNext();) {
+ final Map.Entry entry1 = (Map.Entry) itr1.next();
+ final Map submap = (Map) entry1.getValue();
+
+ buffer.append(" <container id=\""); //$NON-NLS-1$
+ buffer.append((String) entry1.getKey());
+ buffer.append("\">"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+
+ for (Iterator itr2 = submap.entrySet().iterator(); itr2.hasNext();) {
+ final Map.Entry entry2 = (Map.Entry) itr2.next();
+
+ final ClasspathDecorations dec = (ClasspathDecorations) entry2
+ .getValue();
+
+ buffer.append(" <entry id=\""); //$NON-NLS-1$
+ buffer.append((String) entry2.getKey());
+ buffer.append("\">"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+
+ String src = ""; //$NON-NLS-1$
+ if (dec.getSourceAttachmentPath() != null) {
+ src = dec.getSourceAttachmentPath().toString();
+ }
+ buffer.append(" <source-attachment-path>"); //$NON-NLS-1$
+ buffer.append(src);
+ buffer.append("</source-attachment-path>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+
+ if (dec.getSourceAttachmentRootPath() != null) {
+ buffer.append(" <source-attachment-root-path>"); //$NON-NLS-1$
+ buffer.append(dec.getSourceAttachmentRootPath().toString());
+ buffer.append("</source-attachment-root-path>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+ }
+
+ final IClasspathAttribute[] attrs = dec.getExtraAttributes();
+
+ for (int i = 0; i < attrs.length; i++) {
+ final IClasspathAttribute attr = attrs[i];
+
+ buffer.append(" <attribute name=\""); //$NON-NLS-1$
+ buffer.append(attr.getName());
+ buffer.append("\">"); //$NON-NLS-1$
+ buffer.append(attr.getValue());
+ buffer.append("</attribute>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+ }
+
+ buffer.append(" </entry>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+ }
+
+ buffer.append(" </container>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+ }
+
+ buffer.append("</classpath>"); //$NON-NLS-1$
+ buffer.append(SEPARATOR);
+ IEclipsePreferences ep = getEclipsePreferences();
+ ep.put(CLASSPATH_PREFERENCES, buffer.toString());
+ try {
+ ep.flush();
+ } catch (BackingStoreException e) {
+ String msg = Messages.ClasspathDecorationsManager_unexpected_exception;
+ CommonCorePlugin.getDefault().logError(msg, e);
+ }
+ }
+
+ private HashMap read() {
+ final HashMap map = new HashMap();
+ String prefs = getPreferences();
+ if (prefs == null || prefs.length() <= 0)
+ return map;
+
+ Element root = null;
+ try {
+ final DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
+
+ final DocumentBuilder docbuilder = factory.newDocumentBuilder();
+
+ StringReader reader = new StringReader(prefs);
+ InputSource source = new InputSource(reader);
+ root = docbuilder.parse(source).getDocumentElement();
+ } catch (Exception e) {
+ String msg = Messages.ClasspathDecorationsManager_unexpected_exception;
+ CommonCorePlugin.getDefault().logError(msg, e);
+ return map;
+ }
+
+ for (Iterator itr1 = elements(root, "container"); itr1.hasNext();)
//$NON-NLS-1$
+ {
+ final Element e1 = (Element) itr1.next();
+ final String cid = e1.getAttribute("id"); //$NON-NLS-1$
+
+ final HashMap submap = new HashMap();
+ map.put(cid, submap);
+
+ for (Iterator itr2 = elements(e1, "entry"); itr2.hasNext();) //$NON-NLS-1$
+ {
+ final Element e2 = (Element) itr2.next();
+ final String eid = e2.getAttribute("id"); //$NON-NLS-1$
+ final ClasspathDecorations dec = new ClasspathDecorations();
+
+ submap.put(eid, dec);
+
+ for (Iterator itr3 = elements(e2); itr3.hasNext();) {
+ final Element e3 = (Element) itr3.next();
+ final String n = e3.getNodeName();
+ String text = text(e3);
+ if (text != null) {
+ if (n.equals("source-attachment-path")) //$NON-NLS-1$
+ {
+ dec.setSourceAttachmentPath(new Path(text(e3)));
+ } else if (n.equals("source-attachment-root-path")) //$NON-NLS-1$
+ {
+ dec.setSourceAttachmentRootPath(new Path(text(e3)));
+ }
+ }
+ if (n.equals("attribute")) //$NON-NLS-1$
+ {
+ final String name = e3.getAttribute("name"); //$NON-NLS-1$
+ dec.addExtraAttribute(name, text(e3));
+ }
+
+ }
+ }
+ }
+
+ return map;
+ }
+
+ private static String text(final Element el) {
+ final NodeList nodes = el.getChildNodes();
+
+ String str = null;
+ StringBuffer buf = null;
+
+ for (int i = 0, n = nodes.getLength(); i < n; i++) {
+ final Node node = nodes.item(i);
+
+ if (node.getNodeType() == Node.TEXT_NODE) {
+ final String val = node.getNodeValue();
+
+ if (buf != null) {
+ buf.append(val);
+ } else if (str != null) {
+ buf = new StringBuffer();
+ buf.append(str);
+ buf.append(val);
+
+ str = null;
+ } else {
+ str = val;
+ }
+ }
+ }
+
+ if (buf != null) {
+ return buf.toString();
+ }
+ return str;
+ }
+
+ private static Iterator elements(final Element el, final String name) {
+ return new ElementsIterator(el, name);
+ }
+
+ private static Iterator elements(final Element el) {
+ return new ElementsIterator(el, null);
+ }
+
+ private static final class ElementsIterator implements Iterator {
+ private final NodeList nodes;
+ private final int length;
+ private final String name;
+ private int position;
+ private Element element;
+
+ public ElementsIterator(final Element parent, final String name) {
+ this.nodes = parent.getChildNodes();
+ this.length = nodes.getLength();
+ this.position = -1;
+ this.name = name;
+
+ advance();
+ }
+
+ private void advance() {
+ this.element = null;
+ this.position++;
+
+ for (; this.position < this.length && this.element == null;
this.position++) {
+ final Node node = this.nodes.item(this.position);
+
+ if (node.getNodeType() == Node.ELEMENT_NODE
+ && (this.name == null || node.getNodeName().equals(
+ this.name))) {
+ this.element = (Element) node;
+ }
+ }
+ }
+
+ public boolean hasNext() {
+ return (this.element != null);
+ }
+
+ public Object next() {
+ final Element el = this.element;
+
+ if (el == null) {
+ throw new NoSuchElementException();
+ }
+
+ advance();
+
+ return el;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+}
Modified:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/messages.properties
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/messages.properties 2012-08-25
06:14:56 UTC (rev 43233)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/core/messages.properties 2012-08-25
09:35:49 UTC (rev 43234)
@@ -3,3 +3,5 @@
SAXValidator_UnableToInstantiateMessage=error: Unable to instantiate parser ({0})
XMLUtilities_IOExceptionMessage=Unexpected parser error
XMLUtilities_SAXExceptionMessage=Unexpected parser error
+AbstractClasspathContainer_error_loading_container=Error loading classpath container
+ClasspathDecorationsManager_unexpected_exception=Encountered an unexpected exception.
Copied:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/IMemento.java
(from rev 43203,
trunk/jmx/plugins/org.jboss.tools.jmx.core/src/org/jboss/tools/jmx/core/IMemento.java)
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/IMemento.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/IMemento.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * Copyright (c) 2006
+ * 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
+ *******************************************************************************/
+package org.jboss.tools.common.xml;
+
+import java.util.List;
+
+/**
+ * Stolen from webtools wst.server.core,
+ * represents a savable memento to be
+ * translated into xml
+ *
+ */
+public interface IMemento {
+ /**
+ * Creates a new child of this memento with the given type.
+ * <p>
+ * The <code>getChild</code> and <code>getChildren</code>
methods
+ * are used to retrieve children of a given type.
+ * </p>
+ *
+ * @param type the type
+ * @return a new child memento
+ * @see #getChild
+ * @see #getChildren
+ */
+ public IMemento createChild(String type);
+
+ /**
+ * Returns the first child with the given type id.
+ *
+ * @param type the type id
+ * @return the first child with the given type
+ */
+ public IMemento getChild(String type);
+
+ /**
+ * Returns all children with the given type id.
+ *
+ * @param type the type id
+ * @return the list of children with the given type
+ */
+ public IMemento[] getChildren(String type);
+
+ /**
+ * Returns the floating point value of the given key.
+ *
+ * @param key the key
+ * @return the value, or <code>null</code> if the key was not found or was
found
+ * but was not a floating point number
+ */
+ public Float getFloat(String key);
+
+ /**
+ * Returns the integer value of the given key.
+ *
+ * @param key the key
+ * @return the value, or <code>null</code> if the key was not found or was
found
+ * but was not an integer
+ */
+ public Integer getInteger(String key);
+
+ /**
+ * Returns the string value of the given key.
+ *
+ * @param key the key
+ * @return the value, or <code>null</code> if the key was not found or was
found
+ * but was not an integer
+ */
+ public String getString(String key);
+
+ /**
+ * Returns the boolean value of the given key.
+ *
+ * @param key the key
+ * @return the value, or <code>null</code> if the key was not found or was
found
+ * but was not a boolean
+ */
+ public Boolean getBoolean(String key);
+
+ public List<String> getNames();
+
+ /**
+ * Sets the value of the given key to the given integer.
+ *
+ * @param key the key
+ * @param value the value
+ */
+ public void putInteger(String key, int value);
+
+ /**
+ * Sets the value of the given key to the given boolean value.
+ *
+ * @param key the key
+ * @param value the value
+ */
+ public void putBoolean(String key, boolean value);
+
+ /**
+ * Sets the value of the given key to the given string.
+ *
+ * @param key the key
+ * @param value the value
+ */
+ public void putString(String key, String value);
+}
Copied:
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/XMLMemento.java
(from rev 43203,
trunk/jmx/plugins/org.jboss.tools.jmx.core/src/org/jboss/tools/jmx/core/util/XMLMemento.java)
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/XMLMemento.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.core/src/org/jboss/tools/common/xml/XMLMemento.java 2012-08-25
09:35:49 UTC (rev 43234)
@@ -0,0 +1,430 @@
+/*******************************************************************************
+ * Copyright (c) 2006
+ * 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
+ *******************************************************************************/
+package org.jboss.tools.common.xml;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.InputSource;
+
+/**
+ * Stolen from webtools wst.server.core
+ *
+ */
+public final class XMLMemento implements IMemento {
+ private Document factory;
+ private Element element;
+
+ /**
+ * Answer a memento for the document and element. For simplicity
+ * you should use createReadRoot and createWriteRoot to create the initial
+ * mementos on a document.
+ */
+ public XMLMemento(Document doc, Element el) {
+ factory = doc;
+ element = el;
+ }
+
+ /*
+ * @see IMemento
+ */
+ public IMemento createChild(String type) {
+ Element child = factory.createElement(type);
+ element.appendChild(child);
+ return new XMLMemento(factory, child);
+ }
+
+ public void removeChild(XMLMemento child) {
+ element.removeChild(child.element);
+ }
+
+ /**
+ * Create a Document from a Reader and answer a root memento for reading
+ * a document.
+ */
+ public static XMLMemento createReadRoot(InputStream in) {
+ Document document = null;
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder parser = factory.newDocumentBuilder();
+ document = parser.parse(new InputSource(in));
+ Node node = document.getFirstChild();
+ if (node instanceof Element)
+ return new XMLMemento(document, (Element) node);
+ } catch (Exception e) {
+ // ignore
+ } finally {
+ try {
+ in.close();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Answer a root memento for writing a document.
+ *
+ * @param type a type
+ * @return a memento
+ */
+ public static XMLMemento createWriteRoot(String type) {
+ Document document;
+ try {
+ document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ Element element = document.createElement(type);
+ document.appendChild(element);
+ return new XMLMemento(document, element);
+ } catch (ParserConfigurationException e) {
+ throw new Error(e);
+ }
+ }
+
+ /*
+ * @see IMemento
+ */
+ public IMemento getChild(String type) {
+ // Get the nodes.
+ NodeList nodes = element.getChildNodes();
+ int size = nodes.getLength();
+ if (size == 0)
+ return null;
+
+ // Find the first node which is a child of this node.
+ for (int nX = 0; nX < size; nX ++) {
+ Node node = nodes.item(nX);
+ if (node instanceof Element) {
+ Element element2 = (Element)node;
+ if (element2.getNodeName().equals(type))
+ return new XMLMemento(factory, element2);
+ }
+ }
+
+ // A child was not found.
+ return null;
+ }
+
+ /*
+ * @see IMemento
+ */
+ public IMemento [] getChildren(String type) {
+ // Get the nodes.
+ NodeList nodes = element.getChildNodes();
+ int size = nodes.getLength();
+ if (size == 0)
+ return new IMemento[0];
+
+ // Extract each node with given type.
+ List<Element> list = new ArrayList<Element>(size);
+ for (int nX = 0; nX < size; nX ++) {
+ Node node = nodes.item(nX);
+ if (node instanceof Element) {
+ Element element2 = (Element)node;
+ if (element2.getNodeName().equals(type))
+ list.add(element2);
+ }
+ }
+
+ // Create a memento for each node.
+ size = list.size();
+ IMemento [] results = new IMemento[size];
+ for (int x = 0; x < size; x ++) {
+ results[x] = new XMLMemento(factory, list.get(x));
+ }
+ return results;
+ }
+
+ public String[] getChildNames() {
+ // Get the nodes.
+ NodeList nodes = element.getChildNodes();
+ int size = nodes.getLength();
+ if (size == 0)
+ return new String[0];
+
+ // Extract each node with given type.
+ List<String> list = new ArrayList<String>();
+ for (int nX = 0; nX < size; nX ++) {
+ Node node = nodes.item(nX);
+ if (node instanceof Element) {
+ Element element2 = (Element)node;
+ if (!list.contains(element2.getNodeName()))
+ list.add(element2.getNodeName());
+ }
+ }
+ return (String[]) list.toArray(new String[list.size()]);
+ }
+
+ /**
+ * Return the contents of this memento as a byte array.
+ *
+ * @return byte[]
+ * @throws IOException if anything goes wrong
+ */
+ public byte[] getContents() throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ save(out);
+ return out.toByteArray();
+ }
+
+ /**
+ * Returns an input stream for writing to the disk with a local locale.
+ *
+ * @return java.io.InputStream
+ * @throws IOException if anything goes wrong
+ */
+ public InputStream getInputStream() throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ save(out);
+ return new ByteArrayInputStream(out.toByteArray());
+ }
+
+ /*
+ * @see IMemento
+ */
+ public Float getFloat(String key) {
+ Attr attr = element.getAttributeNode(key);
+ if (attr == null)
+ return null;
+ String strValue = attr.getValue();
+ try {
+ return new Float(strValue);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
+ /*
+ * @see IMemento
+ */
+ public Integer getInteger(String key) {
+ Attr attr = element.getAttributeNode(key);
+ if (attr == null)
+ return null;
+ String strValue = attr.getValue();
+ try {
+ return new Integer(strValue);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
+ /*
+ * @see IMemento
+ */
+ public String getString(String key) {
+ Attr attr = element.getAttributeNode(key);
+ if (attr == null)
+ return null;
+ return attr.getValue();
+ }
+
+ public List<String> getNames() {
+ NamedNodeMap map = element.getAttributes();
+ int size = map.getLength();
+ List<String> list = new ArrayList<String>();
+ for (int i = 0; i < size; i++) {
+ Node node = map.item(i);
+ String name = node.getNodeName();
+ list.add(name);
+ }
+ return list;
+ }
+
+ /**
+ * Loads a memento from the given filename.
+ *
+ * @param in java.io.InputStream
+ * @return org.eclipse.ui.IMemento
+ */
+ public static IMemento loadMemento(InputStream in) {
+ return createReadRoot(in);
+ }
+
+ /**
+ * Loads a memento from the given filename.
+ *
+ * @param filename java.lang.String
+ * @return org.eclipse.ui.IMemento
+ * @exception java.io.IOException
+ */
+ public static IMemento loadMemento(String filename) throws IOException {
+ InputStream in = null;
+ try {
+ in = new BufferedInputStream(new FileInputStream(filename));
+ return XMLMemento.createReadRoot(in);
+ } finally {
+ try {
+ if (in != null)
+ in.close();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ }
+
+ /*
+ * @see IMemento
+ */
+ public void putInteger(String key, int n) {
+ element.setAttribute(key, String.valueOf(n));
+ }
+
+ /*
+ * @see IMemento
+ */
+ public void putString(String key, String value) {
+ if (value == null)
+ return;
+ element.setAttribute(key, value);
+ }
+
+ /**
+ * Save this Memento to a Writer.
+ *
+ * @throws IOException if there is a problem saving
+ */
+ public void save(OutputStream os) throws IOException {
+ Result result = new StreamResult(os);
+ Source source = new DOMSource(factory);
+ try {
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
+ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
+
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-a...;,
"2"); //$NON-NLS-1$ //$NON-NLS-2$
+ transformer.transform(source, result);
+ } catch (Exception e) {
+ throw (IOException) (new IOException().initCause(e));
+ }
+ }
+
+ /**
+ * Saves the memento to the given file.
+ *
+ * @param filename java.lang.String
+ * @exception java.io.IOException
+ */
+ public void saveToFile(String filename) throws IOException {
+ BufferedOutputStream w = null;
+ try {
+ w = new BufferedOutputStream(new FileOutputStream(filename));
+ save(w);
+ } catch (IOException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new IOException(e.getLocalizedMessage());
+ } finally {
+ if (w != null) {
+ try {
+ w.close();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ }
+ }
+
+ public String saveToString() throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ save(out);
+ return out.toString("UTF-8"); //$NON-NLS-1$
+ }
+
+ /*
+ * @see IMemento#getBoolean(String)
+ */
+ public Boolean getBoolean(String key) {
+ Attr attr = element.getAttributeNode(key);
+ if (attr == null)
+ return null;
+ String strValue = attr.getValue();
+ if ("true".equalsIgnoreCase(strValue)) //$NON-NLS-1$
+ return new Boolean(true);
+ return new Boolean(false);
+ }
+
+ /*
+ * @see IMemento#putBoolean(String, boolean)
+ */
+ public void putBoolean(String key, boolean value) {
+ element.setAttribute(key, new Boolean(value).toString());
+ }
+
+ /**
+ * Returns the Text node of the memento. Each memento is allowed only
+ * one Text node.
+ *
+ * @return the Text node of the memento, or <code>null</code> if
+ * the memento has no Text node.
+ */
+ public Text getTextNode() {
+ // Get the nodes.
+ NodeList nodes = element.getChildNodes();
+ int size = nodes.getLength();
+ if (size == 0) {
+ return null;
+ }
+ for (int nX = 0; nX < size; nX++) {
+ Node node = nodes.item(nX);
+ if (node instanceof Text) {
+ return (Text) node;
+ }
+ }
+ // a Text node was not found
+ return null;
+ }
+
+ /* (non-Javadoc)
+ */
+ public void putTextData(String data) {
+ Text textNode = getTextNode();
+ if (textNode == null) {
+ textNode = factory.createTextNode(data);
+ // Always add the text node as the first child (fixes bug 93718)
+ element.insertBefore(textNode, element.getFirstChild());
+ } else {
+ textNode.setData(data);
+ }
+ }
+
+ public String getTextData() {
+ Text textNode = getTextNode();
+ if (textNode != null) {
+ return textNode.getData();
+ }
+ return ""; //$NON-NLS-1$
+ }
+}
\ No newline at end of file