001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xslt.mapper.methoid;
021:
022: import java.text.NumberFormat;
023: import java.text.ParseException;
024: import org.netbeans.modules.soa.mapper.common.basicmapper.IBasicMapper;
025: import org.netbeans.modules.soa.mapper.common.basicmapper.literal.BasicLiteralEditorFactory;
026: import org.netbeans.modules.soa.mapper.common.basicmapper.literal.ILiteralEditor;
027: import org.netbeans.modules.soa.mapper.common.basicmapper.literal.ILiteralUpdater.LiteralSubTypeInfo;
028: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IFieldNode;
029: import org.netbeans.modules.xml.xpath.XPathNumericLiteral;
030: import org.netbeans.modules.xslt.mapper.model.nodes.LiteralCanvasNode;
031:
032: /**
033: * Updates the xpath expression for number literals.
034: *
035: * @author jsandusky
036: */
037: public class NumericLiteralUpdater extends AbstractLiteralUpdater {
038:
039: private final String TYPE_LONG = "Long"; // NOI18N
040: private final String TYPE_DOUBLE = "Double"; // NOI18N
041:
042: public ILiteralEditor getEditor(IBasicMapper basicMapper,
043: IFieldNode field) {
044: return BasicLiteralEditorFactory.createStrictNumericEditor(
045: basicMapper, field, this );
046: }
047:
048: public String literalSet(IFieldNode fieldNode, String newValue) {
049: LiteralSubTypeInfo typeInfo = getLiteralSubType(newValue);
050: String newType = typeInfo.getType();
051: // TODO reimplement
052: // XPathLiteralNodeImpl literalNode = (XPathLiteralNodeImpl) fieldNode.getNodeObject();
053: // if (literalNode == null) {
054: // if (TYPE_LONG.equals(typeInfo.getType())) {
055: // XPathNumericLiteral literal = AbstractXPathModelHelper.getInstance().newXPathNumericLiteral(new Long(newValue));
056: // literalNode = new XPathLiteralNodeImpl(literal);
057: // } else {
058: // XPathNumericLiteral literal = AbstractXPathModelHelper.getInstance().newXPathNumericLiteral(new Double(newValue));
059: // literalNode = new XPathLiteralNodeImpl(literal);
060: // }
061: // } else {
062: LiteralCanvasNode node = (LiteralCanvasNode) fieldNode
063: .getGroupNode().getNodeObject();
064:
065: XPathNumericLiteral literal = (XPathNumericLiteral) node
066: .getDataObject();
067: if (TYPE_LONG.equals(newType)) {
068: literal.setValue(new Long(newValue));
069: } else {
070: literal.setValue(new Double(newValue));
071: }
072:
073: mProcessor.updateNodeExpression(fieldNode);
074:
075: return newValue;
076: }
077:
078: public LiteralSubTypeInfo getLiteralSubType(String freeTextValue) {
079: if (freeTextValue == null || freeTextValue.length() < 1) {
080: return null;
081: }
082: String value = freeTextValue.trim().toUpperCase();
083: Number number = null;
084: try {
085: // This parsing returns a Long or Double.
086: // The format of value may still be invalid because
087: // unintelligable characters may still exist in value.
088: number = NumberFormat.getInstance().parse(value);
089: } catch (ParseException pe) {
090: return null;
091: }
092: LiteralSubTypeInfo info = null;
093:
094: try {
095: if (value.indexOf(".") > 0) {
096: Double.parseDouble(value);
097: return new LiteralSubTypeInfo(TYPE_DOUBLE, value);
098: }
099: if (number instanceof Long) {
100: Long.parseLong(value); // ensure value has all valid characters
101: info = new LiteralSubTypeInfo(TYPE_LONG,
102: new Long(value).toString());
103: } else {
104: Double.parseDouble(value); // ensure value has all valid characters
105: info = new LiteralSubTypeInfo(TYPE_DOUBLE, value);
106: }
107: } catch (NumberFormatException nfe) {
108: // just return null to indicate no valid number
109: return null;
110: }
111:
112: return info;
113: }
114: }
|