From 94ae08701bbd7585a0b7e5a92d1975965a503c03 Mon Sep 17 00:00:00 2001
From: Dennis Kao <ulysseskao@gmail.com>
Date: Wed, 15 Jan 2014 11:28:52 +0800
Subject: [PATCH] Merge branch 'origin/2.1.x'

---
 xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Element.java |  289 +++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 185 insertions(+), 104 deletions(-)

diff --git a/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Element.java b/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Element.java
index abe63f6..5c44664 100644
--- a/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Element.java
+++ b/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Element.java
@@ -5,7 +5,6 @@
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
-import java.nio.ShortBuffer;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -14,14 +13,12 @@
 import com.ximple.util.DgnUtility;
 
 /**
- * Record
+ * FileRecord
  *
  * @author Ulysses
  * @version 0.1
- * @since 2006/5/18 �W�� 11:14:50
  */
-public class Element
-{
+public class Element {
     public static final int CONSTRUCTION_CLASS = 0;
     public static final int CONSTRUCTION_RULE_CLASS = 0;
     public static final int DIMENSION_CLASS = 0;
@@ -38,9 +35,9 @@
     protected short[] raw;
     protected byte attrOffset = 0;
     protected ByteBuffer rawBuffer;
+    protected boolean newElement = false;
 
-    public Element(byte[] raw)
-    {
+    Element(byte[] raw) {
         // this.raw = raw;
         this.raw = new short[raw.length / 2];
         rawBuffer = ByteBuffer.wrap(raw);
@@ -48,13 +45,18 @@
         rawBuffer.asShortBuffer().get(this.raw);
     }
 
-    public int getLineStyle()
-    {
+    public int getLineStyle() {
         return (raw[17] & 0x0007);
     }
 
-    public Envelope getRange()
-    {
+    protected void setLineStyle(int value) {
+        if (value > -1 && value < 8)
+            raw[17] = (short) ((raw[17] & 0xfff8) | (value & 0x0007));
+        else
+            new IllegalArgumentException("Out of Range!");
+    }
+
+    public Envelope getRange() {
         int lowCoorX = ((raw[3] << 16) & 0xffff0000) + (raw[2] & 0x0000ffff);
         lowCoorX = DgnUtility.convertFromDGN(lowCoorX);
 
@@ -68,11 +70,10 @@
         highCoorY = DgnUtility.convertFromDGN(highCoorY);
 
         return new Envelope(DgnUtility.converUnitToCoord(lowCoorX), DgnUtility.converUnitToCoord(highCoorX),
-                DgnUtility.converUnitToCoord(lowCoorY), DgnUtility.converUnitToCoord(highCoorY));
+                            DgnUtility.converUnitToCoord(lowCoorY), DgnUtility.converUnitToCoord(highCoorY));
     }
 
-    public void setRange(Envelope bbox)
-    {
+    public void setRange(Envelope bbox) {
         int lowCoordX = DgnUtility.converCoordToUnit(bbox.getMinX());
         int temp = DgnUtility.converToDGN(lowCoordX);
         raw[3] = (short) (temp >> 16 & 0x0000ffff);
@@ -83,6 +84,10 @@
         raw[5] = (short) (temp >> 16 & 0x0000ffff);
         raw[4] = (short) (temp & 0x0000ffff);
 
+        // lowZ
+        raw[7] = 0;
+        raw[8] = 0;
+
         int highCoorX = DgnUtility.converCoordToUnit(bbox.getMaxX());
         temp = DgnUtility.converToDGN(highCoorX);
         raw[9] = (short) (temp >> 16 & 0x0000ffff);
@@ -92,132 +97,157 @@
         temp = DgnUtility.converToDGN(highCoorY);
         raw[11] = (short) (temp >> 16 & 0x0000ffff);
         raw[10] = (short) (temp & 0x0000ffff);
+
+        // highZ
+        raw[13] = (short) 0xffff;
+        raw[12] = (short) 0xffff;
+
     }
 
-    public boolean isComponentElement()
-    {
+    public boolean isComponentElement() {
         return (short) ((raw[0] >>> 7) & 0x0001) == 1;
     }
 
-    public boolean removeUserAttributeData(int iLinkageId)
-    {
+    protected void setComponentElement(boolean value) {
+        raw[0] = (short) ((raw[0] & 0xff7f) | (value ? 0x0080 : 0x0));
+    }
+
+    public boolean removeUserAttributeData(int iLinkageId) {
         return true;
     }
 
-    public boolean removeUserAttributeData(int iLinkageId, int iLinkageIndex)
-    {
+    public boolean removeUserAttributeData(int iLinkageId, int iLinkageIndex) {
         return true;
     }
 
-    public boolean isDeleted()
-    {
+    public boolean isDeleted() {
         return (short) ((raw[0] >>> 15) & 0x0001) == 1;
     }
 
-    public int getColorIndex()
-    {
+    protected void setDeleted(boolean value) {
+            raw[0] = (short) ((raw[0] & 0x7fff) | ((((value) ? 1 : 0) << 15) & 0x8000));
+    }
+
+    public int getColorIndex() {
         return ((raw[17] >>> 8) & 0x00ff);
     }
 
-    public int getType()
-    {
+    protected void setColorIndex(int value) {
+        if (value > -1 && value < 256)
+        {
+            raw[17] = (short) ((raw[17] & 0x00ff) | (value << 8 & 0xff00));
+        } else  new IllegalArgumentException("Out of Range!");
+    }
+
+    public int getType() {
         return ((raw[0] >>> 8) & 0x007f);
     }
 
-    public ElementType getElementType()
-    {
+    protected void setType(int value) {
+        raw[0] = (short) ((raw[0] & 0x80ff) | (value << 8) & 0x3f00);
+    }
+
+    public ElementType getElementType() {
         return ElementType.forID(getType());
     }
 
-    public int getLevelIndex()
-    {
+    public int getLevelIndex() {
         return (raw[0] & 0x003f);
     }
 
-    public void setLevelIndex(int value)
-    {
+    public void setLevelIndex(int value) {
         raw[0] = (short) ((raw[0] & 0xffc0) | (value & 0x003f));
     }
 
-    public int getWeight()
-    {
+    public int getWeight() {
         return ((raw[17] >>> 3) & 0x001f);
     }
 
-    public void setWeight(int value)
-    {
-        if (value > -1 && value < 31)
-        {
+    public void setWeight(int value) {
+        if (value > -1 && value < 31) {
             raw[17] = (short) ((raw[17] & 0xff07) | (value << 3 & 0x00f8));
-        }
-        else
-        {
+        } else {
             throw new RuntimeException("Out of Range!");
         }
     }
 
-    public void addUserAttributeData(byte[] pDataBlock, Class dataClass, int iLinkageId) throws Element.Exception
-    {
+    public short getFollowLength() {
+        return raw[1];
     }
 
-    public void addUserAttributeData(byte[] pDataBlock, int iLinkageId, Object oDataDef) throws Element.Exception
-    {
+    protected void setFollowLength(short value) {
+        assert (raw.length >= value + 2);
+        raw[1] = value;
     }
 
-    public boolean hasUserAttributeData()
-    {
-        if (raw[15] <= 0)
-        {
+    public void addUserAttributeData(byte[] pDataBlock, Class dataClass, int iLinkageId) throws Element.Exception {
+    }
+
+    public void addUserAttributeData(byte[] pDataBlock, int iLinkageId, Object oDataDef) throws Element.Exception {
+    }
+
+    public boolean hasUserAttributeData() {
+        if (raw[15] <= 0) {
             return false;
         }
 
         short index = (short) (raw[15] + 16);
 
-        if (index == -1)
-        {
+        if (index == -1) {
             return false;
         }
 
         return true;
     }
 
-    public List<UserAttributeData> getUserAttributeData()
-    {
+    public int getUserAttributeDataOffset() {
+        return (raw[15] + 16);
+    }
+
+    public List<UserAttributeData> getUserAttributeData() {
         short[] data;
         short length, nextAttribute;
 
-        if (raw[15] <= 0)
-        {
+        if (raw[15] <= 0) {
             return new ArrayList<UserAttributeData>();
         }
 
         short index = (short) (raw[15] + 16 + attrOffset);
 
-        if (index == -1)
-        {
+        if (index == -1) {
             return null;
         }
 
         ArrayList<UserAttributeData> aLinkageSet = new ArrayList<UserAttributeData>();
 
-        while (index < raw.length)
-        {
+        while (index < raw.length) {
+
+
+            if (raw[index] == 0 || ((short) raw[index]) == 0x8000)
+              {
+                    index += 4;
+                    aLinkageSet.add(null);
+                    continue;
+              }
+
             length = (short) (raw[index] & (short) 0x00ff);
 
-            if (length == 0)
-            {
+            if (length == 0) {
                 break;
+            }
+
+            if (length > raw.length - index - 1)
+            {
+                    return null;
             }
 
             nextAttribute = (short) (index + length + 1);
             data = new short[length];
             System.arraycopy(raw, index + 1, data, 0, length);
 
-            if (data[0] == (short) 0x0020)
-            {
+            if (data[0] == (short) 0x0020) {
                 aLinkageSet.add(new FrammeAttributeData(data));
-            } else
-            {
+            } else {
                 aLinkageSet.add(new UserAttributeData(data));
             }
 
@@ -227,48 +257,39 @@
         return aLinkageSet;
     }
 
-    public void getUserAttributeData(byte[] pDataBlock, Class dataClass, int iLinkageId, int iLinkageIndex)
-    {
+    public void getUserAttributeData(byte[] pDataBlock, Class dataClass, int iLinkageId, int iLinkageIndex) {
     }
 
-    public void getUserAttributeData(byte[] pDataBlock, int iLinkageId, Object oDataDef)
-    {
+    public void getUserAttributeData(byte[] pDataBlock, int iLinkageId, Object oDataDef) {
     }
 
 
-    public ByteBuffer getRawBuffer()
-    {
+    public ByteBuffer getRawBuffer() {
         return rawBuffer.asReadOnlyBuffer();
     }
 
-    public short[] getRawArray()
-    {
+    public short[] getRawArray() {
         if (raw == null) return null;
         short[] result = new short[raw.length];
         System.arraycopy(raw, 0, result, 0, raw.length);
         return result;
     }
 
-    public static class Exception extends java.lang.Exception
-    {
-        public Exception()
-        {
+    public static class Exception extends java.lang.Exception {
+        public Exception() {
         }
 
-        // Constructs an Record.Exception with no detail message.
-        public Exception(String oStrMessage)
-        {
+        // Constructs an ElementRecord.Exception with no detail message.
+        public Exception(String oStrMessage) {
             super(oStrMessage);
         }
     }
 
-    protected static int getOffsetPosition(int offset)
-    {
+    protected static int getOffsetPosition(int offset) {
         return offset * 2;
     }
 
-    public void resyncBuffer()
-    {
+    public void resyncBuffer() {
         byte[] tempRaw = new byte[this.raw.length * 2];
         ByteBuffer tempBuffer = ByteBuffer.wrap(tempRaw);
         tempBuffer.order(ByteOrder.LITTLE_ENDIAN);
@@ -279,28 +300,22 @@
         rawBuffer.position(pos);
     }
 
-    public static class ElementHandler implements IElementHandler
-    {
+    public static class ElementHandler implements IElementHandler {
         ElementType elementType;
 
-        public ElementHandler(ElementType elementType)
-        {
+        public ElementHandler(ElementType elementType) {
             this.elementType = elementType;
         }
 
-        public ElementType getElementType()
-        {
+        public ElementType getElementType() {
             return elementType;
         }
 
-        public Object read(ByteBuffer buffer, short signature, int length)
-        {
+        public Element read(ByteBuffer buffer, short signature, int length) {
             byte[] dst = new byte[length];
-            try
-            {
+            try {
                 buffer.get(dst, 4, dst.length - 4);
-            } catch (BufferUnderflowException exception)
-            {
+            } catch (BufferUnderflowException exception) {
                 throw exception;
             }
 
@@ -323,24 +338,90 @@
             return elm;
         }
 
-        public void write(ByteBuffer buffer, Object element)
-        {
+        public void write(ByteBuffer buffer, Object element) {
             buffer.put(((Element) element).rawBuffer);
         }
 
-        public int getLength(Object element)
-        {
+        public int getLength(Object element) {
             return ((Element) element).raw.length;
         }
 
-        public int getBufferLength(Object element)
-        {
+        public int getBufferLength(Object element) {
             return ((Element) element).rawBuffer.limit();
         }
 
-        protected Element createElement(byte[] raw)
-        {
+        protected Element createElement(byte[] raw) {
             return new Element(raw);
         }
     }
+
+    public static final class FileRecord {
+        int length;
+        int number = 0;
+        int offset;           // Relative to the whole file
+        int start = 0;    // Relative to the current loaded buffer
+        short signature = 0;
+
+        /**
+         * The minimum X value.
+         */
+        public double minX;
+
+        /**
+         * The minimum Y value.
+         */
+        public double minY;
+
+        /**
+         * The minimum Z value.
+         */
+        public double minZ;
+
+        /**
+         * The maximum X value.
+         */
+        public double maxX;
+
+        /**
+         * The maximum Y value.
+         */
+        public double maxY;
+
+        /**
+         * The maximum Z value.
+         */
+        public double maxZ;
+
+        // ElementType type;
+        int end = 0;    // Relative to the whole file
+        Object element = null;
+        IElementHandler handler;
+        ByteBuffer buffer;
+
+        public Object element() {
+            if (element == null) {
+                buffer.position(start);
+                buffer.order(ByteOrder.LITTLE_ENDIAN);
+
+                if (handler == null) {
+                    return null;
+                }
+
+                element = handler.read(buffer, signature, length);
+            }
+
+            return element;
+        }
+
+        public int offset() {
+            return offset;
+        }
+
+        /**
+         * A summary of the record.
+         */
+        public String toString() {
+            return "FileRecord " + number + " length " + length + " bounds " + minX + "," + minY + " " + maxX + "," + maxY;
+        }
+    }
 }

--
Gitblit v0.0.0-SNAPSHOT