From 5b0993f9136338e0a3ae66133f3bdc40b003abe6 Mon Sep 17 00:00:00 2001 From: Cédric Bosdonnat Date: Mon, 24 Jan 2011 14:13:40 +0100 Subject: Renaming org.openoffice into org.libreoffice --- src/org/libreoffice/tools/DumpException.java | 17 ++ src/org/libreoffice/tools/OptionsHelper.java | 63 ++++ src/org/libreoffice/tools/SkeletonMaker.java | 136 +++++++++ src/org/libreoffice/tools/TypeChecker.java | 129 ++++++++ src/org/libreoffice/tools/language/Language.java | 106 +++++++ .../libreoffice/tools/language/LanguageHelper.java | 68 +++++ .../tools/language/LanguageTemplateLoader.java | 33 ++ src/org/libreoffice/tools/options/Command.java | 80 +++++ .../libreoffice/tools/options/HelpFormatter.java | 111 +++++++ src/org/libreoffice/tools/options/SubCommand.java | 28 ++ .../libreoffice/tools/options/SubcommandLine.java | 24 ++ src/org/libreoffice/tools/tests/AllTests.java | 14 + src/org/libreoffice/tools/tests/CommandTest.java | 134 ++++++++ .../tools/tests/LanguageHelperTest.java | 118 +++++++ .../libreoffice/tools/tests/TypeManagerTest.java | 120 ++++++++ src/org/libreoffice/tools/types/Bootstrap.java | 340 +++++++++++++++++++++ src/org/libreoffice/tools/types/Interface.java | 71 +++++ src/org/libreoffice/tools/types/Service.java | 116 +++++++ src/org/libreoffice/tools/types/TypeManager.java | 301 ++++++++++++++++++ src/org/openoffice/tools/DumpException.java | 17 -- src/org/openoffice/tools/OptionsHelper.java | 63 ---- src/org/openoffice/tools/SkeletonMaker.java | 136 --------- src/org/openoffice/tools/TypeChecker.java | 129 -------- src/org/openoffice/tools/language/Language.java | 106 ------- .../openoffice/tools/language/LanguageHelper.java | 68 ----- .../tools/language/LanguageTemplateLoader.java | 33 -- src/org/openoffice/tools/options/Command.java | 80 ----- .../openoffice/tools/options/HelpFormatter.java | 111 ------- src/org/openoffice/tools/options/SubCommand.java | 28 -- .../openoffice/tools/options/SubcommandLine.java | 24 -- src/org/openoffice/tools/tests/AllTests.java | 14 - src/org/openoffice/tools/tests/CommandTest.java | 134 -------- .../openoffice/tools/tests/LanguageHelperTest.java | 118 ------- .../openoffice/tools/tests/TypeManagerTest.java | 120 -------- src/org/openoffice/tools/types/Bootstrap.java | 339 -------------------- src/org/openoffice/tools/types/Interface.java | 71 ----- src/org/openoffice/tools/types/Service.java | 116 ------- src/org/openoffice/tools/types/TypeManager.java | 254 --------------- 38 files changed, 2009 insertions(+), 1961 deletions(-) create mode 100644 src/org/libreoffice/tools/DumpException.java create mode 100644 src/org/libreoffice/tools/OptionsHelper.java create mode 100644 src/org/libreoffice/tools/SkeletonMaker.java create mode 100644 src/org/libreoffice/tools/TypeChecker.java create mode 100644 src/org/libreoffice/tools/language/Language.java create mode 100644 src/org/libreoffice/tools/language/LanguageHelper.java create mode 100644 src/org/libreoffice/tools/language/LanguageTemplateLoader.java create mode 100644 src/org/libreoffice/tools/options/Command.java create mode 100644 src/org/libreoffice/tools/options/HelpFormatter.java create mode 100644 src/org/libreoffice/tools/options/SubCommand.java create mode 100644 src/org/libreoffice/tools/options/SubcommandLine.java create mode 100644 src/org/libreoffice/tools/tests/AllTests.java create mode 100644 src/org/libreoffice/tools/tests/CommandTest.java create mode 100644 src/org/libreoffice/tools/tests/LanguageHelperTest.java create mode 100644 src/org/libreoffice/tools/tests/TypeManagerTest.java create mode 100644 src/org/libreoffice/tools/types/Bootstrap.java create mode 100644 src/org/libreoffice/tools/types/Interface.java create mode 100644 src/org/libreoffice/tools/types/Service.java create mode 100644 src/org/libreoffice/tools/types/TypeManager.java delete mode 100644 src/org/openoffice/tools/DumpException.java delete mode 100644 src/org/openoffice/tools/OptionsHelper.java delete mode 100644 src/org/openoffice/tools/SkeletonMaker.java delete mode 100644 src/org/openoffice/tools/TypeChecker.java delete mode 100644 src/org/openoffice/tools/language/Language.java delete mode 100644 src/org/openoffice/tools/language/LanguageHelper.java delete mode 100644 src/org/openoffice/tools/language/LanguageTemplateLoader.java delete mode 100644 src/org/openoffice/tools/options/Command.java delete mode 100644 src/org/openoffice/tools/options/HelpFormatter.java delete mode 100644 src/org/openoffice/tools/options/SubCommand.java delete mode 100644 src/org/openoffice/tools/options/SubcommandLine.java delete mode 100644 src/org/openoffice/tools/tests/AllTests.java delete mode 100644 src/org/openoffice/tools/tests/CommandTest.java delete mode 100644 src/org/openoffice/tools/tests/LanguageHelperTest.java delete mode 100644 src/org/openoffice/tools/tests/TypeManagerTest.java delete mode 100644 src/org/openoffice/tools/types/Bootstrap.java delete mode 100644 src/org/openoffice/tools/types/Interface.java delete mode 100644 src/org/openoffice/tools/types/Service.java delete mode 100644 src/org/openoffice/tools/types/TypeManager.java diff --git a/src/org/libreoffice/tools/DumpException.java b/src/org/libreoffice/tools/DumpException.java new file mode 100644 index 0000000..7f9d08d --- /dev/null +++ b/src/org/libreoffice/tools/DumpException.java @@ -0,0 +1,17 @@ +package org.libreoffice.tools; + +/** + * Exception used for all the Uno-skeletonmaker specific problems. + * + * @author cbosdonnat + * + */ +public class DumpException extends Exception { + + public DumpException( String pMessage ) { + super( pMessage ); + } + + private static final long serialVersionUID = -3757702196179358713L; + +} diff --git a/src/org/libreoffice/tools/OptionsHelper.java b/src/org/libreoffice/tools/OptionsHelper.java new file mode 100644 index 0000000..c2e5b5c --- /dev/null +++ b/src/org/libreoffice/tools/OptionsHelper.java @@ -0,0 +1,63 @@ +package org.libreoffice.tools; + +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.libreoffice.tools.language.Language; +import org.libreoffice.tools.language.LanguageHelper; +import org.libreoffice.tools.options.Command; +import org.libreoffice.tools.options.SubCommand; + +/** + * Helper class generating the options list. + * + * @author cbosdonnat + * + */ +public class OptionsHelper { + + public static final String COMMAND_COMPONENT = "component"; + public static final String COMMAND_REGISTRATION = "registration"; + + // TODO Complete the arguments to handle here + protected static Command setupCommandLine( ) { + Command cmdLine = new Command(); + + // General options + Options generalOptions = new Options(); + + OptionGroup langGroup = new OptionGroup(); + String[] langs = LanguageHelper.getLanguagesIds( ); + for (String lang : langs) { + Language langDef = LanguageHelper.getLanguage( lang ); + Option langOpt = new Option( lang, false, langDef.getDescription() ); + langGroup.addOption( langOpt ); + } + langGroup.setRequired( true ); + generalOptions.addOptionGroup( langGroup ); + + generalOptions.addOption( "unoinstall", true, "url specifies a URL to an existing UNO environment (URE, office installation)." ); + generalOptions.addOption( "o", true, "path specifies an existing directory where the output files are generated to. If path=stdout the generated code is generated on standard out instead of a file." ); + + cmdLine.setCommonOptions( generalOptions ); + + // component command options + Options compOptions = new Options( ); + compOptions.addOption( "t", true, "specifies an UNOIDL type name, e.g. com.sun.star.text.XText" ); + compOptions.addOption( "l", true, "specifies a binary type library (can be used more than once). The type library is integrated as an additional type provider in the bootstrapped type system."); + compOptions.addOption( "n", true, "specifies an implementation name for the component (used as classname, filename and package|namespace name). In 'dump' mode it is used as classname (e.g. \"MyBase::\", C++ only) to generate method bodies not inline." ); + compOptions.addOption( "propertysetmixin", false, "the generated skeleton implements the cppu::PropertySetMixin helper if a referenced new style service specifies an interface which provides attributes (directly or inherited)." ); + SubCommand component = new SubCommand( COMMAND_COMPONENT, compOptions, + "generates language specific code skeleton files using the implementation name as the file and class name" ); + cmdLine.addCommand( component ); + + // registration options + Options regOptions = new Options(); + regOptions.addOption( "s", true, "name of the implementation of a service to add to the registration system." ); + SubCommand registration = new SubCommand( COMMAND_REGISTRATION, regOptions, + "creates the registration code for a list of services implementations. The '-s' option has to be used at least once to specify the implementation name of a service to add to the registration system." ); + cmdLine.addCommand( registration ); + + return cmdLine; + } +} diff --git a/src/org/libreoffice/tools/SkeletonMaker.java b/src/org/libreoffice/tools/SkeletonMaker.java new file mode 100644 index 0000000..fce6d89 --- /dev/null +++ b/src/org/libreoffice/tools/SkeletonMaker.java @@ -0,0 +1,136 @@ +package org.libreoffice.tools; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.libreoffice.tools.language.Language; +import org.libreoffice.tools.language.LanguageHelper; +import org.libreoffice.tools.options.Command; +import org.libreoffice.tools.options.HelpFormatter; +import org.libreoffice.tools.options.SubcommandLine; +import org.libreoffice.tools.types.TypeManager; + +import freemarker.template.Template; + +public class SkeletonMaker { + + // The file template names + public static final String COMPONENT = "component.tpl"; + + public static void generateComponent( CommandLine pArgs ) { + + TypeManager manager = null; + PrintWriter writer = null; + + try { + // Load the types manager + String inifile = pArgs.getOptionValue( "unoinstall" ); + manager = new TypeManager( inifile ); + manager.initRegistries( pArgs.getOptionValues( "l" ) ); + + // Extract all the informations from the types + TypeChecker checker = new TypeChecker( manager ); + checker.check( pArgs ); + + // Print everything + writer = getWriter( pArgs ); + + Language lang = getLanguage( pArgs ); + Template tpl = lang.getTemplate( COMPONENT ); + tpl.process( checker.getTemplateModel( ), writer ); + writer.flush(); + + } catch ( Exception e ) { + e.printStackTrace(); + } finally { + // Close the file stream if possible + try { writer.close(); } catch ( Exception e ) { } + + // Close the running OOo + if ( manager != null ) { + manager.dispose(); + } + } + } + + private static PrintWriter getWriter( CommandLine pArgs ) throws IOException { + PrintWriter writer = new PrintWriter( System.out ); + String outPath = pArgs.getOptionValue( "o" ); + if ( outPath != null && !outPath.equals( "stdout" ) ) { + String implName = pArgs.getOptionValue( "n" ).replace( '.', '/' ); + Language lang = getLanguage( pArgs ); + String ext = lang.getExtension(); + + String targetPath = getFilenameFromType( outPath, implName, ext ); + + File out = new File( targetPath ); + writer = new PrintWriter( out ); + } + + return writer; + } + + private static String getFilenameFromType(String pOutPath, + String pImplName, String pExt) { + StringBuffer buf = new StringBuffer(); + if ( pOutPath.isEmpty() ) { + buf.append( "." ); + } else { + buf.append( pOutPath ); + } + + String sep = System.getProperty( "file.separator" ); + + buf.append( sep ); + buf.append( pImplName ); + buf.append( pExt ); + + String filename = buf.toString(); + // Clean up the separators + if ( sep.equals( "/" ) ) { + filename = filename.replace( "\\\\", sep ); + } else { + filename = filename.replace( "/", sep ); + } + + return filename; + } + + public static void main(String[] args) { + + Command cmd = OptionsHelper.setupCommandLine(); + try { + SubcommandLine commandline = cmd.parse( args, new PosixParser() ); + + // TODO Add the other commands + String subCommand = commandline.getSubcommand(); + if ( subCommand.equals( OptionsHelper.COMMAND_COMPONENT ) ) { + generateComponent( commandline.getCommandLine() ); + + } else if ( subCommand.equals( OptionsHelper.COMMAND_REGISTRATION ) ) { + // TODO implement + } + } catch (ParseException e) { + System.err.println( e.getMessage() ); + + HelpFormatter.printHelp( "uno-skeletonmaker", cmd ); + } + } + + private static Language getLanguage( CommandLine pArgs ) { + String[] langs = LanguageHelper.getLanguagesIds(); + String lang = null; + int i = 0; + while ( lang == null && i < langs.length ) { + if ( pArgs.hasOption( langs[i] ) ) { + lang = langs[i]; + } + i++; + } + return LanguageHelper.getLanguage( lang ); + } +} diff --git a/src/org/libreoffice/tools/TypeChecker.java b/src/org/libreoffice/tools/TypeChecker.java new file mode 100644 index 0000000..3489fdf --- /dev/null +++ b/src/org/libreoffice/tools/TypeChecker.java @@ -0,0 +1,129 @@ +package org.libreoffice.tools; + +import java.util.ArrayList; + +import org.apache.commons.cli.CommandLine; +import org.libreoffice.tools.types.TypeManager; + +import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; +import com.sun.star.reflection.XPropertyTypeDescription; + +public class TypeChecker { + + private static final int MAX_INTERFACES = 12; + + private TypeManager mManager; + + private boolean mSupportXComp; + private ArrayList mInterfaces; + private ArrayList mServices; + private ArrayList mProperties; + private ArrayList mAttributes; + private ArrayList mPropinterfaces; + private String mPropertyHelper; + + + public TypeChecker(TypeManager pManager) { + mManager = pManager; + + mInterfaces = new ArrayList(); + mServices = new ArrayList(); + mProperties = new ArrayList(); + mAttributes = new ArrayList(); + mPropinterfaces = new ArrayList(); + mPropertyHelper = null; + } + + public boolean isSupportXComp() { + return mSupportXComp; + } + + public ArrayList getInterfaces() { + return mInterfaces; + } + + public ArrayList getServices() { + return mServices; + } + + public ArrayList getProperties() { + return mProperties; + } + + public ArrayList getAttributes() { + return mAttributes; + } + + public ArrayList getPropinterfaces() { + return mPropinterfaces; + } + + public String getPropertyHelper() { + return mPropertyHelper; + } + + public void check( CommandLine pArgs ) throws DumpException { + // Check the types + String[] types = pArgs.getOptionValues( "t" ); + + + // Get the service, interfaces and members from the types + for (String type : types) { + mManager.checkType( type, mServices, mInterfaces, mProperties ); + } + + // TODO For ProtocolHandler case, check the frame.ProtocolHandler and frame.XDispatch types + + // Check for the property helper + mPropertyHelper = mManager.checkPropertyHelper( pArgs, mServices, mInterfaces, mAttributes, mPropinterfaces ); + + // Check the default interfaces + checkDefaultInterfaces( ); + + if ( mInterfaces.size() > MAX_INTERFACES ) { + throw new DumpException( "The skeletonmaker supports components with 12 interfaces only (limitation of the UNO implementation helpers)!" ); + } + + mSupportXComp = mManager.checkXComponentSupport( mInterfaces ); + } + + /** + * Variables usable in the template model: + *
    + *
  • serviceObject: boolean
  • + *
  • supportsXComponent: boolean
  • + *
  • interfaces: array of interfaces properties
  • + *
  • propertyHelper: string (empty string or the property helper name)
  • + *
  • classname: string (the name of the class to create)
  • + *
+ * + * Interface objet: + *
    + *
  • name: string for the interface name
  • + *
  • + *
+ * + * @return the template model object + */ + public Object getTemplateModel() { + // TODO Setup the template model using the checked infos. + return null; + } + + private void checkDefaultInterfaces( ) { + + if ( mServices.isEmpty() ) { + mInterfaces.remove( "com.sun.star.lang.XServiceInfo" ); + } else { + if ( !mInterfaces.contains( "com.sun.star.lang.XServiceInfo" ) ) { + mInterfaces.add( "com.sun.star.lang.XServiceInfo" ); + } + } + + if ( mPropertyHelper.equals( TypeManager.NOPROPERTYHELPER ) ) { + mInterfaces.remove( "com.sun.star.beans.XPropertySet" ); + mInterfaces.remove( "com.sun.star.beans.XFastPropertySet" ); + mInterfaces.remove( "com.sun.star.beans.XPropertyAccess" ); + } + } +} diff --git a/src/org/libreoffice/tools/language/Language.java b/src/org/libreoffice/tools/language/Language.java new file mode 100644 index 0000000..7f86500 --- /dev/null +++ b/src/org/libreoffice/tools/language/Language.java @@ -0,0 +1,106 @@ +package org.libreoffice.tools.language; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.NoSuchElementException; +import java.util.Properties; + +import freemarker.template.Configuration; +import freemarker.template.Template; + +/** + * TODO Implement hierarchical language definitions. + * + * @author cbosdonnat + * + */ +public class Language { + + private static final String PROP_DESCRIPTION = "description"; + private static final String PROP_EXTENSION = "extension"; + + private static final String DESCRIPTION_FILENAME = "lang.properties"; + + private String mId; + private File mLangDir; + + private Configuration mTemplateConfig; + + private Properties mProps; + + /** + * Constructor. + * + * @param pId the language identifier. + * @param pDir the language root directory (eg. cpp for C++). + * + * @throws IOException if the file provided isn't a proper language directory. + */ + public Language( String pId, File pDir ) throws IOException { + + mId = pId; + if ( !pDir.isDirectory() ) { + throw new IOException( "Invalid language directory: " + pDir.toString() ); + } + mLangDir = pDir; + + // Define a template config for each language + mTemplateConfig = new Configuration(); + mTemplateConfig.setTemplateLoader( new LanguageTemplateLoader( this ) ); + } + + public String getDescription( ) { + return getProperty( PROP_DESCRIPTION ); + } + + public String getExtension( ) { + return getProperty( PROP_EXTENSION ); + } + + public String getProperty( String pPropName ) { + if ( mProps == null ) { + loadProperties( ); + } + String serialized = null; + Object value = mProps.get( pPropName ); + if ( value != null ) { + serialized = value.toString(); + } + return serialized; + } + + public Template getTemplate( String pName ) throws IOException { + return mTemplateConfig.getTemplate( pName ); + } + + protected File getTemplateFile( String pName ) { + File result = null; + + result = new File( mLangDir, pName ); + if ( !result.isFile() || !result.canRead() ) { + String msg = MessageFormat.format( "No template {0} for language {1}", pName, mId ); + throw new NoSuchElementException( msg ); + } + + return result; + } + + private void loadProperties() { + mProps = new Properties(); + File descFile = new File( mLangDir, DESCRIPTION_FILENAME ); + if ( descFile.isFile() && descFile.canRead() ) { + Properties props = new Properties( ); + FileInputStream in = null; + try { + in = new FileInputStream( descFile ); + props.load( in ); + mProps.putAll( props ); + } catch ( Exception e ) { + } finally { + try { in.close(); } catch ( Exception e) { }; + } + } + } +} diff --git a/src/org/libreoffice/tools/language/LanguageHelper.java b/src/org/libreoffice/tools/language/LanguageHelper.java new file mode 100644 index 0000000..5f7fe14 --- /dev/null +++ b/src/org/libreoffice/tools/language/LanguageHelper.java @@ -0,0 +1,68 @@ +package org.libreoffice.tools.language; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.net.URISyntaxException; + +/** + * Utility methods handling the language definitions and templates. + * + * TODO Store the language objects in a static way for perf. reasons + * + * @author cbosdonnat + * + */ +public class LanguageHelper { + + private static final String DEFAULT_LANGS_DIR = "langs"; + + private static File sLangsDir; + + /** + * Changes the languages configuration directory. + * + * @param pConfigDir the new directory containing the languages definitions. + */ + public static void setLanguagesDir( File pConfigDir ) { + sLangsDir = pConfigDir; + } + + public static String[] getLanguagesIds() { + File langsDir = getLangsDir(); + + String[] langs = langsDir.list( new FilenameFilter() { + + @Override + public boolean accept(File pDir, String pName) { + return !pName.startsWith("."); + } + }); + + return langs; + } + + public static Language getLanguage( String pLang ) { + Language language = null; + try { + language = new Language( pLang, new File( getLangsDir( ), pLang ) ); + } catch (IOException e) { + e.printStackTrace(); + } + return language; + } + + private static File getLangsDir( ) { + + if ( sLangsDir == null ) { + try { + File jarFile = new File(LanguageHelper.class.getProtectionDomain() + .getCodeSource().getLocation().toURI()); + sLangsDir = new File( jarFile.getParentFile(), DEFAULT_LANGS_DIR ); + + } catch (URISyntaxException e) { + } + } + return sLangsDir; + } +} diff --git a/src/org/libreoffice/tools/language/LanguageTemplateLoader.java b/src/org/libreoffice/tools/language/LanguageTemplateLoader.java new file mode 100644 index 0000000..a1a15cd --- /dev/null +++ b/src/org/libreoffice/tools/language/LanguageTemplateLoader.java @@ -0,0 +1,33 @@ +package org.libreoffice.tools.language; + +import java.io.File; +import java.io.IOException; +import java.util.NoSuchElementException; + +import freemarker.cache.FileTemplateLoader; + +public class LanguageTemplateLoader extends FileTemplateLoader { + + private Language mLang; + + public LanguageTemplateLoader( Language pLangDef ) throws IOException { + mLang = pLangDef; + } + + /** + * Delegate the template finding to the language itself. + * + * @param pName the template name to find + * + * @return the template File if found, or null. + */ + @Override + public Object findTemplateSource(String pName ) throws IOException { + File file = null; + try { + file = mLang.getTemplateFile( pName ); + } catch ( NoSuchElementException e ) { + } + return file; + } +} diff --git a/src/org/libreoffice/tools/options/Command.java b/src/org/libreoffice/tools/options/Command.java new file mode 100644 index 0000000..2dd9095 --- /dev/null +++ b/src/org/libreoffice/tools/options/Command.java @@ -0,0 +1,80 @@ +package org.libreoffice.tools.options; + +import java.util.Collection; +import java.util.HashMap; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +public class Command { + + private HashMap< String, SubCommand > mCommands; + private Options mCommonOptions; + + public Command( ) { + mCommands = new HashMap(); + } + + public void addCommand( SubCommand pCommand ) { + if ( !mCommands.containsKey( pCommand.getName() ) ) { + mCommands.put( pCommand.getName(), pCommand ); + } + } + + public void removeCommand( String pCommandName ) { + mCommands.remove( pCommandName ); + } + + public Collection< SubCommand > getCommands( ) { + return mCommands.values(); + } + + public void setCommonOptions( Options pOptions ) { + mCommonOptions = pOptions; + } + + public Options getCommonOptions( ) { + return mCommonOptions; + } + + public SubcommandLine parse( String[] pArguments, CommandLineParser pParser ) throws ParseException { + if ( pArguments.length < 1 ) { + throw new ParseException( "No sub-command" ); + } + String subcommand = pArguments[0]; + + // Compute the final options list: common + subcommand + Options allOptions = new Options(); + addAllOptions( allOptions, mCommonOptions ); + + SubCommand def = mCommands.get( subcommand ); + if ( def == null ) { + throw new ParseException( "Invalid sub-command: " + subcommand ); + } + + addAllOptions( allOptions, def.getOptions() ); + + // Get only the subcommand arguments + String[] subCommandArguments = new String[ pArguments.length - 1 ]; + System.arraycopy( pArguments, 1, subCommandArguments, 0, subCommandArguments.length ); + + // Parse + CommandLine cmdline = pParser.parse( allOptions, subCommandArguments ); + + return new SubcommandLine( subcommand, cmdline ); + } + + private void addAllOptions(Options pAllOptions, Options pCommonOptions) { + for ( Object opt : pCommonOptions.getOptions() ) { + if ( opt instanceof Option ) { + pAllOptions.addOption( ( Option ) opt ); + } else if ( opt instanceof OptionGroup ) { + pAllOptions.addOptionGroup( (OptionGroup ) opt ); + } + } + } +} diff --git a/src/org/libreoffice/tools/options/HelpFormatter.java b/src/org/libreoffice/tools/options/HelpFormatter.java new file mode 100644 index 0000000..15276b7 --- /dev/null +++ b/src/org/libreoffice/tools/options/HelpFormatter.java @@ -0,0 +1,111 @@ +package org.libreoffice.tools.options; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.Collection; + +public class HelpFormatter { + + private static final int TAB_SPACES = 4; + private static final int COMMAND_SEP_SPACES = 2; + + public static void printHelp( String pProgram, Command pCommandLine ) { + printHelp( System.out, 80, pProgram, pCommandLine ); + } + + public static void printHelp( PrintStream pOut, int pMaxLength, String pProgram, Command pCommandLine ) { + + org.apache.commons.cli.HelpFormatter formatter = new org.apache.commons.cli.HelpFormatter(); + formatter.setSyntaxPrefix( getFilledString( ' ', TAB_SPACES ) ); + PrintWriter pw = new PrintWriter( pOut ); + + // Output the global usage + pOut.println( "\nUsage:" ); + for ( SubCommand command : pCommandLine.getCommands() ) { + String app = pProgram + " " + command.getName(); + formatter.printUsage( pw, pMaxLength, app, command.getOptions() ); + } + pw.flush(); + + // Output the commands list + pOut.println( "\nSub-commands:" ); + printSubCommands( pOut, pMaxLength, pCommandLine.getCommands() ); + + + // Output the common options + pOut.println( "\nCommon options:" ); + formatter.printOptions( pw, pMaxLength, pCommandLine.getCommonOptions(), TAB_SPACES, COMMAND_SEP_SPACES ); + pw.flush(); + + + // Output the commands options + pOut.println( "\nSub-commands options:" ); + for ( SubCommand command : pCommandLine.getCommands() ) { + int nbOptions = command.getOptions().getOptions().size(); + if ( nbOptions > 0 ) { + pOut.println( getFilledString( ' ', TAB_SPACES ) + command.getName() ); + formatter.printOptions( pw, pMaxLength, command.getOptions(), TAB_SPACES * 2, COMMAND_SEP_SPACES ); + pw.flush(); + } + } + } + + private static void printSubCommands( PrintStream pOut, int pMaxLength, Collection pCommands ) { + int maxCmdLength = 0; + for ( SubCommand command : pCommands ) { + if ( command.getName().length() > maxCmdLength ) { + maxCmdLength = command.getName().length(); + } + } + + for ( SubCommand command : pCommands ) { + String name = command.getName(); + String message = getFilledString( ' ', TAB_SPACES ) + name; + + int nameDiff = ( maxCmdLength - name.length() ); + message += getFilledString( ' ', nameDiff + COMMAND_SEP_SPACES ); + + int restLen = pMaxLength - maxCmdLength - TAB_SPACES - COMMAND_SEP_SPACES; + String aligningBlanks = getFilledString( ' ', pMaxLength - restLen ); + + String[] words = command.getDescription().split( " " ); + int i = 0; + boolean firstLine = true; + String line = new String(); + while ( i < words.length ) { + String word = words[i]; + if ( line.length() + 1 + word.length() < restLen ) { + if ( line.length() > 0 ) { + line += " "; + } + line += word; + + if ( i == words.length - 1 ) { + if ( !firstLine ) { + message += aligningBlanks; + } + message += line + "\n"; + } + + } else { + if ( !firstLine ) { + message += aligningBlanks; + } + message += line + "\n"; + line = word; + firstLine = false; + } + i++; + } + + pOut.print( message ); + } + } + + private static String getFilledString(char pChar, int pNumber) { + char[] chars = new char[pNumber]; + Arrays.fill( chars, pChar ); + return new String( chars ); + } +} diff --git a/src/org/libreoffice/tools/options/SubCommand.java b/src/org/libreoffice/tools/options/SubCommand.java new file mode 100644 index 0000000..5a1aa20 --- /dev/null +++ b/src/org/libreoffice/tools/options/SubCommand.java @@ -0,0 +1,28 @@ +package org.libreoffice.tools.options; + +import org.apache.commons.cli.Options; + +public class SubCommand { + + private String mName; + private String mDescription; + private Options mOptions; + + public SubCommand( String pName, Options pOptions, String pDescription ) { + mName = pName; + mDescription = pDescription; + mOptions = pOptions; + } + + public String getName() { + return mName; + } + + public String getDescription() { + return mDescription; + } + + public Options getOptions() { + return mOptions; + } +} diff --git a/src/org/libreoffice/tools/options/SubcommandLine.java b/src/org/libreoffice/tools/options/SubcommandLine.java new file mode 100644 index 0000000..8e45dcf --- /dev/null +++ b/src/org/libreoffice/tools/options/SubcommandLine.java @@ -0,0 +1,24 @@ +package org.libreoffice.tools.options; + +import org.apache.commons.cli.CommandLine; + +public class SubcommandLine { + + private static final long serialVersionUID = 1578591361018635215L; + + private String mSubcommand; + private CommandLine mCommandLine; + + public SubcommandLine( String pSubcommand, CommandLine pCommandLine ) { + mSubcommand = pSubcommand; + mCommandLine = pCommandLine; + } + + public String getSubcommand() { + return mSubcommand; + } + + public CommandLine getCommandLine() { + return mCommandLine; + } +} diff --git a/src/org/libreoffice/tools/tests/AllTests.java b/src/org/libreoffice/tools/tests/AllTests.java new file mode 100644 index 0000000..8919f10 --- /dev/null +++ b/src/org/libreoffice/tools/tests/AllTests.java @@ -0,0 +1,14 @@ +package org.libreoffice.tools.tests; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith( Suite.class ) +@SuiteClasses( { + LanguageHelperTest.class, + CommandTest.class, + TypeManagerTest.class } ) +public class AllTests { + +} diff --git a/src/org/libreoffice/tools/tests/CommandTest.java b/src/org/libreoffice/tools/tests/CommandTest.java new file mode 100644 index 0000000..d2f00e5 --- /dev/null +++ b/src/org/libreoffice/tools/tests/CommandTest.java @@ -0,0 +1,134 @@ +package org.libreoffice.tools.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.libreoffice.tools.options.Command; +import org.libreoffice.tools.options.SubCommand; +import org.libreoffice.tools.options.SubcommandLine; + +public class CommandTest { + + private Command mCommand; + + @Before + public void setUp() throws Exception { + mCommand = setupCommandLine(); + } + + @After + public void tearDown( ) throws Exception { + mCommand = null; + } + + @Test + public void testParseValid() { + String[] validArgs = new String[] { + "registration", + "-cpp", + "-s", + "TestImpl" + }; + + try { + SubcommandLine line = mCommand.parse( validArgs, new PosixParser() ); + assertNotNull( "Result of parsing is null", line.getCommandLine() ); + assertEquals( "Wrong subcommand", "registration", line.getSubcommand() ); + + // Check the presence of the last option and its value + assertTrue( "Option not recognized", line.getCommandLine().hasOption( "s" ) ); + assertEquals( "wrong value detected", "TestImpl", line.getCommandLine().getOptionValue( "s" ) ) ; + } catch (ParseException e) { + fail( "Unexcepted exception: " + e.getMessage() ); + } + } + + @Test + public void testParseSubcommandNameError() { + String[] validArgs = new String[] { + "failure", + "-cpp", + "-s", + "TestImpl" + }; + + try { + mCommand.parse( validArgs, new PosixParser() ); + fail( "Should have thrown an exception" ); + } catch (ParseException e) { + assertTrue( "Wrong exception message", e.getMessage().endsWith( "failure" ) ); + } + } + + @Test + public void testParseSubcommandArgsError() { + String[] validArgs = new String[] { + "registration", + "-cpp", + "--error", + "--err2" + }; + + try { + mCommand.parse( validArgs, new PosixParser() ); + fail( "Should have thrown an exception" ); + } catch (ParseException e) { + assertTrue( "Error message doesn't contain '--error'" , e.getMessage().contains( "--error" ) ); + } + } + + @Test( expected=ParseException.class ) + public void testParseSubcommandNoArg() throws Exception { + String[] validArgs = new String[] { + "uno-skeletonmaker" + }; + + mCommand.parse( validArgs, new PosixParser() ); + } + + private static Command setupCommandLine( ) { + Command cmdLine = new Command(); + + // General options + Options generalOptions = new Options(); + + OptionGroup langGroup = new OptionGroup(); + Option java5 = new Option( "java5", false, "generate output for Java 1.5 or later (is currently the default)" ); + Option java4 = new Option( "java4", false, "generate output for Java 1.4 or earlier" ); + Option cpp = new Option( "cpp", false, "generate output for C++" ); + langGroup.addOption( java4 ); + langGroup.addOption( java5 ); + langGroup.addOption( cpp ); + langGroup.setRequired( false ); + generalOptions.addOptionGroup( langGroup ); + + cmdLine.setCommonOptions( generalOptions ); + + + // component command options + Options compOptions = new Options( ); + compOptions.addOption( "t", true, "specifies an UNOIDL type name, e.g. com.sun.star.text.XText" ); + SubCommand component = new SubCommand( "component", compOptions, + "generates language specific code skeleton files using the implementation name as the file and class name" ); + cmdLine.addCommand( component ); + + // registration options + Options regOptions = new Options(); + regOptions.addOption( "s", true, "name of the implementation of a service to add to the registration system." ); + SubCommand registration = new SubCommand( "registration", regOptions, + "creates the registration code for a list of services implementations. The '-s' option has to be used at least once to specify the implementation name of a service to add to the registration system." ); + cmdLine.addCommand( registration ); + + return cmdLine; + } +} diff --git a/src/org/libreoffice/tools/tests/LanguageHelperTest.java b/src/org/libreoffice/tools/tests/LanguageHelperTest.java new file mode 100644 index 0000000..7879212 --- /dev/null +++ b/src/org/libreoffice/tools/tests/LanguageHelperTest.java @@ -0,0 +1,118 @@ +package org.libreoffice.tools.tests; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.File; +import java.io.StringWriter; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.HashMap; + +import org.junit.Before; +import org.junit.Test; +import org.libreoffice.tools.language.Language; +import org.libreoffice.tools.language.LanguageHelper; + +import freemarker.template.Template; + +/** + * ATM, the fixture of this class is the main language configuration: it + * would be more complex to generate the fixture here. + * + * @author cbosdonnat + * + */ +public class LanguageHelperTest { + + private static final String TESTS_LANGS_DIR = "test_langs"; + + private static final String WORKING_LANG = "working"; + private static final Object WORKING_DESCRIPTION = "Working test lang"; + + private static final String MISSING_LANG = "missing"; + private static final String UNCOMPLETE_LANG = "uncomplete"; + + private static final String SIMPLE_TPL = "simple.tpl"; + private static final String NESTED_TPL = "nested.tpl"; + + private static final String VAR_SOMETEXT_NAME = "sometext"; + private static final String VAR_SOMETEXT_VALUE = "some text"; + + @Before + public void setup( ) throws URISyntaxException { + File jarFile = new File(LanguageHelperTest.class.getProtectionDomain() + .getCodeSource().getLocation().toURI()); + File dir = new File( jarFile.getParentFile(), TESTS_LANGS_DIR ); + LanguageHelper.setLanguagesDir( dir ); + } + + @Test + public void testGetLanguages( ) { + String[] result = LanguageHelper.getLanguagesIds(); + String[] expected = new String[] { WORKING_LANG, UNCOMPLETE_LANG }; + + // The order in results aren't guaranteed + Arrays.sort( result ); + Arrays.sort( expected ); + assertArrayEquals( "Different values", expected, result ); + } + + @Test + public void testGetDescriptionOk( ) { + Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); + assertNotNull( langDef ); + String actual = langDef.getDescription( ); + assertEquals( WORKING_DESCRIPTION , actual ); + } + + @Test + public void testGetLanguageMissing( ) { + Language langDef = LanguageHelper.getLanguage( MISSING_LANG ); + assertNull( langDef ); + } + + @Test + public void testGetLanguageMissingProp( ) { + Language langDef = LanguageHelper.getLanguage( UNCOMPLETE_LANG ); + assertNotNull( langDef ); + String actual = langDef.getDescription(); + assertNull( actual ); + } + + @Test + public void testSimpleTemplate( ) throws Exception { + Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); + assertNotNull( langDef ); + + Template tpl = langDef.getTemplate( SIMPLE_TPL ); + StringWriter out = new StringWriter(); + tpl.process( getTestTemplateModel(), out ); + out.flush(); + + String expected = VAR_SOMETEXT_VALUE; + assertEquals( expected, out.toString() ); + } + + @Test + public void testNestedTemplate( ) throws Exception { + Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); + assertNotNull( langDef ); + + Template tpl = langDef.getTemplate( NESTED_TPL ); + StringWriter out = new StringWriter(); + tpl.process( getTestTemplateModel(), out ); + out.flush(); + + String expected = VAR_SOMETEXT_VALUE + "\n" + VAR_SOMETEXT_VALUE; + assertEquals( expected, out.toString() ); + } + + private Object getTestTemplateModel() { + HashMap model = new HashMap(); + model.put( VAR_SOMETEXT_NAME, VAR_SOMETEXT_VALUE ); + return model; + } +} diff --git a/src/org/libreoffice/tools/tests/TypeManagerTest.java b/src/org/libreoffice/tools/tests/TypeManagerTest.java new file mode 100644 index 0000000..dd0fceb --- /dev/null +++ b/src/org/libreoffice/tools/tests/TypeManagerTest.java @@ -0,0 +1,120 @@ +package org.libreoffice.tools.tests; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.libreoffice.tools.types.TypeManager; + +import com.sun.star.container.NoSuchElementException; +import com.sun.star.reflection.XTypeDescription; + +/** + * This test class assumes a ooo.install.dir Java property is set and + * points to the directory of an installed OOo 3.x. + * + * @author cbosdonnat + * + */ +public class TypeManagerTest { + + private static final String SEARCHED_TYPE = "com.sun.star.lang.XTypeProvider"; + private static final String SEARCHED_TYPE_ERR = "some.dummy.type"; + + private static TypeManager sTestedManager; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + String installDir = System.getProperty( "ooo.install.dir" ); + + sTestedManager = new TypeManager( installDir ); + sTestedManager.initRegistries( new String[]{ + getUreRegistryUrl( installDir ) + } ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + sTestedManager.dispose(); + } + + @Test + public void testSearchTypeValid() { + try { + XTypeDescription result = sTestedManager.searchType( SEARCHED_TYPE ); + assertEquals( "Actual description doesn't match", SEARCHED_TYPE, result.getName() ); + } catch ( NoSuchElementException e ) { + fail( "This element should be found" ); + } + } + + @Test( expected=NoSuchElementException.class ) + public void testSearchTypeIncorrect() throws Exception { + sTestedManager.searchType( SEARCHED_TYPE_ERR ); + } + + private static String getUreRegistryUrl( String pInstallDir ) { + String regUrl = null; + + try { + File install = new File( pInstallDir ); + File basis = getPortableLink( "basis-link", install ); + File ure = getPortableLink( "ure-link", basis ); + + String sep = System.getProperty( "file.separator" ); + File reg = new File( ure, "share" + sep + "misc" + sep + "types.rdb" ); + + if ( reg.canRead() ) { + regUrl = "file://" + reg.toURI().toURL().getPath(); + } + + } catch ( Exception e ) { + } + + return regUrl; + } + + /** + * Get the file object for the link defined as a child of a folder. + * + * On Windows platform, the link relative location is specified as the content of + * a file named after the link name. On Unix-based systems symbolic links are + * supported. + * + *

This method has been adapted from the ooeclipse integration code, class + * org.openoffice.ide.eclipse.core.OOo.OOo3PathMapper

+ * + * @param pName the name of the symbolic link + * @param pParent the parent directory file + * + * @return the file representing the link target or null + * + */ + private static File getPortableLink(String pName, File pParent) { + File link = null; + + File linkFile = new File(pParent, pName); + if ( System.getProperty( "os.name" ).toLowerCase().startsWith( "win" ) ) { + // Read the content of the file to get the true folder + try { + FileInputStream is = new FileInputStream(linkFile); + byte[] buf = new byte[is.available()]; + is.read(buf); + + String relativePath = new String(buf); + linkFile = new File(pParent, relativePath); + link = linkFile; + } catch (Exception e) { + // the returned link is null to show the error + } + } else { + link = linkFile; + } + + return link; + } +} diff --git a/src/org/libreoffice/tools/types/Bootstrap.java b/src/org/libreoffice/tools/types/Bootstrap.java new file mode 100644 index 0000000..ccb8f75 --- /dev/null +++ b/src/org/libreoffice/tools/types/Bootstrap.java @@ -0,0 +1,340 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: Bootstrap.java,v $ + * $Revision: 1.16 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package org.libreoffice.tools.types; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Random; + +import com.sun.star.bridge.UnoUrlResolver; +import com.sun.star.bridge.XUnoUrlResolver; +import com.sun.star.comp.helper.BootstrapException; +import com.sun.star.comp.helper.ComponentContext; +import com.sun.star.comp.helper.ComponentContextEntry; +import com.sun.star.comp.loader.JavaLoader; +import com.sun.star.container.XSet; +import com.sun.star.lang.XInitialization; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XSingleComponentFactory; +import com.sun.star.lib.util.NativeLibraryLoader; +import com.sun.star.loader.XImplementationLoader; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +/** Bootstrap offers functionality to obtain a context or simply + a service manager. + The service manager can create a few basic services, whose implementations are: +
    +
  • com.sun.star.comp.loader.JavaLoader
  • +
  • com.sun.star.comp.urlresolver.UrlResolver
  • +
  • com.sun.star.comp.bridgefactory.BridgeFactory
  • +
  • com.sun.star.comp.connections.Connector
  • +
  • com.sun.star.comp.connections.Acceptor
  • +
  • com.sun.star.comp.servicemanager.ServiceManager
  • +
+ + Other services can be inserted into the service manager by + using its XSet interface: +
+        XSet xSet = (XSet)UnoRuntime.queryInterface( XSet.class, aMultiComponentFactory );
+        // insert the service manager
+        xSet.insert( aSingleComponentFactory );
+    
+ + @author cbosdo Changed the class for a better control of the bootstrap using a custom classloader +*/ +public class Bootstrap { + + private static void insertBasicFactories( + XSet xSet, XImplementationLoader xImpLoader ) + throws Exception + { + // insert the factory of the loader + xSet.insert( xImpLoader.activate( + "com.sun.star.comp.loader.JavaLoader", null, null, null ) ); + + // insert the factory of the URLResolver + xSet.insert( xImpLoader.activate( + "com.sun.star.comp.urlresolver.UrlResolver", null, null, null ) ); + + // insert the bridgefactory + xSet.insert( xImpLoader.activate( + "com.sun.star.comp.bridgefactory.BridgeFactory", null, null, null ) ); + + // insert the connector + xSet.insert( xImpLoader.activate( + "com.sun.star.comp.connections.Connector", null, null, null ) ); + + // insert the acceptor + xSet.insert( xImpLoader.activate( + "com.sun.star.comp.connections.Acceptor", null, null, null ) ); + } + + /** Bootstraps an initial component context with service manager and basic + jurt components inserted. + @param context_entries the hash table contains mappings of entry names (type string) to + context entries (type class ComponentContextEntry). + @return a new context. + */ + @SuppressWarnings("unchecked") + static public XComponentContext createInitialComponentContext( Hashtable context_entries ) + throws Exception + { + XImplementationLoader xImpLoader = (XImplementationLoader)UnoRuntime.queryInterface( + XImplementationLoader.class, new JavaLoader() ); + + // Get the factory of the ServiceManager + XSingleComponentFactory smgr_fac = (XSingleComponentFactory)UnoRuntime.queryInterface( + XSingleComponentFactory.class, xImpLoader.activate( + "com.sun.star.comp.servicemanager.ServiceManager", null, null, null ) ); + + // Create an instance of the ServiceManager + XMultiComponentFactory xSMgr = (XMultiComponentFactory)UnoRuntime.queryInterface( + XMultiComponentFactory.class, smgr_fac.createInstanceWithContext( null ) ); + + // post init loader + XInitialization xInit = (XInitialization)UnoRuntime.queryInterface( + XInitialization.class, xImpLoader ); + Object[] args = new Object [] { xSMgr }; + xInit.initialize( args ); + + // initial component context + if (context_entries == null) + context_entries = new Hashtable( 1 ); + // add smgr + context_entries.put( + "/singletons/com.sun.star.lang.theServiceManager", + new ComponentContextEntry( null, xSMgr ) ); + // ... xxx todo: add standard entries + XComponentContext xContext = new ComponentContext( context_entries, null ); + + // post init smgr + xInit = (XInitialization)UnoRuntime.queryInterface( + XInitialization.class, xSMgr ); + args = new Object [] { null, xContext }; // no registry, default context + xInit.initialize( args ); + + XSet xSet = (XSet)UnoRuntime.queryInterface( XSet.class, xSMgr ); + // insert the service manager + xSet.insert( smgr_fac ); + // and basic jurt factories + insertBasicFactories( xSet, xImpLoader ); + + return xContext; + } + + /** + * Bootstraps a servicemanager with the jurt base components registered. + *

+ * @return a freshly boostrapped service manager + * @see com.sun.star.lang.ServiceManager + */ + static public XMultiServiceFactory createSimpleServiceManager() throws Exception + { + return (XMultiServiceFactory)UnoRuntime.queryInterface( + XMultiServiceFactory.class, createInitialComponentContext( null ).getServiceManager() ); + } + + + /** Bootstraps the initial component context from a native UNO installation. + + @see cppuhelper/defaultBootstrap_InitialComponentContext() + */ + static public final XComponentContext defaultBootstrap_InitialComponentContext() + throws Exception + { + return defaultBootstrap_InitialComponentContext( null, null ); + } + /** Bootstraps the initial component context from a native UNO installation. + + @param ini_file + ini_file (may be null: uno.rc besides cppuhelper lib) + @param bootstrap_parameters + bootstrap parameters (maybe null) + + @see cppuhelper/defaultBootstrap_InitialComponentContext() + */ + @SuppressWarnings("unchecked") + static public final XComponentContext defaultBootstrap_InitialComponentContext( + String ini_file, Hashtable bootstrap_parameters ) + throws Exception + { + // jni convenience: easier to iterate over array than calling Hashtable + String pairs [] = null; + if (null != bootstrap_parameters) + { + pairs = new String [ 2 * bootstrap_parameters.size() ]; + Enumeration keys = bootstrap_parameters.keys(); + int n = 0; + while (keys.hasMoreElements()) + { + String name = (String)keys.nextElement(); + pairs[ n++ ] = name; + pairs[ n++ ] = (String)bootstrap_parameters.get( name ); + } + } + + if (! m_loaded_juh) + { + System.loadLibrary( "juh" ); + m_loaded_juh = true; + } + return (XComponentContext)UnoRuntime.queryInterface( + XComponentContext.class, + cppuhelper_bootstrap( + ini_file, pairs, Bootstrap.class.getClassLoader() ) ); + } + + static private boolean m_loaded_juh = false; + static private native Object cppuhelper_bootstrap( + String ini_file, String bootstrap_parameters [], ClassLoader loader ) + throws Exception; + + /** + * Bootstraps the component context from a UNO installation. + * + * @return a bootstrapped component context. + * + * @since UDK 3.1.0 + */ + public static final XComponentContext bootstrap(URLClassLoader loader) + throws BootstrapException { + + XComponentContext xContext = null; + + try { + // create default local component context + XComponentContext xLocalContext = + createInitialComponentContext( null ); + if ( xLocalContext == null ) + throw new BootstrapException( "no local component context!" ); + + // find office executable relative to this class's class loader + String sOffice = + System.getProperty( "os.name" ).startsWith( "Windows" ) ? + "soffice.exe" : "soffice"; + File fOffice = NativeLibraryLoader.getResource( loader, sOffice ); + if ( fOffice == null ) + throw new BootstrapException( "no office executable found!" ); + + // create random pipe name + String sPipeName = "uno" + + Long.toString( (new Random()).nextLong() & 0x7fffffffffffffffL ); + + // create call with arguments + String[] cmdArray = new String[7]; + cmdArray[0] = fOffice.getPath(); + cmdArray[1] = "-nologo"; + cmdArray[2] = "-nodefault"; + cmdArray[3] = "-norestore"; + cmdArray[4] = "-nocrashreport"; + cmdArray[5] = "-nolockcheck"; + cmdArray[6] = "-accept=pipe,name=" + sPipeName + ";urp;"; + + // start office process + Process p = Runtime.getRuntime().exec( cmdArray ); + pipe( p.getInputStream(), System.out, "CO> " ); + pipe( p.getErrorStream(), System.err, "CE> " ); + + // initial service manager + XMultiComponentFactory xLocalServiceManager = + xLocalContext.getServiceManager(); + if ( xLocalServiceManager == null ) + throw new BootstrapException( "no initial service manager!" ); + + // create a URL resolver + XUnoUrlResolver xUrlResolver = + UnoUrlResolver.create( xLocalContext ); + + + // connection string + String sConnect = "uno:pipe,name=" + sPipeName + + ";urp;StarOffice.ComponentContext"; + + // wait until office is started + for (int i = 0;; ++i) { + try { + // try to connect to office + Object context = xUrlResolver.resolve( sConnect ); + xContext = (XComponentContext) UnoRuntime.queryInterface( + XComponentContext.class, context); + if ( xContext == null ) + throw new BootstrapException( "no component context!" ); + break; + } catch ( com.sun.star.connection.NoConnectException ex ) { + // Wait 500 ms, then try to connect again, but do not wait + // longer than 5 min (= 600 * 500 ms) total: + if (i == 600) { + throw new BootstrapException(ex.toString()); + } + Thread.sleep( 500 ); + } + } + } catch ( BootstrapException e ) { + throw e; + } catch ( java.lang.RuntimeException e ) { + throw e; + } catch ( java.lang.Exception e ) { + throw new BootstrapException( e ); + } + + return xContext; + } + + private static void pipe( + final InputStream in, final PrintStream out, final String prefix ) { + + new Thread( "Pipe: " + prefix) { + public void run() { + BufferedReader r = new BufferedReader( + new InputStreamReader( in ) ); + try { + for ( ; ; ) { + String s = r.readLine(); + if ( s == null ) { + break; + } + out.println( prefix + s ); + } + } catch ( java.io.IOException e ) { + e.printStackTrace( System.err ); + } + } + }.start(); + } +} diff --git a/src/org/libreoffice/tools/types/Interface.java b/src/org/libreoffice/tools/types/Interface.java new file mode 100644 index 0000000..871a1ea --- /dev/null +++ b/src/org/libreoffice/tools/types/Interface.java @@ -0,0 +1,71 @@ +package org.libreoffice.tools.types; + +import java.util.ArrayList; + +import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; +import com.sun.star.reflection.XInterfaceMemberTypeDescription; +import com.sun.star.reflection.XInterfaceTypeDescription2; +import com.sun.star.reflection.XTypeDescription; +import com.sun.star.uno.TypeClass; +import com.sun.star.uno.UnoRuntime; + +public class Interface { + + XInterfaceTypeDescription2 mDescr; + + public Interface( XTypeDescription pDescr ) { + if ( !pDescr.getTypeClass().equals( TypeClass.INTERFACE ) ) { + throw new IllegalArgumentException( "Not an interface description" ); + } + + mDescr = (XInterfaceTypeDescription2)UnoRuntime.queryInterface( + XInterfaceTypeDescription2.class, pDescr ); + } + + public String getName( ) { + return mDescr.getName(); + } + + public void checkAttributes( ArrayList pAttributes, ArrayList pPropinterfaces) { + String typeName = getName( ); + if ( typeName.equals( "com.sun.star.beans.XPropertySet" ) || + typeName.equals( "com.sun.star.beans.XFastPropertySet" ) || + typeName.equals( "com.sun.star.beans.XPropertyAccess" ) ) { + pPropinterfaces.add( typeName ); + } + + // Check the attributes in the base types + XTypeDescription[] baseTypes = mDescr.getBaseTypes(); + for (XTypeDescription baseType : baseTypes) { + Interface iface = new Interface( baseType ); + iface.checkAttributes( pAttributes, pPropinterfaces ); + } + + // Check the attributes of the interface + XInterfaceMemberTypeDescription[] members = mDescr.getMembers(); + for (XInterfaceMemberTypeDescription member : members) { + XInterfaceAttributeTypeDescription2 attr = (XInterfaceAttributeTypeDescription2) + UnoRuntime.queryInterface( XInterfaceAttributeTypeDescription2.class, member ); + if ( attr != null ) { + pAttributes.add( attr ); + } + } + } + + public boolean checkXComponentSupport() { + boolean supported = false; + + if ( getName().equals( "com.sun.star.lang.XComponent" ) ) { + supported = true; + } else { + XTypeDescription[] baseTypes = mDescr.getBaseTypes(); + int i = 0; + while ( i < baseTypes.length && !supported ) { + Interface iface = new Interface( baseTypes[i] ); + supported = iface.checkXComponentSupport(); + } + } + + return supported; + } +} diff --git a/src/org/libreoffice/tools/types/Service.java b/src/org/libreoffice/tools/types/Service.java new file mode 100644 index 0000000..6f391d3 --- /dev/null +++ b/src/org/libreoffice/tools/types/Service.java @@ -0,0 +1,116 @@ +package org.libreoffice.tools.types; + +import java.util.ArrayList; +import java.util.Arrays; + +import com.sun.star.reflection.XPropertyTypeDescription; +import com.sun.star.reflection.XServiceTypeDescription2; +import com.sun.star.reflection.XTypeDescription; +import com.sun.star.uno.TypeClass; +import com.sun.star.uno.UnoRuntime; + +/** + * Wrapper class to get the informations on a service. + * + * @author cbosdonnat + * + */ +public class Service { + + XServiceTypeDescription2 mDescr; + ArrayList mRefs; + + + public Service( XTypeDescription pDescr ) { + if ( !pDescr.getTypeClass().equals( TypeClass.SERVICE ) ) { + throw new IllegalArgumentException( "Not a service description" ); + } + + mDescr = (XServiceTypeDescription2)UnoRuntime.queryInterface( + XServiceTypeDescription2.class, pDescr ); + + mRefs = new ArrayList(); + if ( !isNewStyleService() ) { + mRefs.addAll( Arrays.asList( mDescr.getMandatoryServices() ) ); + mRefs.addAll( Arrays.asList( mDescr.getOptionalServices() ) ); + mRefs.addAll( Arrays.asList( mDescr.getMandatoryInterfaces() ) ); + mRefs.addAll( Arrays.asList( mDescr.getOptionalInterfaces() ) ); + } + } + + public XServiceTypeDescription2 getDescription( ) { + return mDescr; + } + + public boolean isNewStyleService( ) { + return mDescr.isSingleInterfaceBased(); + } + + public XTypeDescription getSupertype( ) { + XTypeDescription superType = null; + if ( isNewStyleService() ) { + superType = mDescr.getInterface(); + } + return superType; + } + + public int getReferenceCount( ) { + return mRefs.size(); + } + + public String getReferenceTypeName( int pRefNumber ) { + String name = new String( ); + + XTypeDescription ref = mRefs.get( pRefNumber ); + if ( ref != null ) { + name = ref.getName(); + } + + return name; + } + + public int getMethodCount( ) { + return mDescr.getConstructors().length; + } + + public String getMethodName( int pMethodNumber ) { + String name = new String(); + if ( pMethodNumber >= 0 && pMethodNumber < getMethodCount() ) { + name = mDescr.getConstructors()[ pMethodNumber ].getName(); + } + return name; + } + + public int getPropertiesCount( ) { + int count = 0; + if ( !isNewStyleService() ) { + count = mDescr.getProperties().length; + } + return count; + } + + public XPropertyTypeDescription getProperty( int pPropNumber ) { + XPropertyTypeDescription result = null; + if ( pPropNumber >= 0 && pPropNumber < getPropertiesCount() ) { + result = mDescr.getProperties()[ pPropNumber ]; + } + return result; + } + + public boolean hasProperties() { + + boolean hasProps = getPropertiesCount() > 0; + + // Check the references for properties + int i = 0; + while ( !hasProps && i < getReferenceCount() ) { + XTypeDescription desc = mRefs.get( i ); + if ( desc.getTypeClass().equals( TypeClass.SERVICE ) ) { + Service service = new Service( desc ); + hasProps = service.hasProperties(); + } + } + + return hasProps; + } +} diff --git a/src/org/libreoffice/tools/types/TypeManager.java b/src/org/libreoffice/tools/types/TypeManager.java new file mode 100644 index 0000000..355d755 --- /dev/null +++ b/src/org/libreoffice/tools/types/TypeManager.java @@ -0,0 +1,301 @@ +package org.libreoffice.tools.types; + +import java.io.File; +import java.io.FileInputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Iterator; + +import org.apache.commons.cli.CommandLine; + +import com.sun.star.container.NoSuchElementException; +import com.sun.star.container.XHierarchicalNameAccess; +import com.sun.star.container.XSet; +import com.sun.star.frame.XDesktop; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; +import com.sun.star.reflection.XPropertyTypeDescription; +import com.sun.star.reflection.XTypeDescription; +import com.sun.star.registry.XSimpleRegistry; +import com.sun.star.uno.RuntimeException; +import com.sun.star.uno.TypeClass; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +/* + * TODO: + * + Load the registries + * + use the introspection API to extract the useful infos from the type + */ +public class TypeManager { + + public static final String NOPROPERTYHELPER = "_"; + + private String mInstallDir; + private XComponentContext mCtxt; + private XHierarchicalNameAccess mTypeDescMngr; + + public TypeManager( String pInstallDir ) { + mInstallDir = pInstallDir; + connect( ); + } + + public void dispose( ) { + disconnect(); + } + + public void initRegistries( String[] pUrls ) throws Exception { + // Get the type description manager + Object o = mCtxt.getValueByName( "/singletons/com.sun.star.reflection.theTypeDescriptionManager" ); + mTypeDescMngr = (XHierarchicalNameAccess)UnoRuntime.queryInterface( XHierarchicalNameAccess.class, o ); + + if ( mTypeDescMngr == null ) { + throw new RuntimeException( "Couldn't get the type description manager from OOo" ); + } + + // Get the service manager + XMultiComponentFactory xServiceMngr = mCtxt.getServiceManager(); + + // Open the registries + XSimpleRegistry[] regs = new XSimpleRegistry[ pUrls.length ]; + for ( int i = 0; i < pUrls.length; i++ ) { + String regUrl = pUrls[i]; + o = xServiceMngr.createInstanceWithContext( "com.sun.star.registry.SimpleRegistry", mCtxt ); + XSimpleRegistry reg = (XSimpleRegistry)UnoRuntime.queryInterface( XSimpleRegistry.class, o ); + reg.open( regUrl, true, false); + regs[i] = reg; + } + + // Fill the type description manager + o = xServiceMngr.createInstanceWithArgumentsAndContext( + "com.sun.star.reflection.TypeDescriptionProvider", regs, mCtxt ); + XHierarchicalNameAccess tdProvider = (XHierarchicalNameAccess)UnoRuntime.queryInterface( + XHierarchicalNameAccess.class, o ); + if ( tdProvider == null ) { + throw new RuntimeException( "Can't initialize the TypeDescriptionProvider" ); + } + + XSet mngrSet = (XSet)UnoRuntime.queryInterface( XSet.class, mTypeDescMngr ); + mngrSet.insert( tdProvider ); + } + + public void checkType( String pTypeName, ArrayList pServices, ArrayList pInterfaces, + ArrayList< XPropertyTypeDescription > pMembers ) { + try { + XTypeDescription descr = searchType( pTypeName ); + + switch ( descr.getTypeClass().getValue() ) { + case TypeClass.INTERFACE_value: + // Don't add the css.lang.XTypeProvider and cxx.uno.XWeak + if ( !pTypeName.equals( "com.sun.star.lang.XTypeProvider" ) && + !pTypeName.equals( "com.sun.star.uno.XWeak" ) && + !pInterfaces.contains( pTypeName ) ) { + pInterfaces.add( pTypeName ); + } + break; + case TypeClass.SERVICE_value: + if ( !pServices.contains( pTypeName ) ) { + pServices.add( pTypeName ); + + Service service = new Service( descr ); + if ( service.getSupertype() != null ) { + // New style services + + String supertype = service.getSupertype().getName(); + if ( !pInterfaces.contains( supertype ) ) { + pInterfaces.add( supertype ); + } + + // check if constructors are specified, if yes automatically + // support of XInitialization. We will take care of the default + // constructor because in this case XInitialization is not called. + if ( service.getMethodCount() > 1 || + service.getMethodCount() == 1 && service.getMethodName( 0 ).length() > 0 ) { + String initIterface = "com.sun.star.lang.XInitialization"; + if ( !pInterfaces.contains( initIterface ) ) { + pInterfaces.add( initIterface ); + } + } + } else { + // Old style services + + for ( int i = 0, len = service.getReferenceCount(); i < len; i++ ) { + String refName = service.getReferenceTypeName( i ); + checkType( refName, pServices, pInterfaces, pMembers ); + } + + // Get the properties + for ( int i = 0, len = service.getPropertiesCount(); i < len; i++ ) { + pMembers.add( service.getProperty( i ) ); + } + } + } + break; + } + } catch ( Exception e ) { + } + } + + public String checkPropertyHelper(CommandLine pArgs, ArrayList pServices, ArrayList pInterfaces, + ArrayList pAttributes, ArrayList pPropinterfaces) { + + String result = new String(); + + Iterator it = pInterfaces.iterator(); + if ( !pServices.isEmpty() ) { + it = pServices.iterator(); + } + + boolean oldStyleWithProps = false; + boolean propMixin = Boolean.parseBoolean( pArgs.getOptionValue( "propertysetmixin" ) ); + while ( it.hasNext() && result.isEmpty() ) { + String typename = it.next( ); + try { + XTypeDescription typeDesc = searchType( typename ); + if ( !pServices.isEmpty() ) { + Service service = new Service( typeDesc ); + + if ( propMixin && service.getSupertype() != null ) { + Interface iface = new Interface( service.getSupertype() ); + iface.checkAttributes( pAttributes, pPropinterfaces ); + + if ( !pAttributes.isEmpty() && !pPropinterfaces.isEmpty() ) { + result = service.getSupertype().getName(); + } + } else { + oldStyleWithProps = service.hasProperties(); + } + } else { + Interface iface = new Interface( typeDesc ); + iface.checkAttributes( pAttributes, pPropinterfaces ); + if ( !pAttributes.isEmpty() && !pPropinterfaces.isEmpty() ) { + result = typename; + } + } + } catch( Exception e ) { + } + } + + if ( result.isEmpty() && oldStyleWithProps ) { + result = NOPROPERTYHELPER; + } + + return result; + } + + public boolean checkXComponentSupport( ArrayList pInterfaces ) { + + boolean supported = false; + + if ( pInterfaces.isEmpty() ) { + supported = false; + } else { + + Iterator it = pInterfaces.iterator(); + while ( it.hasNext() && !supported ) { + String typeName = it.next( ); + if ( typeName.equals( "com.sun.star.lang.XComponent" ) ) { + pInterfaces.remove( "com.sun.star.lang.XComponent" ); + supported = true; + } else { + try { + XTypeDescription descr = searchType( typeName ); + Interface iface = new Interface( descr ); + supported = iface.checkXComponentSupport( ); + } catch ( NoSuchElementException e ) { + } + } + } + } + + return supported; + } + + /** + * Search the description of a given UNO type in the registries. + * + * @param pTypeName the name of the type to look for, separated by '.' + * + * @return the type description + * + * @throws NoSuchElementException if no element is corresponding to the asked type. + */ + public XTypeDescription searchType( String pTypeName ) throws NoSuchElementException { + Object o = mTypeDescMngr.getByHierarchicalName( pTypeName ); + return (XTypeDescription)UnoRuntime.queryInterface( XTypeDescription.class, o ); + } + + private void connect( ) { + try { + File installDir = new File( mInstallDir ); + File programDir = new File( installDir, "program" ); + File basisDir = getPortableLink( installDir, "basis-link" ); + File ureDir = getPortableLink( basisDir, "ure-link" ); + + URL[] urls = new URL[] { + programDir.toURI().toURL(), + new File( ureDir, "lib" ).toURI().toURL(), + new File( ureDir, "share/java" ).toURI().toURL(), + new File( basisDir, "program" ).toURI().toURL(), + new File( basisDir, "program/classes" ).toURI().toURL() + }; + + URLClassLoader loader = new URLClassLoader( urls ); + mCtxt = Bootstrap.bootstrap( loader ); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Get the file object for the link defined as a child of a folder. + * + * On Windows platform, the link relative location is specified as the content of a file named after the link + * name. On Unix-based systems symbolic links are supported. + * + * @param pName the name of the symbolic link + * @param pParent the parent directory file + * + * @return the file representing the link target or null + */ + private File getPortableLink( File pParent, String pName ) { + File link = null; + + File linkFile = new File( pParent, pName ); + if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "win" ) >= 0 ) { + // Read the content of the file to get the true folder + try { + FileInputStream is = new FileInputStream( linkFile ); + byte[] buf = new byte[is.available()]; + is.read( buf ); + + String relativePath = new String( buf ); + linkFile = new File( pParent, relativePath ); + link = linkFile; + } catch ( Exception e ) { + // the returned link is null to show the error + } + } else { + link = linkFile; + } + + return link; + } + + private void disconnect( ) { + try { + if ( mCtxt != null ) { + // Only the uno test suite which started the office can stop it + XMultiComponentFactory xMngr = mCtxt.getServiceManager(); + Object oDesktop = xMngr.createInstanceWithContext("com.sun.star.frame.Desktop", mCtxt); + XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, oDesktop); + + xDesktop.terminate(); + mCtxt = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/org/openoffice/tools/DumpException.java b/src/org/openoffice/tools/DumpException.java deleted file mode 100644 index faf19b8..0000000 --- a/src/org/openoffice/tools/DumpException.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.openoffice.tools; - -/** - * Exception used for all the Uno-skeletonmaker specific problems. - * - * @author cbosdonnat - * - */ -public class DumpException extends Exception { - - public DumpException( String pMessage ) { - super( pMessage ); - } - - private static final long serialVersionUID = -3757702196179358713L; - -} diff --git a/src/org/openoffice/tools/OptionsHelper.java b/src/org/openoffice/tools/OptionsHelper.java deleted file mode 100644 index c1adcf9..0000000 --- a/src/org/openoffice/tools/OptionsHelper.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.openoffice.tools; - -import org.apache.commons.cli.Option; -import org.apache.commons.cli.OptionGroup; -import org.apache.commons.cli.Options; -import org.openoffice.tools.language.Language; -import org.openoffice.tools.language.LanguageHelper; -import org.openoffice.tools.options.Command; -import org.openoffice.tools.options.SubCommand; - -/** - * Helper class generating the options list. - * - * @author cbosdonnat - * - */ -public class OptionsHelper { - - public static final String COMMAND_COMPONENT = "component"; - public static final String COMMAND_REGISTRATION = "registration"; - - // TODO Complete the arguments to handle here - protected static Command setupCommandLine( ) { - Command cmdLine = new Command(); - - // General options - Options generalOptions = new Options(); - - OptionGroup langGroup = new OptionGroup(); - String[] langs = LanguageHelper.getLanguagesIds( ); - for (String lang : langs) { - Language langDef = LanguageHelper.getLanguage( lang ); - Option langOpt = new Option( lang, false, langDef.getDescription() ); - langGroup.addOption( langOpt ); - } - langGroup.setRequired( true ); - generalOptions.addOptionGroup( langGroup ); - - generalOptions.addOption( "unoinstall", true, "url specifies a URL to an existing UNO environment (URE, office installation)." ); - generalOptions.addOption( "o", true, "path specifies an existing directory where the output files are generated to. If path=stdout the generated code is generated on standard out instead of a file." ); - - cmdLine.setCommonOptions( generalOptions ); - - // component command options - Options compOptions = new Options( ); - compOptions.addOption( "t", true, "specifies an UNOIDL type name, e.g. com.sun.star.text.XText" ); - compOptions.addOption( "l", true, "specifies a binary type library (can be used more than once). The type library is integrated as an additional type provider in the bootstrapped type system."); - compOptions.addOption( "n", true, "specifies an implementation name for the component (used as classname, filename and package|namespace name). In 'dump' mode it is used as classname (e.g. \"MyBase::\", C++ only) to generate method bodies not inline." ); - compOptions.addOption( "propertysetmixin", false, "the generated skeleton implements the cppu::PropertySetMixin helper if a referenced new style service specifies an interface which provides attributes (directly or inherited)." ); - SubCommand component = new SubCommand( COMMAND_COMPONENT, compOptions, - "generates language specific code skeleton files using the implementation name as the file and class name" ); - cmdLine.addCommand( component ); - - // registration options - Options regOptions = new Options(); - regOptions.addOption( "s", true, "name of the implementation of a service to add to the registration system." ); - SubCommand registration = new SubCommand( COMMAND_REGISTRATION, regOptions, - "creates the registration code for a list of services implementations. The '-s' option has to be used at least once to specify the implementation name of a service to add to the registration system." ); - cmdLine.addCommand( registration ); - - return cmdLine; - } -} diff --git a/src/org/openoffice/tools/SkeletonMaker.java b/src/org/openoffice/tools/SkeletonMaker.java deleted file mode 100644 index febb932..0000000 --- a/src/org/openoffice/tools/SkeletonMaker.java +++ /dev/null @@ -1,136 +0,0 @@ -package org.openoffice.tools; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; - -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; -import org.apache.commons.cli.PosixParser; -import org.openoffice.tools.language.Language; -import org.openoffice.tools.language.LanguageHelper; -import org.openoffice.tools.options.Command; -import org.openoffice.tools.options.HelpFormatter; -import org.openoffice.tools.options.SubcommandLine; -import org.openoffice.tools.types.TypeManager; - -import freemarker.template.Template; - -public class SkeletonMaker { - - // The file template names - public static final String COMPONENT = "component.tpl"; - - public static void generateComponent( CommandLine pArgs ) { - - TypeManager manager = null; - PrintWriter writer = null; - - try { - // Load the types manager - String inifile = pArgs.getOptionValue( "unoinstall" ); - manager = new TypeManager( inifile ); - manager.initRegistries( pArgs.getOptionValues( "l" ) ); - - // Extract all the informations from the types - TypeChecker checker = new TypeChecker( manager ); - checker.check( pArgs ); - - // Print everything - writer = getWriter( pArgs ); - - Language lang = getLanguage( pArgs ); - Template tpl = lang.getTemplate( COMPONENT ); - tpl.process( checker.getTemplateModel( ), writer ); - writer.flush(); - - } catch ( Exception e ) { - e.printStackTrace(); - } finally { - // Close the file stream if possible - try { writer.close(); } catch ( Exception e ) { } - - // Close the running OOo - if ( manager != null ) { - manager.dispose(); - } - } - } - - private static PrintWriter getWriter( CommandLine pArgs ) throws IOException { - PrintWriter writer = new PrintWriter( System.out ); - String outPath = pArgs.getOptionValue( "o" ); - if ( outPath != null && !outPath.equals( "stdout" ) ) { - String implName = pArgs.getOptionValue( "n" ).replace( '.', '/' ); - Language lang = getLanguage( pArgs ); - String ext = lang.getExtension(); - - String targetPath = getFilenameFromType( outPath, implName, ext ); - - File out = new File( targetPath ); - writer = new PrintWriter( out ); - } - - return writer; - } - - private static String getFilenameFromType(String pOutPath, - String pImplName, String pExt) { - StringBuffer buf = new StringBuffer(); - if ( pOutPath.isEmpty() ) { - buf.append( "." ); - } else { - buf.append( pOutPath ); - } - - String sep = System.getProperty( "file.separator" ); - - buf.append( sep ); - buf.append( pImplName ); - buf.append( pExt ); - - String filename = buf.toString(); - // Clean up the separators - if ( sep.equals( "/" ) ) { - filename = filename.replace( "\\\\", sep ); - } else { - filename = filename.replace( "/", sep ); - } - - return filename; - } - - public static void main(String[] args) { - - Command cmd = OptionsHelper.setupCommandLine(); - try { - SubcommandLine commandline = cmd.parse( args, new PosixParser() ); - - // TODO Add the other commands - String subCommand = commandline.getSubcommand(); - if ( subCommand.equals( OptionsHelper.COMMAND_COMPONENT ) ) { - generateComponent( commandline.getCommandLine() ); - - } else if ( subCommand.equals( OptionsHelper.COMMAND_REGISTRATION ) ) { - // TODO implement - } - } catch (ParseException e) { - System.err.println( e.getMessage() ); - - HelpFormatter.printHelp( "uno-skeletonmaker", cmd ); - } - } - - private static Language getLanguage( CommandLine pArgs ) { - String[] langs = LanguageHelper.getLanguagesIds(); - String lang = null; - int i = 0; - while ( lang == null && i < langs.length ) { - if ( pArgs.hasOption( langs[i] ) ) { - lang = langs[i]; - } - i++; - } - return LanguageHelper.getLanguage( lang ); - } -} diff --git a/src/org/openoffice/tools/TypeChecker.java b/src/org/openoffice/tools/TypeChecker.java deleted file mode 100644 index 30c88af..0000000 --- a/src/org/openoffice/tools/TypeChecker.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.openoffice.tools; - -import java.util.ArrayList; - -import org.apache.commons.cli.CommandLine; -import org.openoffice.tools.types.TypeManager; - -import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; -import com.sun.star.reflection.XPropertyTypeDescription; - -public class TypeChecker { - - private static final int MAX_INTERFACES = 12; - - private TypeManager mManager; - - private boolean mSupportXComp; - private ArrayList mInterfaces; - private ArrayList mServices; - private ArrayList mProperties; - private ArrayList mAttributes; - private ArrayList mPropinterfaces; - private String mPropertyHelper; - - - public TypeChecker(TypeManager pManager) { - mManager = pManager; - - mInterfaces = new ArrayList(); - mServices = new ArrayList(); - mProperties = new ArrayList(); - mAttributes = new ArrayList(); - mPropinterfaces = new ArrayList(); - mPropertyHelper = null; - } - - public boolean isSupportXComp() { - return mSupportXComp; - } - - public ArrayList getInterfaces() { - return mInterfaces; - } - - public ArrayList getServices() { - return mServices; - } - - public ArrayList getProperties() { - return mProperties; - } - - public ArrayList getAttributes() { - return mAttributes; - } - - public ArrayList getPropinterfaces() { - return mPropinterfaces; - } - - public String getPropertyHelper() { - return mPropertyHelper; - } - - public void check( CommandLine pArgs ) throws DumpException { - // Check the types - String[] types = pArgs.getOptionValues( "t" ); - - - // Get the service, interfaces and members from the types - for (String type : types) { - mManager.checkType( type, mServices, mInterfaces, mProperties ); - } - - // TODO For ProtocolHandler case, check the frame.ProtocolHandler and frame.XDispatch types - - // Check for the property helper - mPropertyHelper = mManager.checkPropertyHelper( pArgs, mServices, mInterfaces, mAttributes, mPropinterfaces ); - - // Check the default interfaces - checkDefaultInterfaces( ); - - if ( mInterfaces.size() > MAX_INTERFACES ) { - throw new DumpException( "The skeletonmaker supports components with 12 interfaces only (limitation of the UNO implementation helpers)!" ); - } - - mSupportXComp = mManager.checkXComponentSupport( mInterfaces ); - } - - /** - * Variables usable in the template model: - *

    - *
  • hasLicense: boolean
  • - *
  • serviceObject: boolean
  • - *
  • supportsXComponent: boolean
  • - *
  • interfaces: array of interfaces properties
  • - *
  • propertyHelper: string (empty string or the property helper name)
  • - *
  • classname: string (the name of the class to create)
  • - *
- * - * Interface objet: - *
    - *
  • name: string for the interface name
  • - *
  • - *
- * @return - */ - public Object getTemplateModel() { - // TODO Setup the template model using the checked infos. - return null; - } - - private void checkDefaultInterfaces( ) { - - if ( mServices.isEmpty() ) { - mInterfaces.remove( "com.sun.star.lang.XServiceInfo" ); - } else { - if ( !mInterfaces.contains( "com.sun.star.lang.XServiceInfo" ) ) { - mInterfaces.add( "com.sun.star.lang.XServiceInfo" ); - } - } - - if ( mPropertyHelper.equals( TypeManager.NOPROPERTYHELPER ) ) { - mInterfaces.remove( "com.sun.star.beans.XPropertySet" ); - mInterfaces.remove( "com.sun.star.beans.XFastPropertySet" ); - mInterfaces.remove( "com.sun.star.beans.XPropertyAccess" ); - } - } -} diff --git a/src/org/openoffice/tools/language/Language.java b/src/org/openoffice/tools/language/Language.java deleted file mode 100644 index a4e9d31..0000000 --- a/src/org/openoffice/tools/language/Language.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.openoffice.tools.language; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.NoSuchElementException; -import java.util.Properties; - -import freemarker.template.Configuration; -import freemarker.template.Template; - -/** - * TODO Implement hierarchical language definitions. - * - * @author cbosdonnat - * - */ -public class Language { - - private static final String PROP_DESCRIPTION = "description"; - private static final String PROP_EXTENSION = "extension"; - - private static final String DESCRIPTION_FILENAME = "lang.properties"; - - private String mId; - private File mLangDir; - - private Configuration mTemplateConfig; - - private Properties mProps; - - /** - * Constructor. - * - * @param pId the language identifier. - * @param pDir the language root directory (eg. cpp for C++). - * - * @throws IOException if the file provided isn't a proper language directory. - */ - public Language( String pId, File pDir ) throws IOException { - - mId = pId; - if ( !pDir.isDirectory() ) { - throw new IOException( "Invalid language directory" ); - } - mLangDir = pDir; - - // Define a template config for each language - mTemplateConfig = new Configuration(); - mTemplateConfig.setTemplateLoader( new LanguageTemplateLoader( this ) ); - } - - public String getDescription( ) { - return getProperty( PROP_DESCRIPTION ); - } - - public String getExtension( ) { - return getProperty( PROP_EXTENSION ); - } - - public String getProperty( String pPropName ) { - if ( mProps == null ) { - loadProperties( ); - } - String serialized = null; - Object value = mProps.get( pPropName ); - if ( value != null ) { - serialized = value.toString(); - } - return serialized; - } - - public Template getTemplate( String pName ) throws IOException { - return mTemplateConfig.getTemplate( pName ); - } - - protected File getTemplateFile( String pName ) { - File result = null; - - result = new File( mLangDir, pName ); - if ( !result.isFile() || !result.canRead() ) { - String msg = MessageFormat.format( "No template {0} for language {1}", pName, mId ); - throw new NoSuchElementException( msg ); - } - - return result; - } - - private void loadProperties() { - mProps = new Properties(); - File descFile = new File( mLangDir, DESCRIPTION_FILENAME ); - if ( descFile.isFile() && descFile.canRead() ) { - Properties props = new Properties( ); - FileInputStream in = null; - try { - in = new FileInputStream( descFile ); - props.load( in ); - mProps.putAll( props ); - } catch ( Exception e ) { - } finally { - try { in.close(); } catch ( Exception e) { }; - } - } - } -} diff --git a/src/org/openoffice/tools/language/LanguageHelper.java b/src/org/openoffice/tools/language/LanguageHelper.java deleted file mode 100644 index b2d23b0..0000000 --- a/src/org/openoffice/tools/language/LanguageHelper.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.openoffice.tools.language; - -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.net.URISyntaxException; - -/** - * Utility methods handling the language definitions and templates. - * - * TODO Store the language objects in a static way for perf. reasons - * - * @author cbosdonnat - * - */ -public class LanguageHelper { - - private static final String DEFAULT_LANGS_DIR = "langs"; - - private static File sLangsDir; - - /** - * Changes the languages configuration directory. - * - * @param pConfigDir the new directory containing the languages definitions. - */ - public static void setLanguagesDir( File pConfigDir ) { - sLangsDir = pConfigDir; - } - - public static String[] getLanguagesIds() { - File langsDir = getLangsDir(); - - String[] langs = langsDir.list( new FilenameFilter() { - - @Override - public boolean accept(File pDir, String pName) { - return !pName.startsWith("."); - } - }); - - return langs; - } - - public static Language getLanguage( String pLang ) { - Language language = null; - try { - language = new Language( pLang, new File( getLangsDir( ), pLang ) ); - } catch (IOException e) { - e.printStackTrace(); - } - return language; - } - - private static File getLangsDir( ) { - - if ( sLangsDir == null ) { - try { - File jarFile = new File(LanguageHelper.class.getProtectionDomain() - .getCodeSource().getLocation().toURI()); - sLangsDir = new File( jarFile.getParentFile(), DEFAULT_LANGS_DIR ); - - } catch (URISyntaxException e) { - } - } - return sLangsDir; - } -} diff --git a/src/org/openoffice/tools/language/LanguageTemplateLoader.java b/src/org/openoffice/tools/language/LanguageTemplateLoader.java deleted file mode 100644 index 4f6226f..0000000 --- a/src/org/openoffice/tools/language/LanguageTemplateLoader.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.openoffice.tools.language; - -import java.io.File; -import java.io.IOException; -import java.util.NoSuchElementException; - -import freemarker.cache.FileTemplateLoader; - -public class LanguageTemplateLoader extends FileTemplateLoader { - - private Language mLang; - - public LanguageTemplateLoader( Language pLangDef ) throws IOException { - mLang = pLangDef; - } - - /** - * Delegate the template finding to the language itself. - * - * @param pName the template name to find - * - * @return the template File if found, or null. - */ - @Override - public Object findTemplateSource(String pName ) throws IOException { - File file = null; - try { - file = mLang.getTemplateFile( pName ); - } catch ( NoSuchElementException e ) { - } - return file; - } -} diff --git a/src/org/openoffice/tools/options/Command.java b/src/org/openoffice/tools/options/Command.java deleted file mode 100644 index 480ae90..0000000 --- a/src/org/openoffice/tools/options/Command.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.openoffice.tools.options; - -import java.util.Collection; -import java.util.HashMap; - -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.OptionGroup; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; - -public class Command { - - private HashMap< String, SubCommand > mCommands; - private Options mCommonOptions; - - public Command( ) { - mCommands = new HashMap(); - } - - public void addCommand( SubCommand pCommand ) { - if ( !mCommands.containsKey( pCommand.getName() ) ) { - mCommands.put( pCommand.getName(), pCommand ); - } - } - - public void removeCommand( String pCommandName ) { - mCommands.remove( pCommandName ); - } - - public Collection< SubCommand > getCommands( ) { - return mCommands.values(); - } - - public void setCommonOptions( Options pOptions ) { - mCommonOptions = pOptions; - } - - public Options getCommonOptions( ) { - return mCommonOptions; - } - - public SubcommandLine parse( String[] pArguments, CommandLineParser pParser ) throws ParseException { - if ( pArguments.length < 1 ) { - throw new ParseException( "No sub-command" ); - } - String subcommand = pArguments[0]; - - // Compute the final options list: common + subcommand - Options allOptions = new Options(); - addAllOptions( allOptions, mCommonOptions ); - - SubCommand def = mCommands.get( subcommand ); - if ( def == null ) { - throw new ParseException( "Invalid sub-command: " + subcommand ); - } - - addAllOptions( allOptions, def.getOptions() ); - - // Get only the subcommand arguments - String[] subCommandArguments = new String[ pArguments.length - 1 ]; - System.arraycopy( pArguments, 1, subCommandArguments, 0, subCommandArguments.length ); - - // Parse - CommandLine cmdline = pParser.parse( allOptions, subCommandArguments ); - - return new SubcommandLine( subcommand, cmdline ); - } - - private void addAllOptions(Options pAllOptions, Options pCommonOptions) { - for ( Object opt : pCommonOptions.getOptions() ) { - if ( opt instanceof Option ) { - pAllOptions.addOption( ( Option ) opt ); - } else if ( opt instanceof OptionGroup ) { - pAllOptions.addOptionGroup( (OptionGroup ) opt ); - } - } - } -} diff --git a/src/org/openoffice/tools/options/HelpFormatter.java b/src/org/openoffice/tools/options/HelpFormatter.java deleted file mode 100644 index d61ea0d..0000000 --- a/src/org/openoffice/tools/options/HelpFormatter.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.openoffice.tools.options; - -import java.io.PrintStream; -import java.io.PrintWriter; -import java.util.Arrays; -import java.util.Collection; - -public class HelpFormatter { - - private static final int TAB_SPACES = 4; - private static final int COMMAND_SEP_SPACES = 2; - - public static void printHelp( String pProgram, Command pCommandLine ) { - printHelp( System.out, 80, pProgram, pCommandLine ); - } - - public static void printHelp( PrintStream pOut, int pMaxLength, String pProgram, Command pCommandLine ) { - - org.apache.commons.cli.HelpFormatter formatter = new org.apache.commons.cli.HelpFormatter(); - formatter.setSyntaxPrefix( getFilledString( ' ', TAB_SPACES ) ); - PrintWriter pw = new PrintWriter( pOut ); - - // Output the global usage - pOut.println( "\nUsage:" ); - for ( SubCommand command : pCommandLine.getCommands() ) { - String app = pProgram + " " + command.getName(); - formatter.printUsage( pw, pMaxLength, app, command.getOptions() ); - } - pw.flush(); - - // Output the commands list - pOut.println( "\nSub-commands:" ); - printSubCommands( pOut, pMaxLength, pCommandLine.getCommands() ); - - - // Output the common options - pOut.println( "\nCommon options:" ); - formatter.printOptions( pw, pMaxLength, pCommandLine.getCommonOptions(), TAB_SPACES, COMMAND_SEP_SPACES ); - pw.flush(); - - - // Output the commands options - pOut.println( "\nSub-commands options:" ); - for ( SubCommand command : pCommandLine.getCommands() ) { - int nbOptions = command.getOptions().getOptions().size(); - if ( nbOptions > 0 ) { - pOut.println( getFilledString( ' ', TAB_SPACES ) + command.getName() ); - formatter.printOptions( pw, pMaxLength, command.getOptions(), TAB_SPACES * 2, COMMAND_SEP_SPACES ); - pw.flush(); - } - } - } - - private static void printSubCommands( PrintStream pOut, int pMaxLength, Collection pCommands ) { - int maxCmdLength = 0; - for ( SubCommand command : pCommands ) { - if ( command.getName().length() > maxCmdLength ) { - maxCmdLength = command.getName().length(); - } - } - - for ( SubCommand command : pCommands ) { - String name = command.getName(); - String message = getFilledString( ' ', TAB_SPACES ) + name; - - int nameDiff = ( maxCmdLength - name.length() ); - message += getFilledString( ' ', nameDiff + COMMAND_SEP_SPACES ); - - int restLen = pMaxLength - maxCmdLength - TAB_SPACES - COMMAND_SEP_SPACES; - String aligningBlanks = getFilledString( ' ', pMaxLength - restLen ); - - String[] words = command.getDescription().split( " " ); - int i = 0; - boolean firstLine = true; - String line = new String(); - while ( i < words.length ) { - String word = words[i]; - if ( line.length() + 1 + word.length() < restLen ) { - if ( line.length() > 0 ) { - line += " "; - } - line += word; - - if ( i == words.length - 1 ) { - if ( !firstLine ) { - message += aligningBlanks; - } - message += line + "\n"; - } - - } else { - if ( !firstLine ) { - message += aligningBlanks; - } - message += line + "\n"; - line = word; - firstLine = false; - } - i++; - } - - pOut.print( message ); - } - } - - private static String getFilledString(char pChar, int pNumber) { - char[] chars = new char[pNumber]; - Arrays.fill( chars, pChar ); - return new String( chars ); - } -} diff --git a/src/org/openoffice/tools/options/SubCommand.java b/src/org/openoffice/tools/options/SubCommand.java deleted file mode 100644 index c3aacd4..0000000 --- a/src/org/openoffice/tools/options/SubCommand.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.openoffice.tools.options; - -import org.apache.commons.cli.Options; - -public class SubCommand { - - private String mName; - private String mDescription; - private Options mOptions; - - public SubCommand( String pName, Options pOptions, String pDescription ) { - mName = pName; - mDescription = pDescription; - mOptions = pOptions; - } - - public String getName() { - return mName; - } - - public String getDescription() { - return mDescription; - } - - public Options getOptions() { - return mOptions; - } -} diff --git a/src/org/openoffice/tools/options/SubcommandLine.java b/src/org/openoffice/tools/options/SubcommandLine.java deleted file mode 100644 index 2a6f716..0000000 --- a/src/org/openoffice/tools/options/SubcommandLine.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.openoffice.tools.options; - -import org.apache.commons.cli.CommandLine; - -public class SubcommandLine { - - private static final long serialVersionUID = 1578591361018635215L; - - private String mSubcommand; - private CommandLine mCommandLine; - - public SubcommandLine( String pSubcommand, CommandLine pCommandLine ) { - mSubcommand = pSubcommand; - mCommandLine = pCommandLine; - } - - public String getSubcommand() { - return mSubcommand; - } - - public CommandLine getCommandLine() { - return mCommandLine; - } -} diff --git a/src/org/openoffice/tools/tests/AllTests.java b/src/org/openoffice/tools/tests/AllTests.java deleted file mode 100644 index eacc661..0000000 --- a/src/org/openoffice/tools/tests/AllTests.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.openoffice.tools.tests; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith( Suite.class ) -@SuiteClasses( { - LanguageHelperTest.class, - CommandTest.class, - TypeManagerTest.class } ) -public class AllTests { - -} diff --git a/src/org/openoffice/tools/tests/CommandTest.java b/src/org/openoffice/tools/tests/CommandTest.java deleted file mode 100644 index 06f4315..0000000 --- a/src/org/openoffice/tools/tests/CommandTest.java +++ /dev/null @@ -1,134 +0,0 @@ -package org.openoffice.tools.tests; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.apache.commons.cli.Option; -import org.apache.commons.cli.OptionGroup; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.apache.commons.cli.PosixParser; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.openoffice.tools.options.Command; -import org.openoffice.tools.options.SubCommand; -import org.openoffice.tools.options.SubcommandLine; - -public class CommandTest { - - private Command mCommand; - - @Before - public void setUp() throws Exception { - mCommand = setupCommandLine(); - } - - @After - public void tearDown( ) throws Exception { - mCommand = null; - } - - @Test - public void testParseValid() { - String[] validArgs = new String[] { - "registration", - "-cpp", - "-s", - "TestImpl" - }; - - try { - SubcommandLine line = mCommand.parse( validArgs, new PosixParser() ); - assertNotNull( "Result of parsing is null", line.getCommandLine() ); - assertEquals( "Wrong subcommand", "registration", line.getSubcommand() ); - - // Check the presence of the last option and its value - assertTrue( "Option not recognized", line.getCommandLine().hasOption( "s" ) ); - assertEquals( "wrong value detected", "TestImpl", line.getCommandLine().getOptionValue( "s" ) ) ; - } catch (ParseException e) { - fail( "Unexcepted exception: " + e.getMessage() ); - } - } - - @Test - public void testParseSubcommandNameError() { - String[] validArgs = new String[] { - "failure", - "-cpp", - "-s", - "TestImpl" - }; - - try { - mCommand.parse( validArgs, new PosixParser() ); - fail( "Should have thrown an exception" ); - } catch (ParseException e) { - assertTrue( "Wrong exception message", e.getMessage().endsWith( "failure" ) ); - } - } - - @Test - public void testParseSubcommandArgsError() { - String[] validArgs = new String[] { - "registration", - "-cpp", - "--error", - "--err2" - }; - - try { - mCommand.parse( validArgs, new PosixParser() ); - fail( "Should have thrown an exception" ); - } catch (ParseException e) { - assertTrue( "Error message doesn't contain '--error'" , e.getMessage().contains( "--error" ) ); - } - } - - @Test( expected=ParseException.class ) - public void testParseSubcommandNoArg() throws Exception { - String[] validArgs = new String[] { - "uno-skeletonmaker" - }; - - mCommand.parse( validArgs, new PosixParser() ); - } - - private static Command setupCommandLine( ) { - Command cmdLine = new Command(); - - // General options - Options generalOptions = new Options(); - - OptionGroup langGroup = new OptionGroup(); - Option java5 = new Option( "java5", false, "generate output for Java 1.5 or later (is currently the default)" ); - Option java4 = new Option( "java4", false, "generate output for Java 1.4 or earlier" ); - Option cpp = new Option( "cpp", false, "generate output for C++" ); - langGroup.addOption( java4 ); - langGroup.addOption( java5 ); - langGroup.addOption( cpp ); - langGroup.setRequired( false ); - generalOptions.addOptionGroup( langGroup ); - - cmdLine.setCommonOptions( generalOptions ); - - - // component command options - Options compOptions = new Options( ); - compOptions.addOption( "t", true, "specifies an UNOIDL type name, e.g. com.sun.star.text.XText" ); - SubCommand component = new SubCommand( "component", compOptions, - "generates language specific code skeleton files using the implementation name as the file and class name" ); - cmdLine.addCommand( component ); - - // registration options - Options regOptions = new Options(); - regOptions.addOption( "s", true, "name of the implementation of a service to add to the registration system." ); - SubCommand registration = new SubCommand( "registration", regOptions, - "creates the registration code for a list of services implementations. The '-s' option has to be used at least once to specify the implementation name of a service to add to the registration system." ); - cmdLine.addCommand( registration ); - - return cmdLine; - } -} diff --git a/src/org/openoffice/tools/tests/LanguageHelperTest.java b/src/org/openoffice/tools/tests/LanguageHelperTest.java deleted file mode 100644 index 49c5b34..0000000 --- a/src/org/openoffice/tools/tests/LanguageHelperTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package org.openoffice.tools.tests; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.io.File; -import java.io.StringWriter; -import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.HashMap; - -import org.junit.Before; -import org.junit.Test; -import org.openoffice.tools.language.Language; -import org.openoffice.tools.language.LanguageHelper; - -import freemarker.template.Template; - -/** - * ATM, the fixture of this class is the main language configuration: it - * would be more complex to generate the fixture here. - * - * @author cbosdonnat - * - */ -public class LanguageHelperTest { - - private static final String TESTS_LANGS_DIR = "test_langs"; - - private static final String WORKING_LANG = "working"; - private static final Object WORKING_DESCRIPTION = "Working test lang"; - - private static final String MISSING_LANG = "missing"; - private static final String UNCOMPLETE_LANG = "uncomplete"; - - private static final String SIMPLE_TPL = "simple.tpl"; - private static final String NESTED_TPL = "nested.tpl"; - - private static final String VAR_SOMETEXT_NAME = "sometext"; - private static final String VAR_SOMETEXT_VALUE = "some text"; - - @Before - public void setup( ) throws URISyntaxException { - File jarFile = new File(LanguageHelperTest.class.getProtectionDomain() - .getCodeSource().getLocation().toURI()); - File dir = new File( jarFile.getParentFile(), TESTS_LANGS_DIR ); - LanguageHelper.setLanguagesDir( dir ); - } - - @Test - public void testGetLanguages( ) { - String[] result = LanguageHelper.getLanguagesIds(); - String[] expected = new String[] { WORKING_LANG, UNCOMPLETE_LANG }; - - // The order in results aren't guaranteed - Arrays.sort( result ); - Arrays.sort( expected ); - assertArrayEquals( "Different values", expected, result ); - } - - @Test - public void testGetDescriptionOk( ) { - Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); - assertNotNull( langDef ); - String actual = langDef.getDescription( ); - assertEquals( WORKING_DESCRIPTION , actual ); - } - - @Test - public void testGetLanguageMissing( ) { - Language langDef = LanguageHelper.getLanguage( MISSING_LANG ); - assertNull( langDef ); - } - - @Test - public void testGetLanguageMissingProp( ) { - Language langDef = LanguageHelper.getLanguage( UNCOMPLETE_LANG ); - assertNotNull( langDef ); - String actual = langDef.getDescription(); - assertNull( actual ); - } - - @Test - public void testSimpleTemplate( ) throws Exception { - Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); - assertNotNull( langDef ); - - Template tpl = langDef.getTemplate( SIMPLE_TPL ); - StringWriter out = new StringWriter(); - tpl.process( getTestTemplateModel(), out ); - out.flush(); - - String expected = VAR_SOMETEXT_VALUE; - assertEquals( expected, out.toString() ); - } - - @Test - public void testNestedTemplate( ) throws Exception { - Language langDef = LanguageHelper.getLanguage( WORKING_LANG ); - assertNotNull( langDef ); - - Template tpl = langDef.getTemplate( NESTED_TPL ); - StringWriter out = new StringWriter(); - tpl.process( getTestTemplateModel(), out ); - out.flush(); - - String expected = VAR_SOMETEXT_VALUE + "\n" + VAR_SOMETEXT_VALUE; - assertEquals( expected, out.toString() ); - } - - private Object getTestTemplateModel() { - HashMap model = new HashMap(); - model.put( VAR_SOMETEXT_NAME, VAR_SOMETEXT_VALUE ); - return model; - } -} diff --git a/src/org/openoffice/tools/tests/TypeManagerTest.java b/src/org/openoffice/tools/tests/TypeManagerTest.java deleted file mode 100644 index 2037c02..0000000 --- a/src/org/openoffice/tools/tests/TypeManagerTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.openoffice.tools.tests; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.FileInputStream; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.openoffice.tools.types.TypeManager; - -import com.sun.star.container.NoSuchElementException; -import com.sun.star.reflection.XTypeDescription; - -/** - * This test class assumes a ooo.install.dir Java property is set and - * points to the directory of an installed OOo 3.x. - * - * @author cbosdonnat - * - */ -public class TypeManagerTest { - - private static final String SEARCHED_TYPE = "com.sun.star.lang.XTypeProvider"; - private static final String SEARCHED_TYPE_ERR = "some.dummy.type"; - - private static TypeManager sTestedManager; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - String installDir = System.getProperty( "ooo.install.dir" ); - - sTestedManager = new TypeManager( installDir ); - sTestedManager.initRegistries( new String[]{ - getUreRegistryUrl( installDir ) - } ); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - sTestedManager.dispose(); - } - - @Test - public void testSearchTypeValid() { - try { - XTypeDescription result = sTestedManager.searchType( SEARCHED_TYPE ); - assertEquals( "Actual description doesn't match", SEARCHED_TYPE, result.getName() ); - } catch ( NoSuchElementException e ) { - fail( "This element should be found" ); - } - } - - @Test( expected=NoSuchElementException.class ) - public void testSearchTypeIncorrect() throws Exception { - sTestedManager.searchType( SEARCHED_TYPE_ERR ); - } - - private static String getUreRegistryUrl( String pInstallDir ) { - String regUrl = null; - - try { - File install = new File( pInstallDir ); - File basis = getPortableLink( "basis-link", install ); - File ure = getPortableLink( "ure-link", basis ); - - String sep = System.getProperty( "file.separator" ); - File reg = new File( ure, "share" + sep + "misc" + sep + "types.rdb" ); - - if ( reg.canRead() ) { - regUrl = "file://" + reg.toURI().toURL().getPath(); - } - - } catch ( Exception e ) { - } - - return regUrl; - } - - /** - * Get the file object for the link defined as a child of a folder. - * - * On Windows platform, the link relative location is specified as the content of - * a file named after the link name. On Unix-based systems symbolic links are - * supported. - * - *

This method has been adapted from the ooeclipse integration code, class - * org.openoffice.ide.eclipse.core.OOo.OOo3PathMapper

- * - * @param pName the name of the symbolic link - * @param pParent the parent directory file - * - * @return the file representing the link target or null - * - */ - private static File getPortableLink(String pName, File pParent) { - File link = null; - - File linkFile = new File(pParent, pName); - if ( System.getProperty( "os.name" ).toLowerCase().startsWith( "win" ) ) { - // Read the content of the file to get the true folder - try { - FileInputStream is = new FileInputStream(linkFile); - byte[] buf = new byte[is.available()]; - is.read(buf); - - String relativePath = new String(buf); - linkFile = new File(pParent, relativePath); - link = linkFile; - } catch (Exception e) { - // the returned link is null to show the error - } - } else { - link = linkFile; - } - - return link; - } -} diff --git a/src/org/openoffice/tools/types/Bootstrap.java b/src/org/openoffice/tools/types/Bootstrap.java deleted file mode 100644 index 8c933b5..0000000 --- a/src/org/openoffice/tools/types/Bootstrap.java +++ /dev/null @@ -1,339 +0,0 @@ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2008 by Sun Microsystems, Inc. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * $RCSfile: Bootstrap.java,v $ - * $Revision: 1.16 $ - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * - * for a copy of the LGPLv3 License. - * - ************************************************************************/ - -package org.openoffice.tools.types; - -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.net.URLClassLoader; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Random; - -import com.sun.star.bridge.UnoUrlResolver; -import com.sun.star.bridge.XUnoUrlResolver; -import com.sun.star.comp.helper.BootstrapException; -import com.sun.star.comp.helper.ComponentContext; -import com.sun.star.comp.helper.ComponentContextEntry; -import com.sun.star.comp.loader.JavaLoader; -import com.sun.star.container.XSet; -import com.sun.star.lang.XInitialization; -import com.sun.star.lang.XMultiComponentFactory; -import com.sun.star.lang.XMultiServiceFactory; -import com.sun.star.lang.XSingleComponentFactory; -import com.sun.star.lib.util.NativeLibraryLoader; -import com.sun.star.loader.XImplementationLoader; -import com.sun.star.uno.UnoRuntime; -import com.sun.star.uno.XComponentContext; - -/** Bootstrap offers functionality to obtain a context or simply - a service manager. - The service manager can create a few basic services, whose implementations are: -
    -
  • com.sun.star.comp.loader.JavaLoader
  • -
  • com.sun.star.comp.urlresolver.UrlResolver
  • -
  • com.sun.star.comp.bridgefactory.BridgeFactory
  • -
  • com.sun.star.comp.connections.Connector
  • -
  • com.sun.star.comp.connections.Acceptor
  • -
  • com.sun.star.comp.servicemanager.ServiceManager
  • -
- - Other services can be inserted into the service manager by - using its XSet interface: -
-        XSet xSet = (XSet)UnoRuntime.queryInterface( XSet.class, aMultiComponentFactory );
-        // insert the service manager
-        xSet.insert( aSingleComponentFactory );
-    
- - @author cbosdo Changed the class for a better control of the bootstrap using a custom classloader -*/ -public class Bootstrap { - - private static void insertBasicFactories( - XSet xSet, XImplementationLoader xImpLoader ) - throws Exception - { - // insert the factory of the loader - xSet.insert( xImpLoader.activate( - "com.sun.star.comp.loader.JavaLoader", null, null, null ) ); - - // insert the factory of the URLResolver - xSet.insert( xImpLoader.activate( - "com.sun.star.comp.urlresolver.UrlResolver", null, null, null ) ); - - // insert the bridgefactory - xSet.insert( xImpLoader.activate( - "com.sun.star.comp.bridgefactory.BridgeFactory", null, null, null ) ); - - // insert the connector - xSet.insert( xImpLoader.activate( - "com.sun.star.comp.connections.Connector", null, null, null ) ); - - // insert the acceptor - xSet.insert( xImpLoader.activate( - "com.sun.star.comp.connections.Acceptor", null, null, null ) ); - } - - /** Bootstraps an initial component context with service manager and basic - jurt components inserted. - @param context_entries the hash table contains mappings of entry names (type string) to - context entries (type class ComponentContextEntry). - @return a new context. - */ - @SuppressWarnings("unchecked") - static public XComponentContext createInitialComponentContext( Hashtable context_entries ) - throws Exception - { - XImplementationLoader xImpLoader = (XImplementationLoader)UnoRuntime.queryInterface( - XImplementationLoader.class, new JavaLoader() ); - - // Get the factory of the ServiceManager - XSingleComponentFactory smgr_fac = (XSingleComponentFactory)UnoRuntime.queryInterface( - XSingleComponentFactory.class, xImpLoader.activate( - "com.sun.star.comp.servicemanager.ServiceManager", null, null, null ) ); - - // Create an instance of the ServiceManager - XMultiComponentFactory xSMgr = (XMultiComponentFactory)UnoRuntime.queryInterface( - XMultiComponentFactory.class, smgr_fac.createInstanceWithContext( null ) ); - - // post init loader - XInitialization xInit = (XInitialization)UnoRuntime.queryInterface( - XInitialization.class, xImpLoader ); - Object[] args = new Object [] { xSMgr }; - xInit.initialize( args ); - - // initial component context - if (context_entries == null) - context_entries = new Hashtable( 1 ); - // add smgr - context_entries.put( - "/singletons/com.sun.star.lang.theServiceManager", - new ComponentContextEntry( null, xSMgr ) ); - // ... xxx todo: add standard entries - XComponentContext xContext = new ComponentContext( context_entries, null ); - - // post init smgr - xInit = (XInitialization)UnoRuntime.queryInterface( - XInitialization.class, xSMgr ); - args = new Object [] { null, xContext }; // no registry, default context - xInit.initialize( args ); - - XSet xSet = (XSet)UnoRuntime.queryInterface( XSet.class, xSMgr ); - // insert the service manager - xSet.insert( smgr_fac ); - // and basic jurt factories - insertBasicFactories( xSet, xImpLoader ); - - return xContext; - } - - /** - * Bootstraps a servicemanager with the jurt base components registered. - *

- * @return a freshly boostrapped service manager - * @see com.sun.star.lang.ServiceManager - */ - static public XMultiServiceFactory createSimpleServiceManager() throws Exception - { - return (XMultiServiceFactory)UnoRuntime.queryInterface( - XMultiServiceFactory.class, createInitialComponentContext( null ).getServiceManager() ); - } - - - /** Bootstraps the initial component context from a native UNO installation. - - @see cppuhelper/defaultBootstrap_InitialComponentContext() - */ - static public final XComponentContext defaultBootstrap_InitialComponentContext() - throws Exception - { - return defaultBootstrap_InitialComponentContext( null, null ); - } - /** Bootstraps the initial component context from a native UNO installation. - - @param ini_file - ini_file (may be null: uno.rc besides cppuhelper lib) - @param bootstrap_parameters - bootstrap parameters (maybe null) - - @see cppuhelper/defaultBootstrap_InitialComponentContext() - */ - @SuppressWarnings("unchecked") - static public final XComponentContext defaultBootstrap_InitialComponentContext( - String ini_file, Hashtable bootstrap_parameters ) - throws Exception - { - // jni convenience: easier to iterate over array than calling Hashtable - String pairs [] = null; - if (null != bootstrap_parameters) - { - pairs = new String [ 2 * bootstrap_parameters.size() ]; - Enumeration keys = bootstrap_parameters.keys(); - int n = 0; - while (keys.hasMoreElements()) - { - String name = (String)keys.nextElement(); - pairs[ n++ ] = name; - pairs[ n++ ] = (String)bootstrap_parameters.get( name ); - } - } - - if (! m_loaded_juh) - { - System.loadLibrary( "juh" ); - m_loaded_juh = true; - } - return (XComponentContext)UnoRuntime.queryInterface( - XComponentContext.class, - cppuhelper_bootstrap( - ini_file, pairs, Bootstrap.class.getClassLoader() ) ); - } - - static private boolean m_loaded_juh = false; - static private native Object cppuhelper_bootstrap( - String ini_file, String bootstrap_parameters [], ClassLoader loader ) - throws Exception; - - /** - * Bootstraps the component context from a UNO installation. - * - * @return a bootstrapped component context. - * - * @since UDK 3.1.0 - */ - public static final XComponentContext bootstrap(URLClassLoader loader) - throws BootstrapException { - - XComponentContext xContext = null; - - try { - // create default local component context - XComponentContext xLocalContext = - createInitialComponentContext( null ); - if ( xLocalContext == null ) - throw new BootstrapException( "no local component context!" ); - - // find office executable relative to this class's class loader - String sOffice = - System.getProperty( "os.name" ).startsWith( "Windows" ) ? - "soffice.exe" : "soffice"; - File fOffice = NativeLibraryLoader.getResource( loader, sOffice ); - if ( fOffice == null ) - throw new BootstrapException( "no office executable found!" ); - - // create random pipe name - String sPipeName = "uno" + - Long.toString( (new Random()).nextLong() & 0x7fffffffffffffffL ); - - // create call with arguments - String[] cmdArray = new String[7]; - cmdArray[0] = fOffice.getPath(); - cmdArray[1] = "-nologo"; - cmdArray[2] = "-nodefault"; - cmdArray[3] = "-norestore"; - cmdArray[4] = "-nocrashreport"; - cmdArray[5] = "-nolockcheck"; - cmdArray[6] = "-accept=pipe,name=" + sPipeName + ";urp;"; - - // start office process - Process p = Runtime.getRuntime().exec( cmdArray ); - pipe( p.getInputStream(), System.out, "CO> " ); - pipe( p.getErrorStream(), System.err, "CE> " ); - - // initial service manager - XMultiComponentFactory xLocalServiceManager = - xLocalContext.getServiceManager(); - if ( xLocalServiceManager == null ) - throw new BootstrapException( "no initial service manager!" ); - - // create a URL resolver - XUnoUrlResolver xUrlResolver = - UnoUrlResolver.create( xLocalContext ); - - // connection string - String sConnect = "uno:pipe,name=" + sPipeName + - ";urp;StarOffice.ComponentContext"; - - // wait until office is started - for (int i = 0;; ++i) { - try { - // try to connect to office - Object context = xUrlResolver.resolve( sConnect ); - xContext = (XComponentContext) UnoRuntime.queryInterface( - XComponentContext.class, context); - if ( xContext == null ) - throw new BootstrapException( "no component context!" ); - break; - } catch ( com.sun.star.connection.NoConnectException ex ) { - // Wait 500 ms, then try to connect again, but do not wait - // longer than 5 min (= 600 * 500 ms) total: - if (i == 600) { - throw new BootstrapException(ex.toString()); - } - Thread.sleep( 500 ); - } - } - } catch ( BootstrapException e ) { - throw e; - } catch ( java.lang.RuntimeException e ) { - throw e; - } catch ( java.lang.Exception e ) { - throw new BootstrapException( e ); - } - - return xContext; - } - - private static void pipe( - final InputStream in, final PrintStream out, final String prefix ) { - - new Thread( "Pipe: " + prefix) { - public void run() { - BufferedReader r = new BufferedReader( - new InputStreamReader( in ) ); - try { - for ( ; ; ) { - String s = r.readLine(); - if ( s == null ) { - break; - } - out.println( prefix + s ); - } - } catch ( java.io.IOException e ) { - e.printStackTrace( System.err ); - } - } - }.start(); - } -} diff --git a/src/org/openoffice/tools/types/Interface.java b/src/org/openoffice/tools/types/Interface.java deleted file mode 100644 index bd99aa7..0000000 --- a/src/org/openoffice/tools/types/Interface.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.openoffice.tools.types; - -import java.util.ArrayList; - -import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; -import com.sun.star.reflection.XInterfaceMemberTypeDescription; -import com.sun.star.reflection.XInterfaceTypeDescription2; -import com.sun.star.reflection.XTypeDescription; -import com.sun.star.uno.TypeClass; -import com.sun.star.uno.UnoRuntime; - -public class Interface { - - XInterfaceTypeDescription2 mDescr; - - public Interface( XTypeDescription pDescr ) { - if ( !pDescr.getTypeClass().equals( TypeClass.INTERFACE ) ) { - throw new IllegalArgumentException( "Not an interface description" ); - } - - mDescr = (XInterfaceTypeDescription2)UnoRuntime.queryInterface( - XInterfaceTypeDescription2.class, pDescr ); - } - - public String getName( ) { - return mDescr.getName(); - } - - public void checkAttributes( ArrayList pAttributes, ArrayList pPropinterfaces) { - String typeName = getName( ); - if ( typeName.equals( "com.sun.star.beans.XPropertySet" ) || - typeName.equals( "com.sun.star.beans.XFastPropertySet" ) || - typeName.equals( "com.sun.star.beans.XPropertyAccess" ) ) { - pPropinterfaces.add( typeName ); - } - - // Check the attributes in the base types - XTypeDescription[] baseTypes = mDescr.getBaseTypes(); - for (XTypeDescription baseType : baseTypes) { - Interface iface = new Interface( baseType ); - iface.checkAttributes( pAttributes, pPropinterfaces ); - } - - // Check the attributes of the interface - XInterfaceMemberTypeDescription[] members = mDescr.getMembers(); - for (XInterfaceMemberTypeDescription member : members) { - XInterfaceAttributeTypeDescription2 attr = (XInterfaceAttributeTypeDescription2) - UnoRuntime.queryInterface( XInterfaceAttributeTypeDescription2.class, member ); - if ( attr != null ) { - pAttributes.add( attr ); - } - } - } - - public boolean checkXComponentSupport() { - boolean supported = false; - - if ( getName().equals( "com.sun.star.lang.XComponent" ) ) { - supported = true; - } else { - XTypeDescription[] baseTypes = mDescr.getBaseTypes(); - int i = 0; - while ( i < baseTypes.length && !supported ) { - Interface iface = new Interface( baseTypes[i] ); - supported = iface.checkXComponentSupport(); - } - } - - return supported; - } -} diff --git a/src/org/openoffice/tools/types/Service.java b/src/org/openoffice/tools/types/Service.java deleted file mode 100644 index de8ad7c..0000000 --- a/src/org/openoffice/tools/types/Service.java +++ /dev/null @@ -1,116 +0,0 @@ -package org.openoffice.tools.types; - -import java.util.ArrayList; -import java.util.Arrays; - -import com.sun.star.reflection.XPropertyTypeDescription; -import com.sun.star.reflection.XServiceTypeDescription2; -import com.sun.star.reflection.XTypeDescription; -import com.sun.star.uno.TypeClass; -import com.sun.star.uno.UnoRuntime; - -/** - * Wrapper class to get the informations on a service. - * - * @author cbosdonnat - * - */ -public class Service { - - XServiceTypeDescription2 mDescr; - ArrayList mRefs; - - - public Service( XTypeDescription pDescr ) { - if ( !pDescr.getTypeClass().equals( TypeClass.SERVICE ) ) { - throw new IllegalArgumentException( "Not a service description" ); - } - - mDescr = (XServiceTypeDescription2)UnoRuntime.queryInterface( - XServiceTypeDescription2.class, pDescr ); - - mRefs = new ArrayList(); - if ( !isNewStyleService() ) { - mRefs.addAll( Arrays.asList( mDescr.getMandatoryServices() ) ); - mRefs.addAll( Arrays.asList( mDescr.getOptionalServices() ) ); - mRefs.addAll( Arrays.asList( mDescr.getMandatoryInterfaces() ) ); - mRefs.addAll( Arrays.asList( mDescr.getOptionalInterfaces() ) ); - } - } - - public XServiceTypeDescription2 getDescription( ) { - return mDescr; - } - - public boolean isNewStyleService( ) { - return mDescr.isSingleInterfaceBased(); - } - - public XTypeDescription getSupertype( ) { - XTypeDescription superType = null; - if ( isNewStyleService() ) { - superType = mDescr.getInterface(); - } - return superType; - } - - public int getReferenceCount( ) { - return mRefs.size(); - } - - public String getReferenceTypeName( int pRefNumber ) { - String name = new String( ); - - XTypeDescription ref = mRefs.get( pRefNumber ); - if ( ref != null ) { - name = ref.getName(); - } - - return name; - } - - public int getMethodCount( ) { - return mDescr.getConstructors().length; - } - - public String getMethodName( int pMethodNumber ) { - String name = new String(); - if ( pMethodNumber >= 0 && pMethodNumber < getMethodCount() ) { - name = mDescr.getConstructors()[ pMethodNumber ].getName(); - } - return name; - } - - public int getPropertiesCount( ) { - int count = 0; - if ( !isNewStyleService() ) { - count = mDescr.getProperties().length; - } - return count; - } - - public XPropertyTypeDescription getProperty( int pPropNumber ) { - XPropertyTypeDescription result = null; - if ( pPropNumber >= 0 && pPropNumber < getPropertiesCount() ) { - result = mDescr.getProperties()[ pPropNumber ]; - } - return result; - } - - public boolean hasProperties() { - - boolean hasProps = getPropertiesCount() > 0; - - // Check the references for properties - int i = 0; - while ( !hasProps && i < getReferenceCount() ) { - XTypeDescription desc = mRefs.get( i ); - if ( desc.getTypeClass().equals( TypeClass.SERVICE ) ) { - Service service = new Service( desc ); - hasProps = service.hasProperties(); - } - } - - return hasProps; - } -} diff --git a/src/org/openoffice/tools/types/TypeManager.java b/src/org/openoffice/tools/types/TypeManager.java deleted file mode 100644 index c8b9085..0000000 --- a/src/org/openoffice/tools/types/TypeManager.java +++ /dev/null @@ -1,254 +0,0 @@ -package org.openoffice.tools.types; - -import java.io.File; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Iterator; - -import org.apache.commons.cli.CommandLine; - -import com.sun.star.container.NoSuchElementException; -import com.sun.star.container.XHierarchicalNameAccess; -import com.sun.star.container.XSet; -import com.sun.star.frame.XDesktop; -import com.sun.star.lang.XMultiComponentFactory; -import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; -import com.sun.star.reflection.XPropertyTypeDescription; -import com.sun.star.reflection.XTypeDescription; -import com.sun.star.registry.XSimpleRegistry; -import com.sun.star.uno.RuntimeException; -import com.sun.star.uno.TypeClass; -import com.sun.star.uno.UnoRuntime; -import com.sun.star.uno.XComponentContext; - -/* - * TODO: - * + Load the registries - * + use the introspection API to extract the useful infos from the type - */ -public class TypeManager { - - public static final String NOPROPERTYHELPER = "_"; - - private String mInstallDir; - private XComponentContext mCtxt; - private XHierarchicalNameAccess mTypeDescMngr; - - public TypeManager( String pInstallDir ) { - mInstallDir = pInstallDir; - connect( ); - } - - public void dispose( ) { - disconnect(); - } - - public void initRegistries( String[] pUrls ) throws Exception { - // Get the type description manager - Object o = mCtxt.getValueByName( "/singletons/com.sun.star.reflection.theTypeDescriptionManager" ); - mTypeDescMngr = (XHierarchicalNameAccess)UnoRuntime.queryInterface( XHierarchicalNameAccess.class, o ); - - if ( mTypeDescMngr == null ) { - throw new RuntimeException( "Couldn't get the type description manager from OOo" ); - } - - // Get the service manager - XMultiComponentFactory xServiceMngr = mCtxt.getServiceManager(); - - // Open the registries - XSimpleRegistry[] regs = new XSimpleRegistry[ pUrls.length ]; - for ( int i = 0; i < pUrls.length; i++ ) { - String regUrl = pUrls[i]; - o = xServiceMngr.createInstanceWithContext( "com.sun.star.registry.SimpleRegistry", mCtxt ); - XSimpleRegistry reg = (XSimpleRegistry)UnoRuntime.queryInterface( XSimpleRegistry.class, o ); - reg.open( regUrl, true, false); - regs[i] = reg; - } - - // Fill the type description manager - o = xServiceMngr.createInstanceWithArgumentsAndContext( - "com.sun.star.reflection.TypeDescriptionProvider", regs, mCtxt ); - XHierarchicalNameAccess tdProvider = (XHierarchicalNameAccess)UnoRuntime.queryInterface( - XHierarchicalNameAccess.class, o ); - if ( tdProvider == null ) { - throw new RuntimeException( "Can't initialize the TypeDescriptionProvider" ); - } - - XSet mngrSet = (XSet)UnoRuntime.queryInterface( XSet.class, mTypeDescMngr ); - mngrSet.insert( tdProvider ); - } - - public void checkType( String pTypeName, ArrayList pServices, ArrayList pInterfaces, - ArrayList< XPropertyTypeDescription > pMembers ) { - try { - XTypeDescription descr = searchType( pTypeName ); - - switch ( descr.getTypeClass().getValue() ) { - case TypeClass.INTERFACE_value: - // Don't add the css.lang.XTypeProvider and cxx.uno.XWeak - if ( !pTypeName.equals( "com.sun.star.lang.XTypeProvider" ) && - !pTypeName.equals( "com.sun.star.uno.XWeak" ) && - !pInterfaces.contains( pTypeName ) ) { - pInterfaces.add( pTypeName ); - } - break; - case TypeClass.SERVICE_value: - if ( !pServices.contains( pTypeName ) ) { - pServices.add( pTypeName ); - - Service service = new Service( descr ); - if ( service.getSupertype() != null ) { - // New style services - - String supertype = service.getSupertype().getName(); - if ( !pInterfaces.contains( supertype ) ) { - pInterfaces.add( supertype ); - } - - // check if constructors are specified, if yes automatically - // support of XInitialization. We will take care of the default - // constructor because in this case XInitialization is not called. - if ( service.getMethodCount() > 1 || - service.getMethodCount() == 1 && service.getMethodName( 0 ).length() > 0 ) { - String initIterface = "com.sun.star.lang.XInitialization"; - if ( !pInterfaces.contains( initIterface ) ) { - pInterfaces.add( initIterface ); - } - } - } else { - // Old style services - - for ( int i = 0, len = service.getReferenceCount(); i < len; i++ ) { - String refName = service.getReferenceTypeName( i ); - checkType( refName, pServices, pInterfaces, pMembers ); - } - - // Get the properties - for ( int i = 0, len = service.getPropertiesCount(); i < len; i++ ) { - pMembers.add( service.getProperty( i ) ); - } - } - } - break; - } - } catch ( Exception e ) { - } - } - - public String checkPropertyHelper(CommandLine pArgs, ArrayList pServices, ArrayList pInterfaces, - ArrayList pAttributes, ArrayList pPropinterfaces) { - - String result = new String(); - - Iterator it = pInterfaces.iterator(); - if ( !pServices.isEmpty() ) { - it = pServices.iterator(); - } - - boolean oldStyleWithProps = false; - boolean propMixin = Boolean.parseBoolean( pArgs.getOptionValue( "propertysetmixin" ) ); - while ( it.hasNext() && result.isEmpty() ) { - String typename = it.next( ); - try { - XTypeDescription typeDesc = searchType( typename ); - if ( !pServices.isEmpty() ) { - Service service = new Service( typeDesc ); - - if ( propMixin && service.getSupertype() != null ) { - Interface iface = new Interface( service.getSupertype() ); - iface.checkAttributes( pAttributes, pPropinterfaces ); - - if ( !pAttributes.isEmpty() && !pPropinterfaces.isEmpty() ) { - result = service.getSupertype().getName(); - } - } else { - oldStyleWithProps = service.hasProperties(); - } - } else { - Interface iface = new Interface( typeDesc ); - iface.checkAttributes( pAttributes, pPropinterfaces ); - if ( !pAttributes.isEmpty() && !pPropinterfaces.isEmpty() ) { - result = typename; - } - } - } catch( Exception e ) { - } - } - - if ( result.isEmpty() && oldStyleWithProps ) { - result = NOPROPERTYHELPER; - } - - return result; - } - - public boolean checkXComponentSupport( ArrayList pInterfaces ) { - - boolean supported = false; - - if ( pInterfaces.isEmpty() ) { - supported = false; - } else { - - Iterator it = pInterfaces.iterator(); - while ( it.hasNext() && !supported ) { - String typeName = it.next( ); - if ( typeName.equals( "com.sun.star.lang.XComponent" ) ) { - pInterfaces.remove( "com.sun.star.lang.XComponent" ); - supported = true; - } else { - try { - XTypeDescription descr = searchType( typeName ); - Interface iface = new Interface( descr ); - supported = iface.checkXComponentSupport( ); - } catch ( NoSuchElementException e ) { - } - } - } - } - - return supported; - } - - /** - * Search the description of a given UNO type in the registries. - * - * @param pTypeName the name of the type to look for, separated by '.' - * - * @return the type description - * - * @throws NoSuchElementException if no element is corresponding to the asked type. - */ - public XTypeDescription searchType( String pTypeName ) throws NoSuchElementException { - Object o = mTypeDescMngr.getByHierarchicalName( pTypeName ); - return (XTypeDescription)UnoRuntime.queryInterface( XTypeDescription.class, o ); - } - - private void connect( ) { - try { - File installDir = new File( mInstallDir ); - File programDir = new File( installDir, "program" ); - URLClassLoader loader = new URLClassLoader( new URL[] { programDir.toURI().toURL() }); - mCtxt = Bootstrap.bootstrap( loader ); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private void disconnect( ) { - try { - if ( mCtxt != null ) { - // Only the uno test suite which started the office can stop it - XMultiComponentFactory xMngr = mCtxt.getServiceManager(); - Object oDesktop = xMngr.createInstanceWithContext("com.sun.star.frame.Desktop", mCtxt); - XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, oDesktop); - - xDesktop.terminate(); - mCtxt = null; - } - } catch (Exception e) { - e.printStackTrace(); - } - } -} -- cgit v1.2.3