${expand(...)} editor variable

Are you missing a feature? Request its implementation here.
Krille
Posts: 24
Joined: Thu Nov 12, 2020 12:24 pm

${expand(...)} editor variable

Post by Krille »

Hi,

right now, oXygen allows to set user-defined editor variables in a
global or project-specific scope, e.g. ${myvar}. And it throws an
exception, when trying to access an editor variable, that was not
initialized (set). It would be cool, to have an editor variable (or a
function) that would try to get a variable by name and if that
variable was not initialized, a default value would be
returned. Having such a variable (or function) would greatly leverage
framework writing, because it would enable us to write parametrizable
author actions, thus more generic frameworks.

The thing I have in mind would be something like,

Code: Select all

${expand(VARIABLE_NAME, DEFAULT)}
and would expand to the value of the variable called VARIABLE_NAME if
that variable is present, and to DEFAULT, if that variable is not
present.

An alternative improvement would be, to enable shipping variable
initialization along with a framework, while also allowing to override
such variables by setting them again (be it in a global or
project-specific way).

Could you imagine, adding such a feature? Or are there simple
alternatives?

Regards,
Chris
Radu
Posts: 7779
Joined: Fri Jul 09, 2004 5:18 pm

Re: ${expand(...)} editor variable

Post by Radu »

Hi Chris,

I added an internal issue to consider such an expand with fallback function:
EXM-49470 Editor variable function which provides a fallback if variable not defined

One thing which can be done is to use our API to provide your own editor variables expansion mechanism from a plugin.
For example this sample plugin for Oxygen provides a ${clipboard} editor variable expansion code via Javascript:

https://github.com/oxygenxml/wsaccess-j ... rVariables

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Krille
Posts: 24
Joined: Thu Nov 12, 2020 12:24 pm

Re: ${expand(...)} editor variable

Post by Krille »

Hi Radu,

thanks for this hint! After few days now, I got used to reading oXygen's Java API.

I found, that I can use code like this for trying to access editor variables in custom author mode actions:

Code: Select all

public class MyCustomOperation
    implements AuthorOperation {
    
    /* ... */
    
        public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
		throws AuthorOperationException, IllegalArgumentException {
		
		/* ... */
		
		UtilAccess utilAccess = authorAccess.getUtilAccess();
		String myvar = utilAccess.expandEditorVariables("${myvar}", null);

		/* ... */
	}
}
If the ${myvar} custom editor variable has not been initialized, it's name is returned by expandEditorVariables(). That's a bit weird (null or an Exception would be cleaner) but it works.

BTW: I do not use an custom EditorVariablesResolver, because the variable names are dynamically concatenated of prefixes and context read in the edited file.

Regrads,
Chris
Radu
Posts: 7779
Joined: Fri Jul 09, 2004 5:18 pm

Re: ${expand(...)} editor variable

Post by Radu »

Hi Chris,

Right. The "expandEditorVariables" method can also be called on any string like "abc${someVar1}def${someVar2}" and it's not feasible for it to return null if it cannot resolve a single variable, it just leaves the variable as it is because it may not be an Oxygen editor variable at all, it may be some user defined string literal which looks like an editor variable.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply