forked from geodmms/xdgnjobs

Dennis Kao
2013-06-05 fc9880cd77a09dcfdea2791dee418f54f5721cf3
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.ximple.io.dgn7;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Iterator;
import java.util.List;
 
import org.apache.log4j.Logger;
import org.geotools.TestData;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 
/**
 * Dgn7TextElementReaderTest
 * User: Ulysses
 * Date: 2008/1/10
 */
public class Dgn7TextElementReaderTest {
    private final static Logger logger = Logger.getLogger(Dgn7fileReaderTest.class);
 
    private final static String testFilePathCreated = "demo.dgn";
    private final static String testFilePathExist = "HV88491-1.dgn";
    private final static String testFilePathPostComplete = "HV88494_0.dgn";
 
    private FileInputStream _fs;
 
    @BeforeTest
    public void setUp() throws FileNotFoundException {
    }
 
    @Test
    public void testRead() throws Dgn7fileException, IOException {
        File dataFile = TestData.file(this, testFilePathCreated);
        if (dataFile.exists()) {
            System.out.println("Output--" + testFilePathCreated);
            _fs = new FileInputStream(dataFile);
            FileChannel fc = _fs.getChannel();
            dumpElements(fc);
            fc.close();
            _fs.close();
        }
 
        dataFile = TestData.file(this, testFilePathExist);
        if (dataFile.exists()) {
            System.out.println("Output--" + testFilePathExist);
            _fs = new FileInputStream(dataFile);
            FileChannel fc = _fs.getChannel();
            dumpElements(fc);
            fc.close();
            _fs.close();
        }
 
        dataFile = TestData.file(this, testFilePathPostComplete);
        if (dataFile.exists()) {
            System.out.println("Output--" + testFilePathPostComplete);
            _fs = new FileInputStream(dataFile);
            FileChannel fc = _fs.getChannel();
            dumpElements(fc);
            fc.close();
            _fs.close();
        }
    }
 
    public void dumpElements(FileChannel fc) throws Dgn7fileException, IOException {
        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();
 
                if ((!type.isComplexElement()) && (!element.isComponentElement())) {
                    if (lastComplex != null) {
                        // @todo add process in here
                        lastComplex = null;
                    }
 
                    // @todo add process in here
                } else if (element.isComponentElement()) {
                    if (lastComplex != null) {
                        ((ComplexElement) lastComplex).add(element);
                    }
                } else if (type.isComplexElement()) {
                    if (lastComplex == null) {
                        lastComplex = element;
                    } else {
                        // @todo add process in here
                        lastComplex = element;
                    }
                }
 
                if (element.getElementType().isComplexElement()) {
                    if (element instanceof ComplexChainElement) {
                        ComplexChainElement complexChain = (ComplexChainElement) element;
                        int size = complexChain.size();
                        for (Object aComplexChain : complexChain) {
                            Element subElement = (Element) aComplexChain;
                            subElement.getType();
                        }
                    }
 
                    if (element instanceof ComplexShapeElement) {
                        ComplexShapeElement complexShape = (ComplexShapeElement) element;
                    }
 
                    if (element instanceof TextNodeElement) {
                        TextNodeElement textNode = (TextNodeElement) element;
                        int size = textNode.size();
                        for (int i = 0; i < size; i++) {
                            Element subElement = (Element) textNode.get(i);
                            subElement.getElementType();
                        }
                    }
 
                } else {
                    boolean hasLinkage = false;
                    if (element instanceof TextElement) {
                        TextElement textElm = (TextElement) element;
                        List<UserAttributeData> usrData = textElm.getUserAttributeData();
                        Iterator<UserAttributeData> it = usrData.iterator();
                        while (it.hasNext()) {
                            UserAttributeData attr = it.next();
                            if (attr instanceof FrammeAttributeData) {
                                hasLinkage = true;
                                System.out.println("------------------------------------------");
                                System.out.println("FSC=" + ((FrammeAttributeData) attr).getFsc() + ":" +
                                    ((FrammeAttributeData) attr).getUfid());
                            }
                        }
 
                        if (hasLinkage) {
                            System.out.println("Text.Font=" + textElm.getFontIndex());
                            System.out.println("Text.Just=" + textElm.getJustification());
                            System.out.println("usrData.len=" + usrData.size());
                            System.out.println("text=" + textElm.getText());
                            System.out.println("Origin=" + textElm.getOrigin());
                            System.out.println("UserOrigin=" + textElm.getUserOrigin());
                        }
                    }
                }
            }
            count++;
        }
 
        logger.info("FileRecord Count=" + count);
    }
}