Sorry for being unclear. In my effort to get this working I tried to use the DynamicNamespaceResolver which led to the namespace mapping issue. The real issue is that my custom aspect is 'invalid' when accessed via the java code.
The java code is called from a script which may also be leading to some confusion.
After creating my custom aspect I run this javascript to set a property:
var metwest_schema_nd = 'workspace://SpacesStore/90329581-c8c8-11db-900a-31d3e5b11671';
// example of hasAspect() API functions
if (document.hasAspect("evg:schemaValidation"))
{
document.properties["evg:schema_node_ref"] = metwest_schema_nd;
document.save();
}
This script runs without any issues - the document has the aspect and the property is set.
Then I call the next script (which in turn calls my java class)
//create Action
var act = actions.create("xmlvalidator");
act.execute(document);
As you can see there is really nothing in this script and the resulting error is actually from the java class.
Here is the Java:
package com.evergreeninvestments.alfresco.action;
import java.io.IOException;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XmlValidator extends ActionExecuterAbstractBase {
/** The name of the action */
public static final String NAME = "xml-validator-action";
private static String nspre = "evg";
private NodeService nodeService;
private ContentService contentService;
private NamespaceService namespaceService;
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
public void setNamespaceService(NamespaceService namespaceService)
{
this.namespaceService = namespaceService;
}
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
int currStatus = 1;
String currMsg = "";
NodeRef schemaNdRef = null;
ContentData content = null;
QName aspectQN = QName.createQName(nspre,"schemaValidation", namespaceService);
QName ndrefQN = QName.createQName(nspre,"schema_node_ref", namespaceService);
QName statusQN = QName.createQName(nspre,"status", namespaceService);
QName msgQN = QName.createQName(nspre,"message", namespaceService);
if (nodeService.exists(actionedUponNodeRef) == true)
{
if (nodeService.hasAspect(actionedUponNodeRef, aspectQN)) {
//get schema node ref
schemaNdRef = (NodeRef)nodeService.getProperty(actionedUponNodeRef, ndrefQN);
} else {
//add aspect
nodeService.addAspect(actionedUponNodeRef, aspectQN, null);
}
//get xml content
content = (ContentData)nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_CONTENT);
if (content.getSize() < 1) {
//content is empty
currStatus = 1;
currMsg = "XML Document Not Found";
}
else
{
ContentReader actRdr = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
try
{
// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = null;
try {
document = parser.parse(actRdr.getContentInputStream());
//well-formed, get schema for validation
ContentReader schemaRdr = contentService.getReader(schemaNdRef, ContentModel.PROP_CONTENT);
if (schemaRdr.getSize() > 0) {
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Schema schema = factory.newSchema(new StreamSource(schemaRdr.getContentInputStream()));
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();
// validate the DOM tree
try {
validator.validate(new DOMSource(document));
currStatus = 4;
currMsg = "XML Document is valid and well-formed";
} catch (SAXException e) {
// instance document is invalid!
currStatus = 3;
currMsg = "Document is not valid.: " + e.toString();
}
}
} catch (SAXException e1) {
// Document not well formed
currStatus = 2;
currMsg = "XML Document Not Well-Formed.: " + e1.toString();
}
}catch(ParserConfigurationException pce) {
currStatus = 1;
currMsg = "ParserConfigurationException: " + pce.toString();
pce.printStackTrace();
}catch(IOException io) {
currStatus = 1;
currMsg = "IOException: " + io.toString();
io.printStackTrace();
}
}
nodeService.setProperty(actionedUponNodeRef, statusQN, currStatus);
nodeService.setProperty(actionedUponNodeRef, msgQN, currMsg);
}
}
@Override
protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
// Specify the parameters
// paramList.add(new ParameterDefinitionImpl(PARAM_SCHEMA_URL, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_SCHEMA_URL)));
}
}
Why is the aspect valid in javascript but not in java?