Permalink
Browse files

Added annotation to hold the original data

  • Loading branch information...
1 parent a31fac9 commit fff6e04d8536a34933ad64bb4a50e20a505fa7b1 @egonw committed Sep 3, 2009
Showing with 43 additions and 27 deletions.
  1. +43 −27 src/org/openscience/cdk/itty/CDKittyServlet.java
@@ -20,8 +20,15 @@
@SuppressWarnings("serial")
public class CDKittyServlet extends AbstractRobotServlet {
- Pattern mwPattern = Pattern.compile("mwOf:\\w[\\w|\\d]+\\s");
- Pattern htmlPattern = Pattern.compile("htmlOf:\\w[\\w|\\d]+\\s");
+ private final static String VERSION = "8";
+
+ private final static String MWOF_PREFIX = "mwOf:";
+ private final static String HTMLOF_PREFIX = "htmlOf:";
+
+ private final static String PATTERN_MOL_FORM = "\\w[\\w|\\d]+\\s";
+
+ Pattern mwPattern = Pattern.compile(MWOF_PREFIX + PATTERN_MOL_FORM);
+ Pattern htmlPattern = Pattern.compile(HTMLOF_PREFIX + PATTERN_MOL_FORM);
@Override
public void processEvents(RobotMessageBundle bundle) {
@@ -30,12 +37,13 @@ public void processEvents(RobotMessageBundle bundle) {
if (bundle.wasSelfAdded()) {
Blip blip = wavelet.appendBlip();
TextView textView = blip.getDocument();
- textView.append("Prefix a molecular formula with a command to gets the property. " +
+ textView.appendMarkup("CDKitty v" + VERSION + ".<br /> " +
+ "Prefix a molecular formula with a command to gets the property. " +
"If the formula is not recognized 0.0 will be returned; the property " +
"is not calculated until a space is given after the formula. " +
- "The available commands: " +
- "mwOf: calculate the molecular weight;" +
- "htmlOf: return the formula as HTML.");
+ "The available commands:<br /> " +
+ MWOF_PREFIX + " calculate the molecular weight;<br /> " +
+ HTMLOF_PREFIX + " return the formula as HTML.");
}
for (Event e: bundle.getEvents()) {
@@ -46,7 +54,7 @@ public void processEvents(RobotMessageBundle bundle) {
TextView textView = blip.getDocument();
// apply all known commands
calcMw(textView); // mfOf:
-// returnHTML(textView); // htmlOf:
+ returnHTML(textView); // htmlOf:
}
}
}
@@ -60,18 +68,22 @@ private void calcMw(TextView textView) {
int start = matcher.start();
int end = matcher.end();
- String formula = match.substring(5).trim();
- IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula(
- formula,
- NoNotificationChemObjectBuilder.getInstance()
- );
+ String formula = match.substring(MWOF_PREFIX.length()).trim();
+ if (formula != null && formula.length() > 0) {
+ IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula(
+ formula,
+ NoNotificationChemObjectBuilder.getInstance()
+ );
- textView.replace(
- new Range(start, end),
- "" + AtomContainerManipulator.getNaturalExactMass(
- MolecularFormulaManipulator.getAtomContainer(mf)
- )
- );
+ String replacement = "" + AtomContainerManipulator.getNaturalExactMass(
+ MolecularFormulaManipulator.getAtomContainer(mf)
+ );
+ textView.replace(
+ new Range(start, end),
+ replacement
+ );
+ textView.setAnnotation(new Range(start, start+replacement.length()), "chem/molForm", formula);
+ }
} else {
// OK, nothing more found, so return
return;
@@ -87,16 +99,20 @@ private void returnHTML(TextView textView) {
int start = matcher.start();
int end = matcher.end();
- String formula = match.substring(5).trim();
- IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula(
- formula,
- NoNotificationChemObjectBuilder.getInstance()
- );
+ String formula = match.substring(HTMLOF_PREFIX.length()).trim();
+ if (formula != null && formula.length() > 0) {
+ IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula(
+ formula,
+ NoNotificationChemObjectBuilder.getInstance()
+ );
- textView.replace(
- new Range(start, end),
- MolecularFormulaManipulator.getHTML(mf)
- );
+ String replacement = MolecularFormulaManipulator.getHTML(mf);
+ textView.replace(
+ new Range(start, end),
+ replacement
+ );
+ textView.setAnnotation(new Range(start, start+replacement.length()), "chem/molForm", formula);
+ }
} else {
// OK, nothing more found, so return
return;

0 comments on commit fff6e04

Please sign in to comment.