forked from geodmms/xdgnjobs

?? ?
2011-10-24 1cc0f32f7fefa2821a052f823e735bdb14193b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
    }
}