001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2007 Ola Bini <ola@ologix.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jvyamlb;
028:
029: import java.io.FileInputStream;
030: import java.io.InputStream;
031:
032: import java.util.HashMap;
033: import java.util.HashSet;
034: import java.util.Iterator;
035: import java.util.Map;
036: import java.util.Set;
037:
038: import java.util.regex.Pattern;
039:
040: import org.jruby.util.ByteList;
041:
042: /**
043: * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
044: */
045: public class ConstructorImpl extends SafeConstructorImpl {
046: private final static Map yamlConstructors = new HashMap();
047: private final static Map yamlMultiConstructors = new HashMap();
048: private final static Map yamlMultiRegexps = new HashMap();
049:
050: public YamlConstructor getYamlConstructor(final Object key) {
051: YamlConstructor mine = (YamlConstructor) yamlConstructors
052: .get(key);
053: if (mine == null) {
054: mine = super .getYamlConstructor(key);
055: }
056: return mine;
057: }
058:
059: public YamlMultiConstructor getYamlMultiConstructor(final Object key) {
060: YamlMultiConstructor mine = (YamlMultiConstructor) yamlMultiConstructors
061: .get(key);
062: if (mine == null) {
063: mine = super .getYamlMultiConstructor(key);
064: }
065: return mine;
066: }
067:
068: public Pattern getYamlMultiRegexp(final Object key) {
069: Pattern mine = (Pattern) yamlMultiRegexps.get(key);
070: if (mine == null) {
071: mine = super .getYamlMultiRegexp(key);
072: }
073: return mine;
074: }
075:
076: public Set getYamlMultiRegexps() {
077: final Set all = new HashSet(super .getYamlMultiRegexps());
078: all.addAll(yamlMultiRegexps.keySet());
079: return all;
080: }
081:
082: public static void addConstructor(final String tag,
083: final YamlConstructor ctor) {
084: yamlConstructors.put(tag, ctor);
085: }
086:
087: public static void addMultiConstructor(final String tagPrefix,
088: final YamlMultiConstructor ctor) {
089: yamlMultiConstructors.put(tagPrefix, ctor);
090: yamlMultiRegexps.put(tagPrefix, Pattern
091: .compile("^" + tagPrefix));
092: }
093:
094: public ConstructorImpl(final Composer composer) {
095: super (composer);
096: }
097:
098: public static void main(final String[] args) throws Exception {
099: final String filename = args[0];
100: System.out.println("Reading of file: \"" + filename + "\"");
101:
102: final ByteList input = new ByteList(1024);
103: final InputStream reader = new FileInputStream(filename);
104: byte[] buff = new byte[1024];
105: int read = 0;
106: while (true) {
107: read = reader.read(buff);
108: input.append(buff, 0, read);
109: if (read < 1024) {
110: break;
111: }
112: }
113: reader.close();
114: final long before = System.currentTimeMillis();
115: for (int i = 0; i < 1; i++) {
116: final Constructor ctor = new ConstructorImpl(
117: new ComposerImpl(new ParserImpl(new ScannerImpl(
118: input)), new ResolverImpl()));
119: for (final Iterator iter = ctor.eachDocument(); iter
120: .hasNext(); iter.next()) {
121: iter.next();
122: // System.out.println(iter.next());
123: }
124: }
125: final long after = System.currentTimeMillis();
126: final long time = after - before;
127: final double timeS = (after - before) / 1000.0;
128: System.out.println("Walking through the nodes for the file: "
129: + filename + " took " + time + "ms, or " + timeS
130: + " seconds");
131: }
132: }// ConstructorImpl
|