Skip to content
This repository has been archived by the owner on Dec 11, 2021. It is now read-only.

Commit

Permalink
Added annotation to hold the original data
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Sep 3, 2009
1 parent a31fac9 commit fff6e04
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions src/org/openscience/cdk/itty/CDKittyServlet.java
Expand Up @@ -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) {
Expand All @@ -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()) {
Expand All @@ -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:
}
}
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit fff6e04

Please sign in to comment.