KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jvyamlb > ConstructorImpl


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2007 Ola Bini <ola@ologix.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jvyamlb;
29
30 import java.io.FileInputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 import java.util.regex.Pattern JavaDoc;
40
41 import org.jruby.util.ByteList;
42
43 /**
44  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
45  */

46 public class ConstructorImpl extends SafeConstructorImpl {
47     private final static Map JavaDoc yamlConstructors = new HashMap JavaDoc();
48     private final static Map JavaDoc yamlMultiConstructors = new HashMap JavaDoc();
49     private final static Map JavaDoc yamlMultiRegexps = new HashMap JavaDoc();
50     public YamlConstructor getYamlConstructor(final Object JavaDoc key) {
51         YamlConstructor mine = (YamlConstructor)yamlConstructors.get(key);
52         if(mine == null) {
53             mine = super.getYamlConstructor(key);
54         }
55         return mine;
56     }
57
58     public YamlMultiConstructor getYamlMultiConstructor(final Object JavaDoc key) {
59         YamlMultiConstructor mine = (YamlMultiConstructor)yamlMultiConstructors.get(key);
60         if(mine == null) {
61             mine = super.getYamlMultiConstructor(key);
62         }
63         return mine;
64     }
65
66     public Pattern JavaDoc getYamlMultiRegexp(final Object JavaDoc key) {
67         Pattern JavaDoc mine = (Pattern JavaDoc)yamlMultiRegexps.get(key);
68         if(mine == null) {
69             mine = super.getYamlMultiRegexp(key);
70         }
71         return mine;
72     }
73
74     public Set JavaDoc getYamlMultiRegexps() {
75         final Set JavaDoc all = new HashSet JavaDoc(super.getYamlMultiRegexps());
76         all.addAll(yamlMultiRegexps.keySet());
77         return all;
78     }
79
80     public static void addConstructor(final String JavaDoc tag, final YamlConstructor ctor) {
81         yamlConstructors.put(tag,ctor);
82     }
83
84     public static void addMultiConstructor(final String JavaDoc tagPrefix, final YamlMultiConstructor ctor) {
85         yamlMultiConstructors.put(tagPrefix,ctor);
86         yamlMultiRegexps.put(tagPrefix,Pattern.compile("^"+tagPrefix));
87     }
88
89     public ConstructorImpl(final Composer composer) {
90         super(composer);
91     }
92
93     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
94         final String JavaDoc filename = args[0];
95         System.out.println("Reading of file: \"" + filename + "\"");
96
97         final ByteList input = new ByteList(1024);
98         final InputStream JavaDoc reader = new FileInputStream JavaDoc(filename);
99         byte[] buff = new byte[1024];
100         int read = 0;
101         while(true) {
102             read = reader.read(buff);
103             input.append(buff,0,read);
104             if(read < 1024) {
105                 break;
106             }
107         }
108         reader.close();
109         final long before = System.currentTimeMillis();
110         for(int i=0;i<1;i++) {
111             final Constructor ctor = new ConstructorImpl(new ComposerImpl(new ParserImpl(new ScannerImpl(input)),new ResolverImpl()));
112             for(final Iterator JavaDoc iter = ctor.eachDocument();iter.hasNext();iter.next()) {
113                 iter.next();
114                 // System.out.println(iter.next());
115
}
116         }
117         final long after = System.currentTimeMillis();
118         final long time = after-before;
119         final double timeS = (after-before)/1000.0;
120         System.out.println("Walking through the nodes for the file: " + filename + " took " + time + "ms, or " + timeS + " seconds");
121     }
122 }// ConstructorImpl
123
Popular Tags