package com.ximple.eofms; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FilenameFilter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.HashMap; import java.util.List; import org.apache.commons.collections.map.MultiValueMap; import org.apache.commons.digester.Digester; import org.apache.commons.digester.xmlrules.DigesterLoader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.geotools.feature.Feature; import org.geotools.feature.FeatureType; import org.geotools.feature.SimpleFeature; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.FeatureStore; import org.geotools.data.shapefile.ShapefileDataStore; import org.xml.sax.SAXException; import com.vividsolutions.jts.geom.GeometryFactory; import com.ximple.eofms.filter.AbstractFLinkageDispatchableFilter; import com.ximple.eofms.filter.ElementDispatcher; import com.ximple.eofms.jobs.context.postgis.FeatureDgnConvertPostGISJobContext; import com.ximple.eofms.util.FeatureTypeBuilderUtil; import com.ximple.io.dgn7.ComplexElement; import com.ximple.io.dgn7.Dgn7fileException; import com.ximple.io.dgn7.Dgn7fileReader; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.ElementType; import com.ximple.io.dgn7.FrammeAttributeData; import com.ximple.io.dgn7.Lock; public class XElementParser implements Runnable { static Log logger = LogFactory.getLog(XElementParser.class); static final GeometryFactory geometryFactory = new GeometryFactory(); static final boolean isCompactMode = true; static final boolean postOutMode = true; protected static final String ROOTDATAPATH = "DATAPATH"; protected static final String DEFAULT_DATAPATH = "G:\\Temp\\JobData\\tctpc"; protected static final String DEFAULT_DGNINPATH = "elmin"; protected static final String DEFAULT_DGNOUTPATH = "elmout"; protected static final String DEFAULT_SHPOUTPATH = "out"; private HashMap dataConfig; private ElementDispatcher elementDispatcher; private MultiValueMap featuresContext = new MultiValueMap(); public static void main(String[] args) { XElementParser parser = new XElementParser(); parser.run(); } public XElementParser() { initializeDataConfig(); } private void initializeDataConfig() { dataConfig = new HashMap(); dataConfig.put(ROOTDATAPATH, DEFAULT_DATAPATH); elementDispatcher = createElementDispatcher(); FeatureTypeBuilderUtil.setNotAllowNull(true); } private ElementDispatcher createElementDispatcher() { try { URL rulesURL = ElementDispatcher.class.getResource("ElementDispatcherRules.xml"); assert rulesURL != null; Digester digester = DigesterLoader.createDigester(rulesURL); URL filterURL; filterURL = FeatureDgnConvertPostGISJobContext.class.getResource("/conf/DefaultConvertShpFilter.xml"); assert filterURL != null; return (ElementDispatcher) digester.parse(filterURL); } catch (UnsupportedEncodingException e) { logger.info(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e); } catch (MalformedURLException e) { logger.info(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { logger.info(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e); } catch (SAXException e) { logger.info(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e); } } public void run() { File rootDir = new File(dataConfig.get(ROOTDATAPATH)); File dataDir = new File(rootDir, DEFAULT_DGNINPATH); if ((!dataDir.exists()) || (!dataDir.isDirectory())) { return; } File[] dataFiles = dataDir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.toLowerCase().endsWith(".dgn"); } }); for (File dataFile : dataFiles) { try { FileInputStream fs = new FileInputStream(dataFile); FileChannel fc = fs.getChannel(); Dgn7fileReader reader = new Dgn7fileReader(fc, new Lock()); int count = 0; Element lastComplex = null; while (reader.hasNext()) { Dgn7fileReader.Record record = reader.nextElement(); if (record.element() != null) { Element element = (Element) record.element(); ElementType type = element.getElementType(); if ((!type.isComplexElement()) && (!element.isComponentElement())) { if (lastComplex != null) { processFeatureElement(lastComplex); lastComplex = null; } processFeatureElement(element); } else if (element.isComponentElement()) { if (lastComplex != null) { ((ComplexElement) lastComplex).add(element); } } else if (type.isComplexElement()) { if (lastComplex != null) { processFeatureElement(lastComplex); } lastComplex = element; } } count++; } if (lastComplex != null) { processFeatureElement(lastComplex); } logger.debug("ElementRecord Count=" + count); } catch (Dgn7fileException e) { logger.warn(e.getMessage(), e); } catch (FileNotFoundException e) { logger.warn(e.getMessage(), e); } catch (IOException e) { logger.warn(e.getMessage(), e); } } if (postOutMode) { try { postProcessFeatureContext(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (IllegalAttributeException e) { logger.warn(e.getMessage(), e); } } } private void processFeatureElement(Element element) { if (element == null) { logger.warn("Unknown Element:" + null); return; } // �P�_�O�_�ũM��� Feature feature = elementDispatcher.execute(element, false); if (feature == null) { FrammeAttributeData linkage = AbstractFLinkageDispatchableFilter.getFeatureLinkage(element); if ((isCompactMode) && (linkage != null)) { logger.warn("Unknown Element:" + element.getElementType().toString() + ":type=" + element.getType() + ":lv=" + element.getLevelIndex() + ":id=" + (linkage.getFsc() + "|" + linkage.getComponentID())); if (element instanceof ComplexElement) { ComplexElement complex = (ComplexElement) element; logger.warn("----Complex Element size=" + complex.size()); } manualElementProcess(element); } else if (!isCompactMode) { logger.warn("Unknown Element:" + element.getElementType().toString() + ":type=" + element.getType() + ":lv=" + element.getLevelIndex() + ":id=" + (linkage == null ? "NULL" : (linkage.getFsc() + "|" + linkage.getComponentID()))); if (element instanceof ComplexElement) { ComplexElement complex = (ComplexElement) element; logger.warn("----Complex Element size=" + complex.size()); } } return; } featuresContext.put(feature.getFeatureType(), feature); } private void manualElementProcess(Element element) { } private void postProcessFeatureContext() throws IOException, IllegalAttributeException { if (featuresContext.size() == 0) return; for (Object key : featuresContext.keySet()) { FeatureType featureType = (FeatureType) key; List elements = (List) featuresContext.get(key); FeatureCollection features = FeatureCollections.newCollection(); features.addAll(elements); createFeatureStore(featureType).addFeatures(features); FeatureWriter writer = createFeatureWriter(featureType); for (Object v : elements) { Feature feature1 = (Feature) v; Object[] attrs = feature1.getAttributes(null); if (attrs != null) ((SimpleFeature) writer.next()).setAttributes(attrs); } writer.close(); } } private FeatureStore createFeatureStore(FeatureType featureType) throws IOException { File sfile = new File(getDataOutPath() + File.separator + featureType.getTypeName()); ShapefileDataStore shapefileDataStore = null; boolean existFile = sfile.exists(); shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL(), true, Charset.forName("UTF-8")); if (!existFile) { shapefileDataStore.createSchema(featureType); } return (FeatureStore) shapefileDataStore.getFeatureSource(featureType.getTypeName()); } private FeatureWriter createFeatureWriter(FeatureType featureType) throws IOException { File sfile = new File(getDataOutPath() + File.separator + featureType.getTypeName()); ShapefileDataStore shapefileDataStore = null; boolean existFile = sfile.exists(); shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL(), true, Charset.forName("UTF-8")); FeatureWriter writer; if (!existFile) { shapefileDataStore.createSchema(featureType); writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT); } else { writer = shapefileDataStore.getFeatureWriterAppend(featureType.getTypeName(), Transaction.AUTO_COMMIT); } return writer; } public String getDataOutPath() { File rootDir = new File(dataConfig.get(ROOTDATAPATH)); File dataDir = new File(rootDir, DEFAULT_SHPOUTPATH); return dataDir.toString(); } }