Skip to content

Commit

Permalink
added ImportAttributeOperation to build 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Daniel Brookshire authored and Patrick Daniel Brookshire committed Jun 25, 2021
1 parent 23c9d41 commit 3ac50d6
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ImportMoreXtension
Adds custom operations with a focus on more interaction with external XML docs to the Oxygen XML Editor

This software package provides six Java operations developed at the Digital Academy of the Academy of Sciences and Literature | Mainz that may be used to create custom actions within the Oxygen XML Editor that simplify importing XML fragments from resources other than the one to be edited. A documentation of all operations can be found in the [wiki of this repository](https://github.com/digicademy/ImportMoreXtension/wiki).
This software package provides seven Java operations developed at the Digital Academy of the Academy of Sciences and Literature | Mainz that may be used to create custom actions within the Oxygen XML Editor that simplify importing XML fragments from resources other than the one to be edited. A documentation of all operations can be found in the [wiki of this repository](https://github.com/digicademy/ImportMoreXtension/wiki).


# Requirements
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* ImportAttributeOperation - is an extension of a ro.sync.ecss.extensions.commons.operations.ChangeAttributeOperation which adds a custom
* operation to the Oxygen XML Editor that lets a user replace the value of an XML attribute with one built dynamically from an external resource.
* It is one of the main classes within the ImportMoreXtension developed at the Digital Academy of the Academy of Sciences and Literature | Mainz.
* @author Patrick D. Brookshire
* @version 1.1.0
*/
package org.adwmainz.da.extensions.importmore.operations;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import org.adwmainz.da.extensions.askmore.exceptions.InputDialogClosedException;
import org.adwmainz.da.extensions.askmore.models.HashedArgumentsMap;
import org.adwmainz.da.extensions.askmore.models.SelectableOption;
import org.adwmainz.da.extensions.askmore.utils.ArgumentParser;
import org.adwmainz.da.extensions.askmore.utils.AskMoreArgumentProvider;
import org.adwmainz.da.extensions.askmore.utils.InputDialogUtils;
import org.adwmainz.da.extensions.importmore.exceptions.ImportMoreXMLException;
import org.adwmainz.da.extensions.importmore.exceptions.ImportMoreXPathException;
import org.adwmainz.da.extensions.importmore.utils.ImportMoreArgumentParser;
import org.adwmainz.da.extensions.importmore.utils.ImportMoreArgumentProvider;
import org.adwmainz.da.extensions.importmore.utils.SaxonUtils;
import org.adwmainz.da.extensions.importmore.utils.XMLImportService;

import net.sf.saxon.s9api.XdmNode;
import ro.sync.ecss.extensions.api.ArgumentDescriptor;
import ro.sync.ecss.extensions.api.ArgumentsMap;
import ro.sync.ecss.extensions.api.AuthorAccess;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.commons.operations.ChangeAttributeOperation;

public class ImportAttributeOperation extends ChangeAttributeOperation {

// field
protected ArgumentDescriptor[] arguments;

// constructor
/**
* Creates a new ImportAnnotatedFragmentOperation
*/
public ImportAttributeOperation() {
super();

// get basic arguments from super class
ArgumentDescriptor[] basicArguments = super.getArguments();

// load localized data
ResourceBundle rb = ResourceBundle.getBundle("org.adwmainz.da.extensions.importmore.resources.DialogTextBundle");

// set arguments
arguments = new ArgumentDescriptor[] {
ImportMoreArgumentProvider.getResourceLocationArgumentDescriptor(),
ImportMoreArgumentProvider.getRequestedElementLocationArgumentDescriptor(),
ImportMoreArgumentProvider.getNamespacePrefixesArgumentDescriptor(),
ImportMoreArgumentProvider.getNamespaceUrisArgumentDescriptor(),
ImportMoreArgumentProvider.getAttributeValueExpressionArgumentDescriptor(),
ImportMoreArgumentProvider.getSelectableNameExpressionArgumentDescriptor(),
AskMoreArgumentProvider.getDialogTitleArgumentDescriptor(rb.getString("IMPORT_ELEMENTS")),
AskMoreArgumentProvider.getSelectionLabelArgumentDescriptor(rb.getString("CHOOSE_ELEMENT")),
ImportMoreArgumentProvider.getArgument(basicArguments, ImportMoreArgumentProvider.ARGUMENT_ATTR_NAME),
ImportMoreArgumentProvider.getArgument(basicArguments, ImportMoreArgumentProvider.ARGUMENT_ATTR_NAMESPACE),
ImportMoreArgumentProvider.getArgument(basicArguments, AskMoreArgumentProvider.ARGUMENT_ELEMENT_LOCATION),
ImportMoreArgumentProvider.getArgument(basicArguments, ImportMoreArgumentProvider.ARGUMENT_EDIT_ATTR)
};
}

// overridden methods
@Override
public String getDescription() {
return "Extends the default ChangeAttributeOperation by adding the possibility to get parts of the attribute value from an external resource.";
}

@Override
public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
throws IllegalArgumentException, AuthorOperationException {
// try to get url from an input dialog
String resourceName;
try {
resourceName = ArgumentParser.getValidStringWithUserInput(args, ImportMoreArgumentProvider.ARGUMENT_RESOURCE_LOCATION);
} catch (InputDialogClosedException ex) {
// abort action if user closes the dialog
throw new IllegalArgumentException(AskMoreArgumentProvider.getClosedDialogMessage());
}

// get other params
Map<String, String> namespaceMap = ImportMoreArgumentParser.getValidMap(args, ImportMoreArgumentProvider.ARGUMENT_NAMESPACE_PREFIXES, ImportMoreArgumentProvider.ARGUMENT_NAMESPACE_URIS);
String attrValueExpression = ArgumentParser.getValidString(args, ImportMoreArgumentProvider.ARGUMENT_ATTR_VALUE_EXPRESSION);
String localElementExpression = ArgumentParser.getValidString(args, AskMoreArgumentProvider.ARGUMENT_ELEMENT_LOCATION, ".");
String requestedElementLocation = ArgumentParser.getValidString(args, ImportMoreArgumentProvider.ARGUMENT_REQUESTED_ELEMENT_LOCATION);
String selectableNameExpression = ArgumentParser.getValidString(args, ImportMoreArgumentProvider.ARGUMENT_SELECTABLE_NAME_EXPRESSION);
String dialogTitle = ArgumentParser.getValidString(args, AskMoreArgumentProvider.ARGUMENT_DIALOG_TITLE);
String selectionLabel = ArgumentParser.getValidString(args, AskMoreArgumentProvider.ARGUMENT_SELECTION_LABEL);

// fetch all options from resource
Set<SelectableOption<XdmNode>> options;
try {
options = XMLImportService.fetchSelectableNodes(resourceName, requestedElementLocation, selectableNameExpression, namespaceMap);
} catch (IOException | ImportMoreXMLException | ImportMoreXPathException ex) {
throw new IllegalArgumentException(ex);
}

// get user selection
XdmNode selectedBaseNode;
try {
selectedBaseNode = InputDialogUtils.fetchSelectedOption(dialogTitle, selectionLabel, options).getRealValue();
} catch (InputDialogClosedException ex) {
// abort action if user closes the dialog
throw new AuthorOperationException(ImportMoreArgumentProvider.getAbortedImportMessage());
}

// build attr value
String attrValue = "";
try {
attrValue = SaxonUtils.getFirstXPathResult(selectedBaseNode, attrValueExpression, namespaceMap).getStringValue();
} catch (ImportMoreXPathException ex) {
throw new IllegalArgumentException(ex);
}

// invoke main action from super class
HashedArgumentsMap parsedArgs = new HashedArgumentsMap(args, Arrays.asList(
ImportMoreArgumentProvider.ARGUMENT_ATTR_NAME,
ImportMoreArgumentProvider.ARGUMENT_ATTR_NAMESPACE,
ImportMoreArgumentProvider.ARGUMENT_EDIT_ATTR)
);
parsedArgs.put(ImportMoreArgumentProvider.ARGUMENT_ATTR_VALUE, attrValue);
parsedArgs.put(AskMoreArgumentProvider.ARGUMENT_ELEMENT_LOCATION, localElementExpression); // set default value if necessary

super.doOperation(authorAccess, parsedArgs);
}

@Override
public ArgumentDescriptor[] getArguments() {
return arguments;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
public class ImportMoreArgumentProvider {

// constant list of all argument names used within this ImportMoreXtension
public static final String ARGUMENT_ATTR_NAME = "name";
public static final String ARGUMENT_ATTR_NAMESPACE = "namespace";
public static final String ARGUMENT_ATTR_VALUE = "value";
public static final String ARGUMENT_ATTR_VALUE_EXPRESSION = "valueExpression";
public static final String ARGUMENT_CONFIRM_ACTION_MESSAGE = "confirmActionMessage";
public static final String ARGUMENT_DISPLAY_PERCENTAGES = "displayPercentages";
public static final String ARGUMENT_EDIT_ATTR = "editAttribute";
public static final String ARGUMENT_EQUAL_ELEMENT_EXPRESSION = "equalElementExpression";
public static final String ARGUMENT_LOCAL_ELEMENT_LOCATION = "localElementLocation";
public static final String ARGUMENT_LOCAL_IDENTIFIER_LOCATION = "localIdentifierLocation";
public static final String ARGUMENT_NAMESPACE_PREFIXES = "namespacePrefixes";
public static final String ARGUMENT_NAMESPACE_URIS = "namespaceUris";
public static final String ARGUMENT_PRECISION = "presicion";
public static final String ARGUMENT_REMOVE_IF_EMPTY = "removeIfEmpty";
public static final String ARGUMENT_RESOURCE_LOCATION = "resourceLocation";
public static final String ARGUMENT_REQUESTED_ELEMENT_LOCATION = "requestedElementLocation";
public static final String ARGUMENT_REQUESTED_IDENTIFIER_LOCATION = "requestedIdentifierLocation";
Expand All @@ -34,7 +40,17 @@ public class ImportMoreArgumentProvider {



// basic ArgumentDescriptor getter methods
// basic ArgumentDescriptor getter methods
public static ArgumentDescriptor getAttributeValueExpressionArgumentDescriptor() {
return new ArgumentDescriptor(
ARGUMENT_ATTR_VALUE_EXPRESSION,
ArgumentDescriptor.TYPE_XPATH_EXPRESSION,
"The XPath expression relative to the result(s) of the param '" +ARGUMENT_REQUESTED_ELEMENT_LOCATION + "' that should be used to create the "
+ "new value for the attribute.\n"
+ "Please note that this expression must not return empty strings! You may use the expression (possiblyEmptyExpression, nonEmptyDefaultExpression)[1] to avoid this.",
".");
}

public static ArgumentDescriptor getDisplayPercentagesArgumentDescriptor() {
return new ArgumentDescriptor(
ARGUMENT_DISPLAY_PERCENTAGES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static Set<SelectableOption<XdmNode>> fetchSelectableNodes(String resourc

/**
* Fetches a List of nodes with keys from the given resource while displaying a loading dialog
* @param resourcePath the path to a resource
* @param resourceName the path to a resource
* @param xPathExpression an XPath expression
* @param keyExpression an XPath expression identifying the Map key using its value as context node
* @param namespaceMap a Map of namespace prefixes and their respective namespace URIs
Expand Down

0 comments on commit 3ac50d6

Please sign in to comment.