View Javadoc
1   /*
2    * #%L
3    * DocumentEditor.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  
21  package com.allanbank.mongodb.bson;
22  
23  import java.beans.PropertyEditorSupport;
24  
25  import com.allanbank.mongodb.bson.json.Json;
26  import com.allanbank.mongodb.error.JsonException;
27  
28  /**
29   * {@link java.beans.PropertyEditor} for the {@link Document} class.
30   * <p>
31   * The string value must a JSON document parsable via the
32   * {@link Json#parse(String)} method.
33   * </p>
34   * 
35   * @api.yes This class is part of the driver's API. Public and protected members
36   *          will be deprecated for at least 1 non-bugfix release (version
37   *          numbers are &lt;major&gt;.&lt;minor&gt;.&lt;bugfix&gt;) before being
38   *          removed or modified.
39   * @copyright 2013, Allanbank Consulting, Inc., All Rights Reserved
40   */
41  public class DocumentEditor extends PropertyEditorSupport {
42  
43      /**
44       * Creates a new DocumentEditor.
45       */
46      public DocumentEditor() {
47          super();
48      }
49  
50      /**
51       * Parse a string to a {@link Document}.
52       */
53      @Override
54      public void setAsText(final String documentString) {
55          try {
56              setValue(Json.parse(documentString));
57          }
58          catch (final JsonException je) {
59              throw new IllegalArgumentException("Could not parse '"
60                      + documentString + "' into a document.", je);
61          }
62      }
63  }