001: package org.geotools.catalog.styling;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005: import java.io.IOException;
006: import java.net.URI;
007: import java.net.URISyntaxException;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import org.geotools.catalog.AbstractFileService;
013: import org.geotools.catalog.Catalog;
014: import org.geotools.catalog.GeoResource;
015: import org.geotools.catalog.ServiceInfo;
016: import org.geotools.catalog.defaults.DefaultServiceInfo;
017: import org.geotools.styling.Style;
018: import org.geotools.styling.StyledLayerDescriptor;
019: import org.geotools.util.ProgressListener;
020:
021: public class SLDService extends AbstractFileService {
022:
023: List members;
024:
025: public SLDService(Catalog parent, File file) {
026: super (parent, file);
027: }
028:
029: public boolean canResolve(Class adaptee) {
030: if (adaptee == null)
031: return false;
032:
033: if (adaptee.equals(StyledLayerDescriptor[].class)) {
034: return true;
035: }
036:
037: return super .canResolve(adaptee);
038: }
039:
040: public Object resolve(Class adaptee, ProgressListener monitor)
041: throws IOException {
042: if (adaptee == null)
043: return null;
044:
045: if (adaptee.equals(StyledLayerDescriptor[].class)) {
046: List members = members(monitor);
047: List styles = new ArrayList();
048:
049: for (Iterator m = members.iterator(); m.hasNext();) {
050: GeoResource member = (GeoResource) m.next();
051: StyledLayerDescriptor sld = (StyledLayerDescriptor) member
052: .resolve(StyledLayerDescriptor.class, monitor);
053: if (sld != null) {
054: styles.add(sld);
055: }
056:
057: }
058:
059: return (StyledLayerDescriptor[]) styles
060: .toArray(new StyledLayerDescriptor[styles.size()]);
061: }
062:
063: return super .resolve(adaptee, monitor);
064: }
065:
066: protected List createMembers(ProgressListener monitor)
067: throws IOException {
068: if (members == null) {
069: synchronized (this ) {
070: if (members == null) {
071: members = new ArrayList();
072:
073: if (getFile().isDirectory()) {
074: File[] files = getFile().listFiles(
075: new FileFilter() {
076:
077: public boolean accept(File pathname) {
078: return SLDServiceFactory
079: .isSLDFile(pathname);
080: }
081: });
082:
083: for (int i = 0; i < files.length; i++) {
084: members.add(new SLDGeoResource(this ,
085: files[i]));
086: }
087:
088: } else {
089: members
090: .add(new SLDGeoResource(this , getFile()));
091:
092: }
093:
094: }
095: }
096: }
097:
098: return members;
099: }
100:
101: public ServiceInfo getInfo(ProgressListener monitor)
102: throws IOException {
103: try {
104: return new DefaultServiceInfo("Styled Layer Descriptor",
105: null, null, getFile().toURI(), null, new URI(
106: SLDServiceFactory.SLD_NAMESPACE), null,
107: null);
108: } catch (URISyntaxException e) {
109: throw (IOException) new IOException().initCause(e);
110: }
111: }
112:
113: }
|