Framework Content Association extension

Post here questions and problems related to oXygen frameworks/document types.
scottbdr
Posts: 50
Joined: Tue Jul 21, 2009 1:48 am

Framework Content Association extension

Post by scottbdr »

Hi, I'm working on a framework that ideally can look at both an attribute of the root element and a child element of the root to determine the correct Document Type to associate with the document. For example:

Code: Select all

<doc schemaVer="3.2">
   <status>production</status>
   ....
</doc>
Currently I use the Doc Association dialog to look at @schemaVer, but I need a way to look at element status to know if I use a Doc Type that uses a production Schematron validation which uses different rules and external resources than used for test data.

What I gather from this post is that I'll need an extension of type ro.sync.ecss.extensions.api.DocumentTypeCustomRuleMatcher to do the second part. I'm more of a Java hack than developer - do I need to just create a piece of code with like the second code snippet in the above post and zip it up in a jar file, add it on the Classpath tab and point to it in the Document Association dialog? It would parse out the status element value (assuming that's possible) and return a boolean as required by logic. Also, would that code need to do both pieces (looking at both @schemaVer and status) or is the result of the extension combined with what is set up in the dialog?

Thanks Scott
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Framework Content Association extension

Post by Radu »

Hi Scott,

Please see some remarks below:
I'm working on a framework that ideally can look at both an attribute of the root element and a child element of the root to determine the correct Document Type to associate with the document.
Maybe if you can modify the XML structure and schema you could promote the <status> as an attribute on the root element instead of a separate element but I understand this might not be possible.
Currently I use the Doc Association dialog to look at @schemaVer, but I need a way to look at element status to know if I use a Doc Type that uses a production Schematron validation which uses different rules and external resources than used for test data.
Maybe as an alternative you can have one framework with one Schematron schema which in in its rules context can use XPath to look at the value of that element.[/quote]
What I gather from this post is that I'll need an extension of type ro.sync.ecss.extensions.api.DocumentTypeCustomRuleMatcher to do the second part.
Actually you would need to extend this base class:
https://www.oxygenxml.com/InstData/Edit ... tcher.html
I see someone gave an example for this in an older post, the matcher has a method which receives a Reader which can be used to read content from the entire XML document and make decisions based on it:
post61947.html#p61947
I'm more of a Java hack than developer - do I need to just create a piece of code with like the second code snippet in the above post and zip it up in a jar file, add it on the Classpath tab and point to it in the Document Association dialog? It would parse out the status element value (assuming that's possible) and return a boolean as required by logic.
You would need to create a Java project either Maven-based using our SDK (https://www.oxygenxml.com/oxygen_sdk.html) or having in its classpath a reference to all the Java JAR libraries in the "OXYGEN_INSTALL_DIR\lib" folder.
In the Java project you create a class which extends ro.sync.ecss.extensions.api.DocumentTypeAdvancedCustomRuleMatcher and uses that method which is passes the Reader to make decisions based on the entire content.
The Java class is compiled and compressed to a JAR library, library which you copy inside your framework folder and which is then referenced in the document type association "Classpath" list.
Then you create an association rule for your framework based on that extension class.
Also, would that code need to do both pieces (looking at both @schemaVer and status) or is the result of the extension combined with what is set up in the dialog
The "Document Type Rule" dialog box has various fields inside it:
https://www.oxygenxml.com/doc/ug-editor ... s-tab.html
All the fields need to match the XML content so if you are already looking for the @schemaVer in the association rules dialog, the Java code does not need to check for that again.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
scottbdr
Posts: 50
Joined: Tue Jul 21, 2009 1:48 am

Re: Framework Content Association extension

Post by scottbdr »

Thank you for your response Radu. I wish I could change this XML structure! LOL... oh well sometimes you just have to live with things. Your other alternative with the Schematron won't work for other reasons I won't go into. I tried with the extension but it it doesn't seem to work - I created a jar containing the following code but it always returns false (it was detected and is clearly being used by the association rules). If you see anything obvious I'd appreciate your thoughts but no worries if not, my guess is I have it packaged wrong somehow... but I wasn't sure how to configure to run the extension in Eclipse so it could be the code too. I may just stick with having to manually select from various validation scenarios. Thanks again.

Scott

Code: Select all

package ro.sync.ecss.extensions.api;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import org.xml.sax.Attributes;
import ro.sync.ecss.extensions.api.DocumentTypeAdvancedCustomRuleMatcher;

public class DdmfProdStatusCustomRulesMatcher extends DocumentTypeAdvancedCustomRuleMatcher {
	public boolean matches(final String systemID, final String rootNamespace, final String rootLocalName,
			final String doctypePublicID, final String doctypeSystemID, final Attributes rootAttributes,
			final Map<String, String> queryParameters, final Reader contentReader) {
		boolean valuesMatch = false;

		int bufferSize = 1024;
		char[] buffer = new char[bufferSize];
		StringBuilder out = new StringBuilder();

		try {
			for (int numRead; (numRead = contentReader.read(buffer, 0, buffer.length)) > 0;) {
				out.append(buffer, 0, numRead);
			}
		} catch (IOException e) {
			return valuesMatch; // if no content then false
		}
		String doc = out.toString();
		if (doc.matches("<status>\\s*production")) {
			valuesMatch = true;
		}
		return valuesMatch;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}
}
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Framework Content Association extension

Post by Radu »

Hi Scott,

When in doubt you can create a small class to test just part of the code like:

Code: Select all

public class ABC {
  public static void main(String[] args) {
    String str = "<doc schemaVer=\"3.2\">\n"
        + "   <status>production</status>\n"
        + "   ....\n"
        + "</doc>";
    System.err.println(str.matches("<status>\\s*production"));
  }
}
This returns "false" because "matches" returns true only if the entire string matches that regexp, but in your case you want to check if the string contains the regular expression. So in your specific case I think you can create a regexp pattern and try to find the pattern in the text, something like:

Code: Select all

  public static void main(String[] args) {
    String str = "<doc schemaVer=\"3.2\">\n"
        + "   <status>production</status>\n"
        + "   ....\n"
        + "</doc>";
    java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("<status>\\s*production");
    java.util.regex.Matcher matcher = pattern.matcher(str);
    System.err.println(matcher.find());
  }
Also if you write messages to the console output using for example "System.err.println("---");" and start Oxygen from the Oxygen installation folder using the "oxygen.bat" script, you will see those messages in the console output and they might help you debug your code while being used by Oxygen.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
scottbdr
Posts: 50
Joined: Tue Jul 21, 2009 1:48 am

Re: Framework Content Association extension

Post by scottbdr »

Perfect.. thanks for the answer - should have figured that out myself : :oops:
The support on this forum is excellent - thanks again!
Scott
Post Reply