public class BaseConfigReader extends Object implements ConfigReader
ConfigReader
.Modifier and Type | Field and Description |
---|---|
protected SAXParserFactory |
mFactory |
Constructor and Description |
---|
BaseConfigReader() |
BaseConfigReader(ResourceResolver resourceResolver) |
Modifier and Type | Method and Description |
---|---|
Object |
configure(InputStream stream,
Object toConfigure,
String... names)
Read configuration from an input stream.
|
Object |
configure(String resource,
Object toConfigure,
String... names)
Read configuration from an application resource.
|
protected Object |
createLazyResolvingNative(Object nativeValue,
Class<?> toResolveTo)
Create a native that is 'lazily resolved' based on the method it is being applied to.
|
protected Object |
createNative(String name,
Class<?> namespace,
String recordedText)
Create the given native type based on the recorded text (as returned by
SAX.endRecording ) |
protected Object |
createNativeCollection(String name)
Certain XML tags are supported 'natively' as collections by the reader.
|
ResourceResolver |
getResourceResolver() |
protected boolean |
isImmutable(Class<?> clazz)
Certain classes are immutable.
|
protected boolean |
isLazyResolvingNative(String name)
Certain XML tags are supported 'natively' by the reader.
|
protected boolean |
isNative(String name)
Certain XML tags are supported 'natively' by the reader.
|
protected Class<?> |
lookupClass(String className,
ClassLoader classLoader)
Lookup a class based on its name.
|
protected Class<?> |
lookupClass(String uri,
String localName,
ClassLoader classLoader)
Lookup a class based on the URI namespace and the local name of the XML tag.
|
protected final SAXParserFactory mFactory
public BaseConfigReader()
public BaseConfigReader(ResourceResolver resourceResolver)
public Object configure(String resource, Object toConfigure, String... names)
This version of configure
uses openResource
to open the specified
resource. It assumes the resource name is a unique key, so subsequent calls do not need to
re-open the resource, or re-parse it, making this version of configure
much
faster than configure( InputStream, Object )
.
This version further caches any immutable objects, in the same way as
configure( InputStream, Object )
(see the JavaDoc for that method).
configure
in interface ConfigReader
resource
- resource name that will be looked up using openResourcetoConfigure
- object to configure. Can be a subclass of the one actually in the resourcenames
- path to a property within the object. If specified, siblings to this path will be
ignored. This allows ConfigReader to be used to initialise only a specific part of
an objectpublic Object configure(InputStream stream, Object toConfigure, String... names)
This version of configure
caches any immutable objects (as determined by
isImmutable
) and reuses them for subsequent calls. This helps ensure there is
only ever one instance of a, say, Inspector
or WidgetBuilder
.
If the Object to configure is a Class
, this method will create and return an
instance of that class based on the configuration file. For example, if the configuration
file is...
<metawidget>
<myInspector config="myConfig">
<someConfigParameter/>
</myInspector>
</metawidget>
...then the code...
Inspector myInspector = myConfigReader.configure( stream, Inspector.class );
...will create a MyInspector
configured with someConfigParameter
.
Conversely, if the Object to configure is already an instance, this method will configure the instance. For example if the configuration file is...
<metawidget>
<swingMetawidget>
<opaque><boolean>true</boolean></opaque>
</swingMetawidget>
</metawidget>
...then the code...
JPanel panel = new JPanel();
myConfigReader.configure( stream, panel );
...will call setOpaque
on the given JPanel
.
configure
in interface ConfigReader
stream
- XML input as a streamtoConfigure
- object to configure. Can be a subclass of the one actually in the resourcenames
- path to a property within the object. If specified, siblings to this path will be
ignored. This allows ConfigReader to be used to initialise only a specific part of
an objectpublic final ResourceResolver getResourceResolver()
protected boolean isNative(String name)
Deciding (ie. isNative
) and creating (ie. createNative
) are
separated into two phases. The former is called to decide whether to
SAX.startRecording
. The latter is called after SAX.endRecording
.
protected boolean isLazyResolvingNative(String name)
Certain tags are indicative of a broader type, but it would be too onerous to specify them
differently for their exact type (ie. array
or enum
). Therefore
they are lazily resolved at time of method call.
protected Object createNative(String name, Class<?> namespace, String recordedText) throws Exception
SAX.endRecording
)namespace
- the Class of the object under constructionException
protected Object createNativeCollection(String name)
protected Object createLazyResolvingNative(Object nativeValue, Class<?> toResolveTo)
nativeValue
- never nulltoResolveTo
- protected Class<?> lookupClass(String uri, String localName, ClassLoader classLoader) throws SAXException
uri
- the URI namespace, to be used as the package namelocalName
- the name of the tag, to be used as the class nameclassLoader
- the classLoader to use to lookup the class. This can be important if the
ClassLoader gets swapped out on us (e.g. SWT's WindowBuilder)SAXException
protected Class<?> lookupClass(String className, ClassLoader classLoader)
Subclasses can override this method to lookup a class in other ways (for example using a module system).
protected boolean isImmutable(Class<?> clazz)
Copyright © 2015. All Rights Reserved.