001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.schematic.internal.document;
017
018import java.io.Serializable;
019import java.text.ParseException;
020import java.util.Date;
021import java.util.UUID;
022import java.util.regex.Pattern;
023import org.modeshape.schematic.document.Binary;
024import org.modeshape.schematic.document.Bson;
025import org.modeshape.schematic.document.Code;
026import org.modeshape.schematic.document.CodeWithScope;
027import org.modeshape.schematic.document.Document;
028import org.modeshape.schematic.document.Null;
029import org.modeshape.schematic.document.ObjectId;
030import org.modeshape.schematic.document.Symbol;
031import org.modeshape.schematic.document.Timestamp;
032
033public class DefaultDocumentValueFactory implements DocumentValueFactory, Serializable {
034
035   public static final DocumentValueFactory INSTANCE = new DefaultDocumentValueFactory();
036
037   private static final long serialVersionUID = 1L;
038
039   @Override
040   public String createString(String value) {
041      return value;
042   }
043
044   @Override
045   public Integer createInt(int value) {
046      return value;
047   }
048
049   @Override
050   public Long createLong(long value) {
051      return value;
052   }
053
054   @Override
055   public Boolean createBoolean(boolean value) {
056      return value;
057   }
058
059   @Override
060   public Double createDouble(double value) {
061      return value;
062   }
063
064   @Override
065   public Date createDate(String iso) throws ParseException {
066      if (iso == null || iso.length() < 20)
067         return null;
068      int indexOfLastChar = iso.length() - 1;
069      char lastChar = iso.charAt(indexOfLastChar);
070      if (lastChar == 'Z' || lastChar == 'z') {
071         // the date format doesn't like 'Z', so use timezone and offset (e.g., "GMT+00:00") ...
072         iso = iso.substring(0, indexOfLastChar) + "GMT+00:00";
073      } else if (iso.length() >= 22) {
074          // 1997-07-16T19:20:30.45
075          // 0123456789012345678901
076          iso = iso.substring(0, 22) + "GMT" + iso.substring(22);
077      }
078      return Bson.getDateParsingFormatter().parse(iso);
079   }
080
081   @Override
082   public Date createDate(long millis) {
083      return new Date(millis);
084   }
085
086   @Override
087   public Timestamp createTimestamp(int time, int inc) {
088      return new Timestamp(time, inc);
089   }
090
091   @Override
092   public ObjectId createObjectId(String hex) {
093      return BsonUtils.readObjectId(hex);
094   }
095
096   @Override
097   public ObjectId createObjectId(byte[] bytes) {
098      return BsonUtils.readObjectId(bytes);
099   }
100
101   @Override
102   public ObjectId createObjectId(int time, int machine, int process, int inc) {
103      return new ObjectId(time, machine, process, inc);
104   }
105
106   @Override
107   public Symbol createSymbol(String value) {
108      return new Symbol(value);
109   }
110
111   @Override
112   public Pattern createRegex(String pattern, String flags) {
113      return createRegex(pattern, BsonUtils.regexFlagsFrom(flags));
114   }
115
116   @Override
117   public Pattern createRegex(String pattern, int flags) {
118      return Pattern.compile(pattern, flags);
119   }
120
121   @Override
122   public Object createNull() {
123      return Null.getInstance();
124   }
125
126   @Override
127   public Binary createBinary(byte type, byte[] data) {
128      return new Binary(type, data);
129   }
130
131   @Override
132   public UUID createUuid(String uuid) {
133      return UUID.fromString(uuid);
134   }
135
136   @Override
137   public UUID createUuid(long part1, long part2) {
138      return new UUID(part1, part2);
139   }
140
141   @Override
142   public Code createCode(String code) {
143      return new Code(code);
144   }
145
146   @Override
147   public CodeWithScope createCode(String code, Document scope) {
148      if (scope instanceof DocumentEditor) {
149         scope = ((DocumentEditor) scope).unwrap();
150      }
151      return new CodeWithScope(code, scope);
152   }
153
154}