From: <dc...@us...> - 2002-09-08 15:22:28
|
Update of /cvsroot/xmlceclipse/net.sourceforge.xmlceclipse.core/src/net/sourceforge/xmlceclipse/core In directory usw-pr-cvs1:/tmp/cvs-serv19441/src/net/sourceforge/xmlceclipse/core Added Files: ProjectPropertyGroup.java XmlcNature.java XmlcProperties.java XmlceclipsePlugin.java XmlcBuilder.java XmlcCapabilityWizard.java Log Message: Capabilities where deprecated, at least for a while --- NEW FILE: ProjectPropertyGroup.java --- /* * Copyright (c) 2002 David Corbin and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License v2 * which accompanies this distribution, and is available at * http://www.gnu.org/copyleft/gpl.html * * Contributors: * David Corbin - Initial implementation. */ package net.sourceforge.xmlceclipse.core; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.ui.IMemento; import org.eclipse.ui.WorkbenchException; import org.eclipse.ui.XMLMemento; public class ProjectPropertyGroup { Map resourceProperties; IProject project; /** * Constructor ProjectPropertyGroup. * @param iProject */ public ProjectPropertyGroup(IProject project) { this.project = project; resourceProperties = new HashMap(); loadProperties(); } private void savePropertyFile() throws IOException { try { String fileName = getFileName(); XMLMemento rootXml = XMLMemento.createWriteRoot("xmlc"); for (Iterator i = resourceProperties.keySet().iterator(); i.hasNext(); ) { String resourcePath = (String)i.next(); XmlcProperties properties = (XmlcProperties)resourceProperties.get(resourcePath); IMemento xml = rootXml.createChild("resource",resourcePath); xml.createChild("javaSourceFolder").putTextData(properties.getJavaSourceFolder()); xml.createChild("classNameTemplate").putTextData(properties.getClassNameTemplate()); xml.createChild("packagePrefix").putTextData(properties.getPackagePrefix()); xml.createChild("domFactory").putTextData(properties.getDomFactory()); xml.createChild("markupLanguageParser").putTextData(properties.getMarkupLanguageParser()); IMemento sourceFoldersXml = xml.createChild("sourceFolders"); for (Iterator f = properties.getSourceFolders().iterator(); f.hasNext(); ) { String folder = (String)f.next(); sourceFoldersXml.createChild("sourceFolder").putTextData(folder); } } Writer writer = new FileWriter(getFileName()); rootXml.save(writer); } catch(NullPointerException ne) { ne.printStackTrace(); } } public void setProperties(IResource resource, XmlcProperties properties) throws IOException { resourceProperties.put(resource.getFullPath().toString(),properties); savePropertyFile(); } private String getFileName() { IPath path = project.getLocation(); path = path.append(".xmlc"); return path.toOSString(); } private Reader getPropertiesFileReader() throws java.io.IOException { return new FileReader(getFileName()); } private void loadResourceProperties(IMemento xml) { XmlcProperties properties = new XmlcProperties(); String resourceId = xml.getID(); properties.setClassNameTemplate(xml.getChild("classNameTemplate").getTextData()); properties.setJavaSourceFolder(xml.getChild("javaSourceFolder").getTextData()); properties.setPackagePrefix(xml.getChild("packagePrefix").getTextData()); properties.setDomFactory(xml.getChild("domFactory").getTextData()); properties.setMarkupLanguageParser(xml.getChild("markupLanguageParser").getTextData()); IMemento[] sourceFolders = xml.getChild("sourceFolders").getChildren("sourceFolder"); String [] folders = new String[sourceFolders.length]; for (int i=0; i<sourceFolders.length; i++) { folders[i] = sourceFolders[i].getTextData(); } properties.setSourceFolders(folders); resourceProperties.put(resourceId,properties); } private void loadProperties() { try { Reader reader = getPropertiesFileReader(); IMemento root = XMLMemento.createReadRoot(reader); IMemento[] mementos = root.getChildren("resource"); for (int i=0; i<mementos.length; i++) { loadResourceProperties(mementos[i]); } } catch (WorkbenchException e) { XmlceclipsePlugin.getDefault().getLog().log(e.getStatus()); } catch (FileNotFoundException e) { } catch (IOException e) { XmlceclipsePlugin.getDefault().getLog().log( new Status(IStatus.ERROR,XmlceclipsePlugin.PLUGIN_ID,-1, e.getMessage(),e)); } } public XmlcProperties getProperty(IResource resource) { IPath path = resource.getFullPath(); XmlcProperties properties = (XmlcProperties)resourceProperties.get(resource.getFullPath().toString()); if (properties != null) return properties; if (resource == resource.getProject()) { properties = XmlcProperties.getDefault(resource); return properties; } return getProperty(resource.getParent()); } } --- NEW FILE: XmlcNature.java --- /* * Copyright (c) 2002 David Corbin and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License v2 * which accompanies this distribution, and is available at * http://www.gnu.org/copyleft/gpl.html * * Contributors: * David Corbin - Initial implementation. */ package net.sourceforge.xmlceclipse.core; import net.sourceforge.xmlceclipse.core.util.CoreUtil; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IProjectNature; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; public class XmlcNature implements IProjectNature { public static final String NATURE_ID = "net.sourceforge.xmlceclipse.xmlcNature"; IProject project; /** * @see org.eclipse.core.resources.IProjectNature#configure() */ public void configure() throws CoreException { XmlcBuilder.addBuilderToProject(project); } /** * @see org.eclipse.core.resources.IProjectNature#deconfigure() */ public void deconfigure() throws CoreException { } /** * @see org.eclipse.core.resources.IProjectNature#getProject() */ public IProject getProject() { return project; } /** * @see org.eclipse.core.resources.IProjectNature#setProject(IProject) */ public void setProject(IProject project) { this.project = project; } public static void addNatureToProject(IProject project) throws CoreException { if (project.hasNature(NATURE_ID)) throw new CoreException(new Status(0,XmlceclipsePlugin.PLUGIN_ID,0,"Can't add nature twice",null)); CoreUtil.valdateEditProject(project); IProjectDescription description = project.getDescription(); String[] natures = description.getNatureIds(); String[] newNatures = new String[natures.length + 1]; System.arraycopy(natures, 0, newNatures, 0, natures.length); newNatures[natures.length] = NATURE_ID; // Appears to be a bug deep in the API related to this. // validateNatureSet(project, newNatures); { description.setNatureIds(newNatures); project.setDescription(description, null); return; } } public static void removeNatureFromProject(IProject project) throws CoreException { if (!project.hasNature(NATURE_ID)) throw new CoreException(new Status(0,XmlceclipsePlugin.PLUGIN_ID,0,"Can't remove non-existant nature",null)); CoreUtil.valdateEditProject(project); IProjectDescription description = project.getDescription(); String[] natures = description.getNatureIds(); String[] newNatures = new String[natures.length - 1]; int ii=0; for (int i=0; i<natures.length; i++) { if (natures[i].equals(NATURE_ID)) continue; newNatures[ii++] = natures[i]; } validateNatureSet(project, newNatures); description.setNatureIds(newNatures); project.setDescription(description, null); return; } private static void validateNatureSet(IProject project, String[] newNatures) throws CoreException { IStatus status = project.getWorkspace().validateNatureSet(newNatures); if (status.getSeverity() != IStatus.OK) throw new CoreException(status); } } --- NEW FILE: XmlcProperties.java --- /* * Copyright (c) 2002 David Corbin and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License v2 * which accompanies this distribution, and is available at * http://www.gnu.org/copyleft/gpl.html * * Contributors: * David Corbin - Initial implementation. */ package net.sourceforge.xmlceclipse.core; import java.util.ArrayList; import java.util.List; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; public class XmlcProperties { private String packagePrefix; private String javaSourceFolder; private String classNameTemplate; private String markupLanguageParser; private String domFactory; private List sourceFolders; public XmlcProperties() { } private static String convertToPackage(IPath path) { StringBuffer buffer = new StringBuffer(); String packageSeparator = ""; for (int i=0; i<path.segmentCount(); i++) { buffer.append(packageSeparator); buffer.append(path.segment(i)); packageSeparator = "."; } return buffer.toString(); } public static XmlcProperties getDefault(IResource resource) { IProject project = (IProject)resource.getAdapter(IProject.class); if (project != null) { XmlcProperties properties = new XmlcProperties(); properties.setClassNameTemplate("@Html"); properties.setJavaSourceFolder(project.getFullPath().append("xmlc-java").toString()); properties.setPackagePrefix(convertToPackage(project.getFullPath())); properties.setMarkupLanguageParser("xerces"); properties.setSourceFolders(new String[] { project.getFullPath().append("xmlc").toString() }); properties.setDomFactory("org.enhydra.xml.xhtml.XHTMLDomFactory"); return properties; } return null; } public void setSourceFolders(String[] folders) { sourceFolders = new ArrayList(folders.length); for (int i=0; i<folders.length; i++) { sourceFolders.add(folders[i]); } } public List getSourceFolders() { return sourceFolders; } /** * Returns the classNameTemplate. * @return String */ public String getClassNameTemplate() { return classNameTemplate; } /** * Returns the javaSourceFolder. * @return String */ public String getJavaSourceFolder() { return javaSourceFolder; } /** * Returns the packagePrefix. * @return String */ public String getPackagePrefix() { return packagePrefix; } /** * Sets the classNameTemplate. * @param classNameTemplate The classNameTemplate to set */ public void setClassNameTemplate(String classNameTemplate) { this.classNameTemplate = classNameTemplate; } /** * Sets the javaSourceFolder. * @param javaSourceFolder The javaSourceFolder to set */ public void setJavaSourceFolder(String javaSourceFolder) { this.javaSourceFolder = javaSourceFolder; } /** * Sets the packagePrefix. * @param packagePrefix The packagePrefix to set */ public void setPackagePrefix(String packagePrefix) { this.packagePrefix = packagePrefix; } /** * Returns the markupLanguageParser. * @return String */ public String getMarkupLanguageParser() { return markupLanguageParser; } /** * Sets the markupLanguageParser. * @param markupLanguageParser The markupLanguageParser to set */ public void setMarkupLanguageParser(String markupLanguageParser) { this.markupLanguageParser = markupLanguageParser; } /** * Returns the domFactory. * @return String */ public String getDomFactory() { return domFactory; } /** * Sets the domFactory. * @param domFactory The domFactory to set */ public void setDomFactory(String domFactory) { this.domFactory = domFactory; } } --- NEW FILE: XmlceclipsePlugin.java --- /* * Copyright (c) 2002 David Corbin and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License v2 * which accompanies this distribution, and is available at * http://www.gnu.org/copyleft/gpl.html * * Contributors: * @author David Corbin - Initial implementation. * @author Bill Barr - Documonkey * @version %I%, %G% */ package net.sourceforge.xmlceclipse.core; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IPluginDescriptor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.ui.plugin.AbstractUIPlugin; /** * The main plugin class to be used in the desktop. */ public class XmlceclipsePlugin extends AbstractUIPlugin { public static String PLUGIN_ID = "net.sourceforge.xmlceclipse"; //The shared instance. private static XmlceclipsePlugin plugin; //Resource bundle. private ResourceBundle resourceBundle; /** * The constructor. * * @param descriptor the named instance of the plugin * @throws MissingResourceException Signals that a resource is missing */ public XmlceclipsePlugin(IPluginDescriptor descriptor) { super(descriptor); plugin = this; try { resourceBundle= ResourceBundle.getBundle("net.sourceforge.xmlceclipse.xmlceclipsePluginResources"); } catch (MissingResourceException x) { resourceBundle = null; } } /** * Returns the shared instance. * * @returns plugin the shared instance */ public static XmlceclipsePlugin getDefault() { return plugin; } /** * Returns the workspace instance. * * @returns ResourcesPlugin the workspace instance */ public static IWorkspace getWorkspace() { return ResourcesPlugin.getWorkspace(); } /** * Returns the string from the plugin's resource bundle, * or 'key' if not found. * * @param key the key from the plugin's resource bundle * @return bundle the value of the key * @return key an empty key * @throws MissingResourceException Signals that a resource is missing */ public static String getResourceString(String key) { ResourceBundle bundle= XmlceclipsePlugin.getDefault().getResourceBundle(); try { return bundle.getString(key); } catch (MissingResourceException e) { return key; } } /** * Returns the plugin's resource bundle * * @return resourceBundle the instance of the resource bundle */ public ResourceBundle getResourceBundle() { return resourceBundle; } Map resourceProperties = new HashMap(); public XmlcProperties getProperties(IResource resource) { ProjectPropertyGroup propertyGroup = getPropertyGroup(resource); return propertyGroup.getProperty(resource); } private ProjectPropertyGroup getPropertyGroup(IResource resource) { String key = resource.getProject().getFullPath().toString(); ProjectPropertyGroup propertyGroup = (ProjectPropertyGroup)resourceProperties.get(key); if (propertyGroup == null) { propertyGroup = new ProjectPropertyGroup(resource.getProject()); resourceProperties.put(key,propertyGroup); } return propertyGroup; } public void storeProperties(IResource resource,XmlcProperties properties) { try { ProjectPropertyGroup group = getPropertyGroup(resource); group.setProperties(resource,properties); } catch(IOException e) { IStatus status = new Status(IStatus.ERROR,PLUGIN_ID,-1, "Unable to save XMLC properties",e); } } } --- NEW FILE: XmlcBuilder.java --- /* * Copyright (c) 2002 David Corbin and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License v2 * which accompanies this distribution, and is available at * http://www.gnu.org/copyleft/gpl.html * * Contributors: * David Corbin - Initial implementation. */ package net.sourceforge.xmlceclipse.core; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sourceforge.xmlceclipse.core.util.CoreUtil; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceDelta; import org.eclipse.core.resources.IResourceDeltaVisitor; import org.eclipse.core.resources.IResourceVisitor; import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.core.JavaModelException; import org.enhydra.xml.xmlc.commands.xmlc.XMLC; /** * @author dcorbin * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class XmlcBuilder extends IncrementalProjectBuilder { public static String BUILDER_ID="net.sourceforge.xmlceclipse.xmlcBuilder"; public static class XmlcSource { IResource resource; XmlcProperties properties; public XmlcSource(IResource resource) { this.resource = resource; properties = XmlceclipsePlugin.getDefault().getProperties(resource); } public String sourceFile() { return resource.getLocation().toOSString(); } public String className() { String resourceName = resource.getName(); int dotPosition = resourceName.indexOf('.'); String baseClassName = resourceName.substring(0,dotPosition); String className = properties.getClassNameTemplate(); className = className.replaceAll("@",baseClassName); return packageName()+"."+className; } private String packageName() { IPath path = resource.getFullPath(); for (Iterator i=properties.getSourceFolders().iterator(); i.hasNext(); ) { IPath srcPath = new Path(i.next().toString()); if (srcPath.isPrefixOf(path)) { path = path.removeFirstSegments(srcPath.segmentCount()); path = path.removeLastSegments(1); if (path.segmentCount() == 0) return properties.getPackagePrefix(); return properties.getPackagePrefix() + "." +path.toString().replaceAll("/","."); } } return properties.getPackagePrefix() + "."; } public IFolder getBaseJavaSourceFolder() { IPath srcPath = new Path(properties.getJavaSourceFolder()); srcPath = srcPath.removeFirstSegments(1); IFolder folder = resource.getProject().getFolder(srcPath); return folder; } public IFolder getJavaSourceFolder() { IFolder folder = getBaseJavaSourceFolder(); String className = className(); String classAsPath = className.replace('.','/'); IPath path = new Path(classAsPath); path = path.removeLastSegments(1); return folder.getFolder(path); } public static boolean isXmlcSource(IResource resource) { String extension = resource.getFileExtension(); if (extension == null) return false; XmlcProperties properties = XmlceclipsePlugin.getDefault().getProperties(resource); List srcDirs = properties.getSourceFolders(); for (Iterator i=srcDirs.iterator(); i.hasNext(); ) { IPath srcPath = new Path((String)i.next()); if (!srcPath.isPrefixOf(resource.getFullPath())) return false; } return extension.equalsIgnoreCase("html"); } public String convertClassNameToJavaSourceFileName() { String className = className(); int i; while ((i=className.indexOf(".")) >= 0) { className = className.substring(0,i)+java.io.File.separator+ className.substring(i+1); } className = className+".java"; return className; } private void refreshResourceTree(IProgressMonitor monitor, IFile javaFile) throws CoreException { javaFile.refreshLocal(IResource.DEPTH_ZERO,monitor); javaFile.setDerived(true); javaFile.getParent().refreshLocal(IResource.DEPTH_ZERO,monitor); } public void refreshResult(IFolder outputSourceDir, IProgressMonitor monitor) throws CoreException { String className = convertClassNameToJavaSourceFileName(); IPath path = new Path(className); IFile javaFile = getBaseJavaSourceFolder().getFile(path); refreshResourceTree(monitor, javaFile); } } private void ensureFolderExists(IFolder folder,IProgressMonitor monitor) throws CoreException { IContainer parent = folder.getParent(); if (parent.exists()) { if (!folder.exists()) folder.create(true,true,monitor); return; } ensureFolderExists((IFolder)parent,monitor); } private boolean build(IResource resource,IProgressMonitor monitor) { try { if (!XmlcSource.isXmlcSource(resource)) return true; XmlcSource source = new XmlcSource(resource); IFolder outputSourceDir = source.getJavaSourceFolder(); ensureFolderExists(outputSourceDir,monitor); monitor.subTask("Compiling "+resource.getFullPath().toString()); ArrayList argList = buildArgList(source, outputSourceDir); XMLC xmlc = new XMLC(); String[] argArray = (String[])argList.toArray(new String[0]); xmlc.compile(argArray); source.refreshResult(outputSourceDir,monitor); if (monitor.isCanceled()) return false; return true; } catch (Exception e) { e.printStackTrace(); } return false; } private ArrayList buildArgList(XmlcSource source, IFolder outputSourceDir) { ArrayList argList = new ArrayList(); argList.add("-class"); argList.add(source.className()); argList.add("-keep"); argList.add("-nocompile"); argList.add("-sourceout"); argList.add(source.getBaseJavaSourceFolder().getLocation().toOSString()); argList.add("-parser"); argList.add(source.properties.getMarkupLanguageParser()); argList.add("-domfactory"); argList.add(source.properties.getDomFactory()); argList.add(source.sourceFile()); for (Iterator i=argList.iterator(); i.hasNext(); ) { System.out.print(i.next().toString()+" "); } System.out.println(); return argList; } private IPath sourceOutputDir(IResource resource) throws JavaModelException { return resource.getProject().getLocation().append("xmlc.src"); } class MyBuildVisitor implements IResourceVisitor, IResourceDeltaVisitor { IProgressMonitor monitor; public MyBuildVisitor(IProgressMonitor monitor) { this.monitor = monitor; } public boolean visit(IResource res) { return build(res,monitor); } public boolean visit(IResourceDelta res) { return build(res.getResource(),monitor); } } /** * @see org.eclipse.core.internal.events.InternalBuilder#build(int, Map, IProgressMonitor) */ protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { if (kind == IncrementalProjectBuilder.FULL_BUILD) { fullBuild(monitor); } else { IResourceDelta delta = getDelta(getProject()); if (delta == null) { fullBuild(monitor); } else { incrementalBuild(delta, monitor); } } return null; } protected void fullBuild(final IProgressMonitor monitor) throws CoreException { try { getProject().accept(new MyBuildVisitor(monitor)); } catch (CoreException e) { } } protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException { // the visitor does the work. delta.accept(new MyBuildVisitor(monitor)); } public static void addBuilderToProject(IProject project) throws CoreException { IProjectDescription desc = project.getDescription(); ICommand[] commands = desc.getBuildSpec(); boolean found = false; for (int i = 0; i < commands.length; ++i) { if (commands[i].getBuilderName().equals(BUILDER_ID)) { found = true; break; } } if (!found) { CoreUtil.valdateEditProject(project); //add builder to project ICommand command = desc.newCommand(); command.setBuilderName(BUILDER_ID); ICommand[] newCommands = new ICommand[commands.length + 1]; // Add it before other builders. System.arraycopy(commands, 0, newCommands, 1, commands.length); newCommands[0] = command; desc.setBuildSpec(newCommands); project.setDescription(desc, null); } } } --- NEW FILE: XmlcCapabilityWizard.java --- /* * Copyright (c) 2002 David Corbin and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License v2 * which accompanies this distribution, and is available at * http://www.gnu.org/copyleft/gpl.html * * Contributors: * David Corbin - Initial implementation. */ package net.sourceforge.xmlceclipse.core; import org.eclipse.core.resources.IProject; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.ui.ICapabilityWizard; import org.eclipse.ui.IWorkbench; /** * @author dcorbin * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ //public class XmlcCapabilityWizard extends Wizard implements ICapabilityWizard //{ // // /** // * @see org.eclipse.jface.wizard.IWizard#performFinish() // */ // public boolean performFinish() // { // return false; // } // // /** // * @see org.eclipse.ui.ICapabilityInstallWizard#init(IWorkbench, IStructuredSelection, IProject) // */ // public void init( // IWorkbench workbench, // IStructuredSelection selection, // IProject project) // { //// addPage(new XmlcOptionsPage("page name here")); // } // //} |