package com.ximple.io.dgn7; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.geotools.TestData; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; public class Dgn7fileWriterTest { private final static Logger logger = Logger.getLogger(Dgn7fileReaderTest.class); // private final static String testFilePath = "test-data\\testHV.dgn"; private final static String testFilePath = "testHV.dgn"; private FileInputStream _fs; @BeforeTest public void setUp() throws IOException { File dataFile = TestData.file(this, testFilePath); if (!dataFile.exists()) { return; } _fs = new FileInputStream(dataFile); } @Test public void testWrite() { } @Test public void testCopy() throws Dgn7fileException, IOException { File target = TestData.temp(this, "testdgn2d.dgn"); FileUtils.copyFile(TestData.file(this, "dgnseed2d.dgn"), target); RandomAccessFile targetStream = new RandomAccessFile(target, "rw"); FileChannel fctarget = targetStream.getChannel(); Lock lock = new Lock(); Dgn7fileReader targetReader = new Dgn7fileReader(fctarget, new Lock()); while (targetReader.hasNext()) { targetReader.nextElement(); } Dgn7fileWriter writer = new Dgn7fileWriter(fctarget, lock); FileChannel fc = _fs.getChannel(); Dgn7fileReader reader = new Dgn7fileReader(fc, new Lock()); int count = 0; Element lastComplex = null; while (reader.hasNext()) { Element.FileRecord record = reader.nextElement(); if (record.element() != null) { Element element = (Element) record.element(); ElementType type = element.getElementType(); boolean completed = false; if ((!type.isComplexElement()) && (!element.isComponentElement())) { if (lastComplex != null) { // @todo add process in here processCompleteElement(lastComplex, writer); lastComplex = null; } // @todo add process in here processCompleteElement(element, writer); } else if (element.isComponentElement()) { if (lastComplex != null) { ((ComplexElement) lastComplex).add(element); } else { logger.warn("wong." + element.toString()); Assert.fail("Component Element cannot found parent."); } } else if (type.isComplexElement()) { if (lastComplex != null) { // @todo add process in here processCompleteElement(lastComplex, writer); } lastComplex = element; } // writer.writeElement(element); } count++; } writer.writeEOF(); writer.close(); // FileUtils.copyFile(target, new File("G://target.dgn")); } private boolean processCompleteElement(Element element, Dgn7fileWriter writer) throws IOException { writer.writeElement(element); return true; } }