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 * Time: �W�� 12:19:14 */ 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 usrData = textElm.getUserAttributeData(); Iterator 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); } }