535ff6ee37e2016b8345eb9fcf2f5d3b69a9df9a
[yangtools.git] / codec / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONNormalizedNodeStreamWriter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.gson.stream.JsonWriter;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.NoSuchElementException;
19 import java.util.regex.Pattern;
20 import javax.xml.transform.dom.DOMSource;
21 import org.checkerframework.checker.regex.qual.Regex;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.common.MountPointLabel;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedAnydata;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.MountPointExtension;
32 import org.opendaylight.yangtools.yang.data.util.NormalizedNodeStreamWriterStack;
33 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
38 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
41 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
42 import org.w3c.dom.Element;
43 import org.w3c.dom.Node;
44 import org.w3c.dom.NodeList;
45 import org.w3c.dom.Text;
46
47 /**
48  * This implementation will create JSON output as output stream.
49  *
50  * <p>
51  * Values of leaf and leaf-list are NOT translated according to codecs.
52  */
53 public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter, MountPointExtension {
54     private static final class Exclusive extends JSONNormalizedNodeStreamWriter {
55         Exclusive(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
56                 final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
57             super(codecFactory, tracker, writer, rootContext);
58         }
59
60         @Override
61         public void close() throws IOException {
62             flush();
63             closeWriter();
64         }
65     }
66
67     private static final class Nested extends JSONNormalizedNodeStreamWriter {
68         Nested(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
69                 final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
70             super(codecFactory, tracker, writer, rootContext);
71         }
72
73         @Override
74         public void close() throws IOException {
75             flush();
76             // The caller "owns" the writer, let them close it
77         }
78     }
79
80     /**
81      * RFC6020 deviation: we are not required to emit empty containers unless they
82      * are marked as 'presence'.
83      */
84     private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
85
86     @Regex
87     private static final String NUMBER_STRING = "-?\\d+(\\.\\d+)?";
88     private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_STRING);
89
90     @Regex
91     private static final String NOT_DECIMAL_NUMBER_STRING = "-?\\d+";
92     private static final Pattern NOT_DECIMAL_NUMBER_PATTERN = Pattern.compile(NOT_DECIMAL_NUMBER_STRING);
93
94     private final NormalizedNodeStreamWriterStack tracker;
95     private final JSONCodecFactory codecs;
96     private final JsonWriter writer;
97     private JSONStreamWriterContext context;
98
99     JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
100             final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
101         this.writer = requireNonNull(writer);
102         codecs = requireNonNull(codecFactory);
103         this.tracker = requireNonNull(tracker);
104         context = requireNonNull(rootContext);
105     }
106
107     /**
108      * Create a new stream writer, which writes to the specified output stream.
109      *
110      * <p>
111      * The codec factory can be reused between multiple writers.
112      *
113      * <p>
114      * Returned writer is exclusive user of JsonWriter, which means it will start
115      * top-level JSON element and ends it.
116      *
117      * <p>
118      * This instance of writer can be used only to emit one top level element,
119      * otherwise it will produce incorrect JSON. Closing this instance will close
120      * the writer too.
121      *
122      * @param codecFactory JSON codec factory
123      * @param jsonWriter JsonWriter
124      * @return A stream writer instance
125      */
126     public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
127             final JsonWriter jsonWriter) {
128         return createExclusiveWriter(codecFactory, jsonWriter, null);
129     }
130
131     /**
132      * Create a new stream writer, which writes to the specified output stream.
133      *
134      * <p>
135      * The codec factory can be reused between multiple writers.
136      *
137      * <p>
138      * Returned writer is exclusive user of JsonWriter, which means it will start
139      * top-level JSON element and ends it.
140      *
141      * <p>
142      * This instance of writer can be used only to emit one top level element,
143      * otherwise it will produce incorrect JSON. Closing this instance will close
144      * the writer too.
145      *
146      * @param codecFactory JSON codec factory
147      * @param jsonWriter JsonWriter
148      * @param initialNs Initial namespace, can be null
149      * @return A stream writer instance
150      */
151     public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
152             final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
153         return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()),
154             jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
155     }
156
157     /**
158      * Create a new stream writer, which writes to the specified output stream.
159      *
160      * <p>
161      * The codec factory can be reused between multiple writers.
162      *
163      * <p>
164      * Returned writer is exclusive user of JsonWriter, which means it will start
165      * top-level JSON element and ends it.
166      *
167      * <p>
168      * This instance of writer can be used only to emit one top level element,
169      * otherwise it will produce incorrect JSON. Closing this instance will close
170      * the writer too.
171      *
172      * @param codecFactory JSON codec factory
173      * @param rootNode Root node inference
174      * @param initialNs Initial namespace
175      * @param jsonWriter JsonWriter
176      * @return A stream writer instance
177      */
178     public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
179             final EffectiveStatementInference rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
180         return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(rootNode), jsonWriter,
181             new JSONStreamWriterExclusiveRootContext(initialNs));
182     }
183
184     /**
185      * Create a new stream writer, which writes to the specified output stream.
186      *
187      * <p>
188      * The codec factory can be reused between multiple writers.
189      *
190      * <p>
191      * Returned writer is exclusive user of JsonWriter, which means it will start
192      * top-level JSON element and ends it.
193      *
194      * <p>
195      * This instance of writer can be used only to emit one top level element,
196      * otherwise it will produce incorrect JSON. Closing this instance will close
197      * the writer too.
198      *
199      * @param codecFactory JSON codec factory
200      * @param path Schema Path
201      * @param initialNs Initial namespace
202      * @param jsonWriter JsonWriter
203      * @return A stream writer instance
204      */
205     public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
206             final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
207         return new Exclusive(codecFactory,
208             NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
209             new JSONStreamWriterExclusiveRootContext(initialNs));
210     }
211
212     /**
213      * Create a new stream writer, which writes to the specified output stream.
214      *
215      * <p>
216      * The codec factory can be reused between multiple writers.
217      *
218      * <p>
219      * Returned writer can be used emit multiple top level element,
220      * but does not start / close parent JSON object, which must be done
221      * by user providing {@code jsonWriter} instance in order for
222      * JSON to be valid. Closing this instance <strong>will not</strong>
223      * close the wrapped writer; the caller must take care of that.
224      *
225      * @param codecFactory JSON codec factory
226      * @param jsonWriter JsonWriter
227      * @return A stream writer instance
228      */
229     public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
230             final JsonWriter jsonWriter) {
231         return createNestedWriter(codecFactory, jsonWriter, null);
232     }
233
234     /**
235      * Create a new stream writer, which writes to the specified output stream.
236      *
237      * <p>
238      * The codec factory can be reused between multiple writers.
239      *
240      * <p>
241      * Returned writer can be used emit multiple top level element,
242      * but does not start / close parent JSON object, which must be done
243      * by user providing {@code jsonWriter} instance in order for
244      * JSON to be valid. Closing this instance <strong>will not</strong>
245      * close the wrapped writer; the caller must take care of that.
246      *
247      * @param codecFactory JSON codec factory
248      * @param initialNs Initial namespace
249      * @param jsonWriter JsonWriter
250      * @return A stream writer instance
251      */
252     public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
253             final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
254         return new Nested(codecFactory, NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()),
255             jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
256     }
257
258     /**
259      * Create a new stream writer, which writes to the specified output stream.
260      *
261      * <p>
262      * The codec factory can be reused between multiple writers.
263      *
264      * <p>
265      * Returned writer can be used emit multiple top level element,
266      * but does not start / close parent JSON object, which must be done
267      * by user providing {@code jsonWriter} instance in order for
268      * JSON to be valid. Closing this instance <strong>will not</strong>
269      * close the wrapped writer; the caller must take care of that.
270      *
271      * @param codecFactory JSON codec factory
272      * @param path Schema Path
273      * @param initialNs Initial namespace
274      * @param jsonWriter JsonWriter
275      * @return A stream writer instance
276      */
277     public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
278             final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
279         return new Nested(codecFactory,
280             NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
281             new JSONStreamWriterSharedRootContext(initialNs));
282     }
283
284     /**
285      * Create a new stream writer, which writes to the specified output stream.
286      *
287      * <p>
288      * The codec factory can be reused between multiple writers.
289      *
290      * <p>
291      * Returned writer can be used emit multiple top level element,
292      * but does not start / close parent JSON object, which must be done
293      * by user providing {@code jsonWriter} instance in order for
294      * JSON to be valid. Closing this instance <strong>will not</strong>
295      * close the wrapped writer; the caller must take care of that.
296      *
297      * @param codecFactory JSON codec factory
298      * @param rootNode Root node inference
299      * @param initialNs Initial namespace
300      * @param jsonWriter JsonWriter
301      * @return A stream writer instance
302      */
303     public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
304             final EffectiveStatementInference rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
305         return new Nested(codecFactory, NormalizedNodeStreamWriterStack.of(rootNode), jsonWriter,
306             new JSONStreamWriterSharedRootContext(initialNs));
307     }
308
309     @Override
310     public Collection<? extends Extension> supportedExtensions() {
311         return List.of(this);
312     }
313
314     @Override
315     public void startLeafNode(final NodeIdentifier name) throws IOException {
316         tracker.startLeafNode(name);
317         context.emittingChild(codecs.getEffectiveModelContext(), writer);
318         context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
319     }
320
321     @Override
322     public final void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
323         tracker.startLeafSet(name);
324         context = new JSONStreamWriterListContext(context, name);
325     }
326
327     @Override
328     public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
329         tracker.startLeafSetEntryNode(name);
330         context.emittingChild(codecs.getEffectiveModelContext(), writer);
331     }
332
333     @Override
334     public final void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
335         tracker.startLeafSet(name);
336         context = new JSONStreamWriterListContext(context, name);
337     }
338
339     /*
340      * Warning suppressed due to static final constant which triggers a warning
341      * for the call to schema.isPresenceContainer().
342      */
343     @Override
344     public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
345         final SchemaNode schema = tracker.startContainerNode(name);
346         final boolean isPresence = schema instanceof ContainerSchemaNode container
347             ? container.isPresenceContainer() : DEFAULT_EMIT_EMPTY_CONTAINERS;
348         context = new JSONStreamWriterNamedObjectContext(context, name, isPresence);
349     }
350
351     @Override
352     public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
353         tracker.startList(name);
354         context = new JSONStreamWriterListContext(context, name);
355     }
356
357     @Override
358     public final void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
359         tracker.startListItem(name);
360         context = new JSONStreamWriterObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
361     }
362
363     @Override
364     public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
365         tracker.startList(name);
366         context = new JSONStreamWriterListContext(context, name);
367     }
368
369     @Override
370     public final void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
371             throws IOException {
372         tracker.startListItem(identifier);
373         context = new JSONStreamWriterObjectContext(context, identifier, DEFAULT_EMIT_EMPTY_CONTAINERS);
374     }
375
376     @Override
377     public final void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
378         tracker.startList(name);
379         context = new JSONStreamWriterListContext(context, name);
380     }
381
382     @Override
383     public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
384         tracker.startChoiceNode(name);
385         context = new JSONStreamWriterInvisibleContext(context);
386     }
387
388     @Override
389     public final boolean startAnydataNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
390         if (NormalizedAnydata.class.isAssignableFrom(objectModel)) {
391             tracker.startAnydataNode(name);
392             context.emittingChild(codecs.getEffectiveModelContext(), writer);
393             context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
394             return true;
395         }
396
397         return false;
398     }
399
400     @Override
401     public final NormalizedNodeStreamWriter startMountPoint(final MountPointLabel label,
402             final MountPointContext mountCtx) throws IOException {
403         final EffectiveModelContext ctx = mountCtx.getEffectiveModelContext();
404         return new Nested(codecs.rebaseTo(ctx), NormalizedNodeStreamWriterStack.of(ctx), writer,
405             new JSONStreamWriterSharedRootContext(context.getNamespace()));
406     }
407
408     @Override
409     public final boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
410         if (DOMSource.class.isAssignableFrom(objectModel)) {
411             tracker.startAnyxmlNode(name);
412             context.emittingChild(codecs.getEffectiveModelContext(), writer);
413             context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
414             return true;
415         }
416         return false;
417     }
418
419     @Override
420     public final void endNode() throws IOException {
421         tracker.endNode();
422         context = context.endNode(codecs.getEffectiveModelContext(), writer);
423     }
424
425     @Override
426     public final void flush() throws IOException {
427         writer.flush();
428     }
429
430     final void closeWriter() throws IOException {
431         if (!(context instanceof JSONStreamWriterRootContext)) {
432             throw new IOException("Unexpected root context " + context);
433         }
434
435         context.endNode(codecs.getEffectiveModelContext(), writer);
436         writer.close();
437     }
438
439     @Override
440     public void scalarValue(final Object value) throws IOException {
441         final Object current = tracker.getParent();
442         if (current instanceof TypedDataSchemaNode typed) {
443             writeValue(value, codecs.codecFor(typed, tracker));
444         } else if (current instanceof AnydataSchemaNode) {
445             writeAnydataValue(value);
446         } else {
447             throw new IllegalStateException(String.format("Cannot emit scalar %s for %s", value, current));
448         }
449     }
450
451     @Override
452     public void domSourceValue(final DOMSource value) throws IOException {
453         final Object current = tracker.getParent();
454         checkState(current instanceof AnyxmlSchemaNode, "Cannot emit DOMSource %s for %s", value, current);
455         // FIXME: should have a codec based on this :)
456         writeAnyXmlValue(value);
457     }
458
459     @SuppressWarnings("unchecked")
460     private void writeValue(final Object value, final JSONCodec<?> codec) throws IOException {
461         ((JSONCodec<Object>) codec).writeValue(writer, value);
462     }
463
464     private void writeAnydataValue(final Object value) throws IOException {
465         if (value instanceof NormalizedAnydata normalized) {
466             writeNormalizedAnydata(normalized);
467         } else {
468             throw new IllegalStateException("Unexpected anydata value " + value);
469         }
470     }
471
472     private void writeNormalizedAnydata(final NormalizedAnydata anydata) throws IOException {
473         // Adjust state to point to parent node and ensure it can handle data tree nodes
474         final SchemaInferenceStack.Inference inference;
475         try {
476             final SchemaInferenceStack stack = SchemaInferenceStack.ofInference(anydata.getInference());
477             stack.exitToDataTree();
478             inference = stack.toInference();
479         } catch (IllegalArgumentException | IllegalStateException | NoSuchElementException e) {
480             throw new IOException("Cannot emit " + anydata, e);
481         }
482
483         anydata.writeTo(JSONNormalizedNodeStreamWriter.createNestedWriter(
484             codecs.rebaseTo(inference.getEffectiveModelContext()), inference, context.getNamespace(), writer));
485     }
486
487     private void writeAnyXmlValue(final DOMSource anyXmlValue) throws IOException {
488         writeXmlNode(anyXmlValue.getNode());
489     }
490
491     private void writeXmlNode(final Node node) throws IOException {
492         if (isArrayElement(node)) {
493             writeArrayContent(node);
494             return;
495         }
496         final Element firstChildElement = getFirstChildElement(node);
497         if (firstChildElement == null) {
498             writeXmlValue(node);
499         } else {
500             writeObjectContent(firstChildElement);
501         }
502     }
503
504     private void writeArrayContent(final Node node) throws IOException {
505         writer.beginArray();
506         handleArray(node);
507         writer.endArray();
508     }
509
510     private void writeObjectContent(final Element firstChildElement) throws IOException {
511         writer.beginObject();
512         writeObject(firstChildElement);
513         writer.endObject();
514     }
515
516     private static boolean isArrayElement(final Node node) {
517         if (Node.ELEMENT_NODE == node.getNodeType()) {
518             final String nodeName = node.getNodeName();
519             for (Node nextNode = node.getNextSibling(); nextNode != null; nextNode = nextNode.getNextSibling()) {
520                 if (Node.ELEMENT_NODE == nextNode.getNodeType() && nodeName.equals(nextNode.getNodeName())) {
521                     return true;
522                 }
523             }
524         }
525         return false;
526     }
527
528     private void handleArray(final Node node) throws IOException {
529         final Element parentNode = (Element)node.getParentNode();
530         final NodeList elementsList = parentNode.getElementsByTagName(node.getNodeName());
531         for (int i = 0, length = elementsList.getLength(); i < length; i++) {
532             final Node arrayElement = elementsList.item(i);
533             final Element parent = (Element)arrayElement.getParentNode();
534             if (parentNode.isSameNode(parent)) {
535                 final Element firstChildElement = getFirstChildElement(arrayElement);
536                 if (firstChildElement != null) {
537                     writeObjectContent(firstChildElement);
538                 } else {
539                     // It may be scalar
540                     writeXmlValue(arrayElement);
541                 }
542             }
543         }
544     }
545
546     private void writeObject(Node node) throws IOException {
547         final var previousNodeNames = new HashSet<String>();
548         while (node != null) {
549             if (Node.ELEMENT_NODE == node.getNodeType()) {
550                 final var nodeName = node.getNodeName();
551                 if (previousNodeNames.add(nodeName)) {
552                     writer.name(nodeName);
553                     writeXmlNode(node);
554                 }
555             }
556             node = node.getNextSibling();
557         }
558     }
559
560     private void writeXmlValue(final Node node) throws IOException {
561         Text firstChild = getFirstChildText(node);
562         String childNodeText = firstChild != null ? firstChild.getWholeText() : "";
563         childNodeText = childNodeText != null ? childNodeText.trim() : "";
564
565         if (NUMBER_PATTERN.matcher(childNodeText).matches()) {
566             writer.value(parseNumber(childNodeText));
567             return;
568         }
569         switch (childNodeText) {
570             case "null" -> writer.nullValue();
571             case "false" -> writer.value(false);
572             case "true" -> writer.value(true);
573             default -> writer.value(childNodeText);
574         }
575     }
576
577     private static Element getFirstChildElement(final Node node) {
578         final NodeList children = node.getChildNodes();
579         for (int i = 0, length = children.getLength(); i < length; i++) {
580             final Node childNode = children.item(i);
581             if (Node.ELEMENT_NODE == childNode.getNodeType()) {
582                 return (Element) childNode;
583             }
584         }
585         return null;
586     }
587
588     private static Text getFirstChildText(final Node node) {
589         final NodeList children = node.getChildNodes();
590         for (int i = 0, length = children.getLength(); i < length; i++) {
591             final Node childNode = children.item(i);
592             if (Node.TEXT_NODE == childNode.getNodeType()) {
593                 return (Text) childNode;
594             }
595         }
596         return null;
597     }
598
599     // json numbers are 64 bit wide floating point numbers - in java terms it is either long or double
600     private static Number parseNumber(final String numberText) {
601         if (NOT_DECIMAL_NUMBER_PATTERN.matcher(numberText).matches()) {
602             return Long.valueOf(numberText);
603         }
604
605         return Double.valueOf(numberText);
606     }
607 }