001/*
002 * Copyright (c) 2004-2012, Willem Cazander
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 * 
008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009 *   following disclaimer.
010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
011 *   the following disclaimer in the documentation and/or other materials provided with the distribution.
012 * 
013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.x4o.xml.core.config;
025
026import java.io.File;
027import java.io.InputStream;
028import java.io.OutputStream;
029import java.net.URL;
030import java.util.Map;
031
032import javax.el.ExpressionFactory;
033
034import org.xml.sax.EntityResolver;
035import org.xml.sax.ErrorHandler;
036import org.xml.sax.InputSource;
037import org.xml.sax.ext.DefaultHandler2;
038
039/**
040 * X4OLanguageProperty holds the language parser properties keys
041 * 
042 * @author Willem Cazander
043 * @version 1.0 6 Aug 2012
044 */
045public enum X4OLanguageProperty {
046        
047        /** Read-Only property returning the language we are working with. */
048        LANGUAGE_NAME("language/name"),
049        
050        /** Read-Only property returning the version of the language. */
051        LANGUAGE_VERSION("language/version"),
052                
053        
054        
055        /** When set to OutputStream xml debug is written to it. note: when output-handler is set this property is ignored. */
056        DEBUG_OUTPUT_STREAM("debug/output-stream",OutputStream.class),
057
058        /** When set to DefaultHandler2 xml debug events are fired to the object. */
059        DEBUG_OUTPUT_HANDLER("debug/output-handler",DefaultHandler2.class),
060
061        /** When set to true print also phases for parsing eld files. */
062        DEBUG_OUTPUT_ELD_PARSER("debug/output-eld-parser",Boolean.class,false),
063        
064        /** When set to true print full element tree per phase. */
065        //DEBUG_OUTPUT_TREE_PER_PHASE("debug/output-tree-per-phase",Boolean.class),
066        
067        
068        /** SAX parser input buffer size: 512-16k defaults to 8k. */
069        INPUT_BUFFER_SIZE("input/buffer-size",Integer.class,4096*2),
070
071        /** When set it allows parsing of non-namespace aware documents. */
072        INPUT_EMPTY_NAMESPACE_URI("input/empty-namespace-uri"),
073        
074        
075        
076        /** The input stream to parse, note is skipped when object is set. */
077        INPUT_SOURCE_STREAM("input/source/stream",InputStream.class),
078        
079        /** When set it overrides automatic encoding detection of sax parser. */
080        INPUT_SOURCE_ENCODING("input/source/encoding"),
081        
082        /** When set use this InputSource instance for parsing. */
083        INPUT_SOURCE_OBJECT("input/source/object",InputSource.class),
084        
085        /** Sets the system-id to the input source. */
086        INPUT_SOURCE_SYSTEM_ID("input/source/system-id",String.class),
087        
088        /** Sets the base path to resolve external input sources. */
089        INPUT_SOURCE_BASE_PATH("input/source/base-path",URL.class),
090        
091        /** Set for custom handling of validation errors. */
092        CONFIG_ERROR_HANDLER("config/error-handler",ErrorHandler.class),
093        
094        /** Resolve more entities from local sources. */
095        CONFIG_ENTITY_RESOLVER("config/entity-resolver",EntityResolver.class),
096        
097        //CONFIG_CACHE_HANDLER("config/cache-handler"),
098        //CONFIG_MODULE_PROVIDER("config/module-provider"),
099        //CONFIG_LOCK_PROPERTIES("config/lock-properties"),
100        
101        
102        /** The beans in this map are set into the EL context for reference. */
103        EL_BEAN_INSTANCE_MAP("el/bean-instance-map",Map.class),
104        //EL_CONTEXT_INSTANCE("el/context-instance"),
105        
106        /** When set this instance is used in ElementLanguage. */
107        EL_FACTORY_INSTANCE("el/factory-instance",ExpressionFactory.class),
108        
109        //EL_INSTANCE_FACTORY("phase/skip-elb-init",Boolean.class),
110        
111        /** When set to an X4OPhase then parsing stops after completing the set phase. */
112        PHASE_STOP_AFTER("phase/stop-after",Boolean.class),
113        
114        /** When set to true skip the release phase. */
115        PHASE_SKIP_RELEASE("phase/skip-release",Boolean.class,false),
116        
117        /** When set to true skip the run phase. */
118        PHASE_SKIP_RUN("phase/skip-run",Boolean.class,false),
119        
120        /** When set to true skip the load siblings language phase. */
121        PHASE_SKIP_SIBLINGS("phase/skip-siblings",Boolean.class,false),
122        
123        
124        /** When set this path is searched for xsd schema files in the language sub directory. */
125        VALIDATION_SCHEMA_PATH("validation/schema-path",File.class),
126        
127        /** When set to true then input xml is validated. */
128        VALIDATION_INPUT("validation/input",Boolean.class,false),
129        
130        /** When set to true then input xsd xml grammer is validated. */
131        VALIDATION_INPUT_XSD("validation/input/xsd",Boolean.class,false),
132        
133        /** When set to true then eld xml is validated. */
134        VALIDATION_ELD("validation/eld",Boolean.class,false),
135        
136        /** When set to true than eld xsd xml grammer is also validated. */
137        VALIDATION_ELD_XSD("validation/eld/xsd",Boolean.class,false);
138        
139        
140        private final static String URI_PREFIX = "http://language.x4o.org/xml/properties/";
141        private final String uriName;
142        private final Class<?>[] classTypes;
143        private final Object defaultValue;
144        
145        private X4OLanguageProperty(String uriName) {
146                this(uriName,String.class);
147        }
148        
149        private X4OLanguageProperty(String uriName,Class<?> classType) {
150                this(uriName,new Class[]{classType},null);
151        }
152        
153        private X4OLanguageProperty(String uriName,Class<?> classType,Object defaultValue) {
154                this(uriName,new Class[]{classType},defaultValue);
155        }
156        
157        private X4OLanguageProperty(String uriName,Class<?>[] classTypes,Object defaultValue) {
158                this.uriName=URI_PREFIX+uriName;
159                this.classTypes=classTypes;
160                this.defaultValue=defaultValue;
161        }
162        
163        /**
164         * Returns the uri defined by this property.
165         * @return The uri defined by this property.
166         */
167        public String toUri() {
168                return uriName;
169        }
170        
171        /**
172         * Returns the default value for this property. 
173         * @return      The default value for this property.
174         */
175        public Object getDefaultValue() {
176                return defaultValue;
177        }
178        
179        /**
180         * Checks if the object is valid to set on this property.
181         * This is done by checking the allowed class types if they are assignable from the value class.
182         * @param value The     object to check.
183         * @return      Returns true when Object value is allowed to be set.
184         */
185        public boolean isValueValid(Object value) {
186                if (LANGUAGE_NAME.equals(this) | LANGUAGE_VERSION.equals(this)) {
187                        return false; // read only are not valid to set.
188                }
189                if (value==null) {
190                        return true;
191                }
192                Class<?> valueClass = value.getClass();
193                for (Class<?> c:classTypes) {
194                        if (c.isAssignableFrom(valueClass)) {
195                                return true;
196                        }
197                }
198                return false;
199        }
200        
201        /**
202         * Search the enum for the value defined by the given uri.
203         * @param uri   The uri to search for.
204         * @return      Return the property for the given uri.
205         * @throws IllegalArgumentException when uri is not found.
206         */
207        static public X4OLanguageProperty valueByUri(String uri) {
208                if (uri==null) {
209                        throw new NullPointerException("Can't search null uri.");
210                }
211                if (uri.length()==0) {
212                        throw new IllegalArgumentException("Can't search empty uri.");
213                }
214                if (uri.startsWith(URI_PREFIX)==false)  {
215                        throw new IllegalArgumentException("Can't search for other name local prefix: "+URI_PREFIX+" found: "+uri);
216                }
217                for (X4OLanguageProperty p:values()) {
218                        if (uri.equals(p.toUri())) {
219                                return p;
220                        }
221                }
222                throw new IllegalArgumentException("Could not find language property for uri key: "+uri);
223        }
224}