ImageCodec.jvm.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. using System;
  2. using System.Configuration;
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Drawing.Imaging;
  6. using System.Xml;
  7. using Mainsoft.Drawing.Configuration;
  8. using imageio = javax.imageio;
  9. using stream = javax.imageio.stream;
  10. using awt = java.awt;
  11. using image = java.awt.image;
  12. using spi = javax.imageio.spi;
  13. using dom = org.w3c.dom;
  14. namespace Mainsoft.Drawing.Imaging {
  15. /// <summary>
  16. /// Summary description for ImageCodec.
  17. /// </summary>
  18. [MonoTODO]
  19. public class ImageCodec : IDisposable {
  20. #region Members
  21. imageio.ImageReader _nativeReader = null;
  22. imageio.ImageWriter _nativeWriter = null;
  23. stream.ImageInputStream _nativeStream = null;
  24. ImageFormat _imageFormat = null;
  25. int _currentFrame = 0;
  26. #endregion
  27. #region Constructros
  28. protected ImageCodec() {
  29. }
  30. static ImageCodec() {
  31. }
  32. #endregion
  33. #region Internal properties
  34. internal imageio.ImageReader NativeReader {
  35. get { return _nativeReader; }
  36. set {
  37. _nativeReader = value;
  38. if (value == null)
  39. return;
  40. _imageFormat = MimeTypesToImageFormat( value.getOriginatingProvider().getMIMETypes() );
  41. }
  42. }
  43. internal imageio.ImageWriter NativeWriter {
  44. get { return _nativeWriter; }
  45. set {
  46. _nativeWriter = value;
  47. if (value == null)
  48. return;
  49. _imageFormat = MimeTypesToImageFormat( value.getOriginatingProvider().getMIMETypes() );
  50. }
  51. }
  52. internal stream.ImageInputStream NativeStream {
  53. get { return _nativeStream; }
  54. set {
  55. _nativeStream = value;
  56. if (value == null)
  57. return;
  58. if (NativeReader != null)
  59. NativeReader.setInput( value );
  60. if (NativeWriter != null)
  61. NativeWriter.setOutput( value );
  62. }
  63. }
  64. #endregion
  65. #region ImageCodec factory methods
  66. public static ImageCodec CreateReader(stream.ImageInputStream inputStream) {
  67. java.util.Iterator iter = imageio.ImageIO.getImageReaders( inputStream );
  68. return CreateReader(iter);
  69. }
  70. public static ImageCodec CreateReader(ImageFormat imageFormat) {
  71. return CreateReader( ImageFormatToClsid( imageFormat ) );
  72. }
  73. public static ImageCodec CreateReader(Guid clsid) {
  74. ImageCodec codec = null;
  75. try {
  76. ImageCodecInfo codecInfo = FindDecoder(clsid);
  77. java.util.Iterator iter = imageio.ImageIO.getImageReadersByMIMEType( codecInfo.MimeType );
  78. codec = CreateReader(iter);
  79. }
  80. catch {}
  81. if (codec == null) {
  82. ImageFormat format = ClsidToImageFormat(clsid);
  83. string name = (format != null) ? format.ToString() : clsid.ToString();
  84. throw new NotSupportedException(String.Format("The '{0}' format decoder is not installed.", name));
  85. }
  86. return codec;
  87. }
  88. private static ImageCodec CreateReader(java.util.Iterator iter) {
  89. if ( !iter.hasNext() )
  90. return null;
  91. ImageCodec imageCodec = new ImageCodec();
  92. imageCodec.NativeReader = (imageio.ImageReader) iter.next();
  93. return imageCodec;
  94. }
  95. public static ImageCodec CreateWriter(ImageFormat imageFormat) {
  96. return CreateWriter( ImageFormatToClsid( imageFormat ) );
  97. }
  98. public static ImageCodec CreateWriter(Guid clsid) {
  99. ImageCodec codec = null;
  100. try {
  101. ImageCodecInfo codecInfo = FindEncoder(clsid);
  102. java.util.Iterator iter = imageio.ImageIO.getImageWritersByMIMEType( codecInfo.MimeType );
  103. codec = CreateWriter(iter);
  104. }
  105. catch {}
  106. if (codec == null) {
  107. ImageFormat format = ClsidToImageFormat(clsid);
  108. string name = (format != null) ? format.ToString() : clsid.ToString();
  109. throw new NotSupportedException(String.Format("The '{0}' format encoder is not installed.", name));
  110. }
  111. return codec;
  112. }
  113. private static ImageCodec CreateWriter(java.util.Iterator iter) {
  114. if ( !iter.hasNext() )
  115. return null;
  116. ImageCodec imageCodec = new ImageCodec();
  117. imageCodec.NativeWriter = (imageio.ImageWriter) iter.next();
  118. return imageCodec;
  119. }
  120. #endregion
  121. #region Codec enumerations
  122. internal static Hashtable Decoders {
  123. get {
  124. const string MYNAME = "System.Drawing.Imaging.ImageCodecInfo.decoders";
  125. Hashtable o = (Hashtable) AppDomain.CurrentDomain.GetData (MYNAME);
  126. if (o != null)
  127. return o;
  128. o = new ReaderSpiIterator().Iterate();
  129. AppDomain.CurrentDomain.SetData(MYNAME, o);
  130. return o;
  131. }
  132. }
  133. internal static Hashtable Encoders {
  134. get {
  135. const string MYNAME = "System.Drawing.Imaging.ImageCodecInfo.encoders";
  136. Hashtable o = (Hashtable) AppDomain.CurrentDomain.GetData (MYNAME);
  137. if (o != null)
  138. return o;
  139. o = new WriterSpiIterator().Iterate();
  140. AppDomain.CurrentDomain.SetData(MYNAME, o);
  141. return o;
  142. }
  143. }
  144. internal static ImageCodecInfo FindEncoder (Guid clsid) {
  145. ImageCodecInfo codec = (ImageCodecInfo) Encoders[clsid];
  146. if (codec == null) {
  147. // .net saves in png if cannot find requested encoder. atc id 316563
  148. codec = (ImageCodecInfo) Encoders[ ImageCodec.PngClsid ];
  149. }
  150. return codec;
  151. }
  152. internal static ImageCodecInfo FindDecoder (Guid clsid) {
  153. ImageCodecInfo codec = (ImageCodecInfo) Decoders[clsid];
  154. if (codec == null) {
  155. ImageFormat format = ClsidToImageFormat(clsid);
  156. string name = (format != null) ? format.ToString() : clsid.ToString();
  157. throw new NotSupportedException(String.Format("The '{0}' format decoder is not installed.", name));
  158. }
  159. return codec;
  160. }
  161. #endregion
  162. #region SpiIterators
  163. abstract class BaseSpiIterator {
  164. protected abstract java.util.Iterator GetIterator (string mimeType);
  165. protected abstract spi.ImageReaderWriterSpi GetNext (java.util.Iterator iter);
  166. #region ProcessOneCodec
  167. private ImageCodecInfo ProcessOneCodec (Guid clsid, Guid formatID, string mimeType) {
  168. ImageCodecInfo ici = new ImageCodecInfo ();
  169. ici.Clsid = clsid;
  170. ici.FormatID = formatID;
  171. ici.MimeType = mimeType;
  172. java.util.Iterator iter = null;
  173. try {
  174. iter = GetIterator (mimeType);
  175. }
  176. catch(Exception) {
  177. return null;
  178. }
  179. while (iter.hasNext ()) {
  180. spi.ImageReaderWriterSpi rw = GetNext (iter);
  181. ici.CodecName = rw.getDescription (java.util.Locale.getDefault ());
  182. ici.DllName = null;
  183. foreach (string suffix in rw.getFileSuffixes ()) {
  184. if (ici.FilenameExtension != null)
  185. ici.FilenameExtension += ";";
  186. ici.FilenameExtension += "*."+suffix;
  187. }
  188. ici.Flags = ImageCodecFlags.Builtin|ImageCodecFlags.SupportBitmap;
  189. if (rw is spi.ImageReaderSpi)
  190. ici.Flags |= ImageCodecFlags.Decoder;
  191. if (rw is spi.ImageWriterSpi)
  192. ici.Flags |= ImageCodecFlags.Encoder;
  193. ici.FormatDescription = string.Join(";",
  194. rw.getFormatNames());
  195. try {
  196. ici.Version = (int)Convert.ToDouble(rw.getVersion ());
  197. }
  198. catch (Exception) {
  199. ici.Version = 1;
  200. }
  201. break;
  202. }
  203. return ici;
  204. }
  205. #endregion
  206. internal Hashtable Iterate () {
  207. // TBD: Insert Exception handling here
  208. NameValueCollection nvc = (NameValueCollection) System.Configuration.ConfigurationSettings
  209. .GetConfig ("system.drawing/codecs");
  210. Hashtable codecs = new Hashtable (10);
  211. for (int i=0; i<nvc.Count; i++) {
  212. Guid clsid = new Guid (nvc.GetKey (i));
  213. ImageFormat format = ClsidToImageFormat (clsid);
  214. ImageCodecInfo codec = ProcessOneCodec (clsid, format.Guid, nvc[i]);
  215. if ((codec != null) && (codec.FilenameExtension != null))
  216. codecs [clsid] = codec;
  217. }
  218. return codecs;
  219. }
  220. }
  221. class ReaderSpiIterator: BaseSpiIterator {
  222. protected override java.util.Iterator GetIterator(string mimeType) {
  223. return imageio.ImageIO.getImageReadersByMIMEType (mimeType);
  224. }
  225. protected override javax.imageio.spi.ImageReaderWriterSpi GetNext(java.util.Iterator iter) {
  226. imageio.ImageReader r = (imageio.ImageReader) iter.next ();
  227. return r.getOriginatingProvider ();
  228. }
  229. }
  230. class WriterSpiIterator: BaseSpiIterator {
  231. protected override java.util.Iterator GetIterator(string mimeType) {
  232. return imageio.ImageIO.getImageWritersByMIMEType (mimeType);
  233. }
  234. protected override javax.imageio.spi.ImageReaderWriterSpi GetNext(java.util.Iterator iter) {
  235. imageio.ImageWriter w = (imageio.ImageWriter) iter.next ();
  236. return w.getOriginatingProvider ();
  237. }
  238. }
  239. #endregion
  240. #region Clsid and FormatID
  241. static Guid BmpClsid = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
  242. static Guid JpegClsid = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
  243. static Guid GifClsid = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
  244. static Guid EmfClsid = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
  245. static Guid WmfClsid = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
  246. static Guid TiffClsid = new Guid ("557cf405-1a04-11d3-9a73-0000f81ef32e");
  247. static Guid PngClsid = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
  248. static Guid IconClsid = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  249. private static ImageFormat MimeTypesToImageFormat (string [] mimeTypes) {
  250. foreach (ImageCodecInfo codec in Decoders.Values)
  251. for (int i=0; i<mimeTypes.Length; i++)
  252. if (codec.MimeType == mimeTypes [i])
  253. return new ImageFormat (codec.FormatID);
  254. return null;
  255. }
  256. internal static ImageFormat ClsidToImageFormat (Guid clsid) {
  257. if (clsid.Equals (BmpClsid))
  258. return ImageFormat.Bmp;
  259. else if (clsid.Equals (JpegClsid))
  260. return ImageFormat.Jpeg;
  261. else if (clsid.Equals (GifClsid))
  262. return ImageFormat.Gif;
  263. else if (clsid.Equals (EmfClsid))
  264. return ImageFormat.Emf;
  265. else if (clsid.Equals (WmfClsid))
  266. return ImageFormat.Wmf;
  267. else if (clsid.Equals (TiffClsid))
  268. return ImageFormat.Tiff;
  269. else if (clsid.Equals (PngClsid))
  270. return ImageFormat.Png;
  271. else if (clsid.Equals (IconClsid))
  272. return ImageFormat.Icon;
  273. else
  274. return null;
  275. }
  276. internal static Guid ImageFormatToClsid (ImageFormat format) {
  277. if (format == null)
  278. return Guid.Empty;
  279. if (format.Guid.Equals (ImageFormat.Bmp.Guid))
  280. return BmpClsid;
  281. else if (format.Guid.Equals (ImageFormat.Jpeg.Guid))
  282. return JpegClsid;
  283. else if (format.Guid.Equals (ImageFormat.Gif))
  284. return GifClsid;
  285. else if (format.Guid.Equals (ImageFormat.Emf.Guid))
  286. return EmfClsid;
  287. else if (format.Guid.Equals (ImageFormat.Wmf.Guid))
  288. return WmfClsid;
  289. else if (format.Guid.Equals (ImageFormat.Tiff.Guid))
  290. return TiffClsid;
  291. else if (format.Guid.Equals (ImageFormat.Png.Guid))
  292. return PngClsid;
  293. else if (format.Guid.Equals (ImageFormat.Icon.Guid))
  294. return IconClsid;
  295. else
  296. return Guid.Empty;
  297. }
  298. private FrameDimension FormatFrameDimesion {
  299. get {
  300. if (ImageFormat == null)
  301. return FrameDimension.Page;
  302. if (ImageFormat.Guid.Equals (ImageFormat.Bmp.Guid))
  303. return FrameDimension.Page;
  304. else if (ImageFormat.Guid.Equals (ImageFormat.Jpeg.Guid))
  305. return FrameDimension.Page;
  306. else if (ImageFormat.Guid.Equals (ImageFormat.Gif))
  307. return FrameDimension.Time;
  308. else if (ImageFormat.Guid.Equals (ImageFormat.Emf.Guid))
  309. return FrameDimension.Page;
  310. else if (ImageFormat.Guid.Equals (ImageFormat.Wmf.Guid))
  311. return FrameDimension.Page;
  312. else if (ImageFormat.Guid.Equals (ImageFormat.Tiff.Guid))
  313. return FrameDimension.Page;
  314. else if (ImageFormat.Guid.Equals (ImageFormat.Png.Guid))
  315. return FrameDimension.Page;
  316. else if (ImageFormat.Guid.Equals (ImageFormat.Icon.Guid))
  317. return FrameDimension.Resolution;
  318. else
  319. return FrameDimension.Page;
  320. }
  321. }
  322. #endregion
  323. #region Image read/write methods
  324. [MonoTODO]
  325. public PlainImage ReadPlainImage() {
  326. awt.Image img = ReadImage( _currentFrame );
  327. if (img == null)
  328. return null;
  329. // its possible to fail to load thumbnails and metadata, but image is ok.
  330. awt.Image [] th = null;
  331. #if THUMBNAIL_SUPPORTED
  332. try {
  333. th = ReadThumbnails( _currentFrame );
  334. }
  335. catch (Exception) {}
  336. #endif
  337. XmlDocument md = null;
  338. imageio.metadata.IIOMetadata nativeMd = null;
  339. try {
  340. nativeMd = ReadImageMetadata( _currentFrame );
  341. md = ConvertImageMetadata( nativeMd );
  342. }
  343. catch (Exception) {}
  344. float [] resolution = GetResolution( md );
  345. PlainImage pi = new PlainImage( img, th, ImageFormat, resolution[0], resolution[1], FormatFrameDimesion );
  346. pi.NativeMetadata = nativeMd;
  347. return pi;
  348. }
  349. public PlainImage ReadNextPlainImage() {
  350. _currentFrame++;
  351. return ReadPlainImage();
  352. }
  353. private awt.Image ReadImage(int frame) {
  354. if (NativeStream == null)
  355. throw new Exception("Input stream not specified");
  356. try {
  357. return NativeReader.read (frame);
  358. }
  359. catch (java.lang.IndexOutOfBoundsException) {
  360. return null;
  361. }
  362. catch (java.io.IOException ex) {
  363. throw new System.IO.IOException(ex.Message, ex);
  364. }
  365. }
  366. #if THUMBNAIL_SUPPORTED
  367. private awt.Image [] ReadThumbnails(int frameIndex) {
  368. awt.Image [] thArray = null;
  369. try {
  370. if (NativeReader.readerSupportsThumbnails()) {
  371. int tmbNumber = NativeReader.getNumThumbnails(frameIndex);
  372. if (tmbNumber > 0) {
  373. thArray = new awt.Image[ tmbNumber ];
  374. for (int i = 0; i < tmbNumber; i++) {
  375. thArray[i] = NativeReader.readThumbnail(frameIndex, i);
  376. }
  377. }
  378. }
  379. return thArray;
  380. }
  381. catch (java.io.IOException ex) {
  382. throw new System.IO.IOException(ex.Message, ex);
  383. }
  384. }
  385. #endif
  386. public void WritePlainImage(PlainImageCollection pic) {
  387. if ((pic == null) || (pic.Count == 0))
  388. return;
  389. if (pic.Count == 1) {
  390. WritePlainImage( pic[0] );
  391. return;
  392. }
  393. try {
  394. if (NativeWriter.canWriteSequence ()) {
  395. NativeWriter.prepareWriteSequence (null);
  396. for (int i=0; i < pic.Count; i++) {
  397. imageio.IIOImage iio = GetIIOImageContainer( pic[i] );
  398. NativeWriter.writeToSequence (iio, null);
  399. }
  400. NativeWriter.endWriteSequence ();
  401. }
  402. else
  403. WritePlainImage( pic[0] );
  404. }
  405. catch (java.io.IOException ex) {
  406. throw new System.IO.IOException(ex.Message, ex);
  407. }
  408. }
  409. public void WritePlainImage(PlainImage pi) {
  410. try {
  411. imageio.IIOImage iio = GetIIOImageContainer( pi );
  412. WriteImage( iio );
  413. }
  414. catch (java.io.IOException ex) {
  415. throw new System.IO.IOException(ex.Message, ex);
  416. }
  417. }
  418. private void WriteImage(imageio.IIOImage iio) {
  419. if (NativeStream == null)
  420. throw new Exception("Output stream not specified");
  421. NativeWriter.write( iio );
  422. }
  423. private imageio.IIOImage GetIIOImageContainer(PlainImage pi) {
  424. java.util.ArrayList al = null;
  425. // prepare thumbnails list
  426. if (pi.Thumbnails != null) {
  427. al = new java.util.ArrayList( pi.Thumbnails.Length );
  428. for (int i=0; i < pi.Thumbnails.Length; i++)
  429. al.add(pi.Thumbnails[i]);
  430. }
  431. // prepare IIOImage container
  432. if (pi.NativeImage is image.BufferedImage) {
  433. imageio.IIOImage iio = new javax.imageio.IIOImage(
  434. (image.BufferedImage)pi.NativeImage, al, null /*pi.NativeMetadata*/);
  435. return iio;
  436. }
  437. else
  438. // TBD: This codec is for raster formats only
  439. throw new NotSupportedException("Only raster formats are supported");
  440. }
  441. private imageio.metadata.IIOMetadata ReadImageMetadata(int frameIndex) {
  442. if (NativeStream == null)
  443. throw new Exception("Input stream not specified");
  444. try {
  445. imageio.metadata.IIOMetadata md = NativeReader.getImageMetadata( frameIndex );
  446. return md;
  447. }
  448. catch (java.io.IOException ex) {
  449. throw new System.IO.IOException(ex.Message, ex);
  450. }
  451. }
  452. #endregion
  453. #region Extra properties
  454. public ImageFormat ImageFormat {
  455. get { return _imageFormat; }
  456. }
  457. #endregion
  458. #region Metadata parse
  459. private float [] GetResolution(XmlDocument metaData) {
  460. if (metaData == null)
  461. return new float[]{0, 0};
  462. ResolutionConfigurationCollection rcc =
  463. (ResolutionConfigurationCollection)
  464. ConfigurationSettings.GetConfig("system.drawing/codecsmetadata");
  465. if (rcc == null)
  466. throw new ConfigurationException("Configuration section codecsmetadata not found");
  467. ResolutionConfiguration rc = rcc[ ImageFormat.ToString() ];
  468. if (rc == null)
  469. return new float[]{0, 0};
  470. // Horizontal resolution
  471. string xResPath = rc.XResPath;
  472. string xRes;
  473. if (xResPath == string.Empty)
  474. xRes = rc.XResDefault;
  475. else
  476. xRes = GetValueFromMetadata(metaData, xResPath);
  477. if ((xRes == null) || (xRes == string.Empty))
  478. xRes = rc.XResDefault;
  479. // Vertical resolution
  480. string yResPath = rc.YResPath;
  481. string yRes;
  482. if (yResPath == string.Empty)
  483. yRes = rc.YResDefault;
  484. else
  485. yRes = GetValueFromMetadata(metaData, yResPath);
  486. if ((yRes == null) || (yRes == string.Empty))
  487. yRes = rc.YResDefault;
  488. // Resolution units
  489. string resUnitsPath = rc.UnitsTypePath;
  490. string resUnitsType;
  491. if (resUnitsPath == string.Empty)
  492. resUnitsType = rc.UnitsTypeDefault;
  493. else
  494. resUnitsType = GetValueFromMetadata(metaData, resUnitsPath);
  495. if (resUnitsType == null)
  496. resUnitsType = rc.UnitsTypeDefault;
  497. // Unit scale
  498. string unitScale = rc.UnitsScale[resUnitsType].ToString();
  499. // Adjust resolution to its units
  500. float [] res = new float[2];
  501. res[0] = ParseFloatValue(xRes) * ParseFloatValue(unitScale);
  502. res[1] = ParseFloatValue(yRes) * ParseFloatValue(unitScale);
  503. return res;
  504. }
  505. private string GetValueFromMetadata(XmlDocument metaData, string path) {
  506. XmlNode n = metaData.SelectSingleNode(path);
  507. if (n == null)
  508. return null;
  509. return n.InnerText;
  510. }
  511. private XmlDocument ConvertImageMetadata(imageio.metadata.IIOMetadata metaData) {
  512. string [] formatNames = metaData.getMetadataFormatNames();
  513. dom.Element rootNode = (dom.Element) metaData.getAsTree(formatNames[0]);
  514. XmlDocument _metadataDocument = new XmlDocument();
  515. XmlConvert(rootNode, _metadataDocument);
  516. return _metadataDocument;
  517. }
  518. private void XmlConvert(dom.Node jNode, XmlNode nNode) {
  519. XmlDocument document = nNode.OwnerDocument;
  520. if (document == null)
  521. document = (XmlDocument)nNode;
  522. XmlNode n = null;
  523. switch (jNode.getNodeType()) {
  524. case 1 :
  525. n = document.CreateNode(XmlNodeType.Element, jNode.getNodeName(), jNode.getNamespaceURI());
  526. break;
  527. case 4 :
  528. n = document.CreateNode(XmlNodeType.CDATA, jNode.getNodeName(), jNode.getNamespaceURI());
  529. break;
  530. default:
  531. return;
  532. }
  533. //set value
  534. n.InnerText = jNode.getNodeValue();
  535. nNode.AppendChild( n );
  536. //copy attributes
  537. org.w3c.dom.NamedNodeMap nm = jNode.getAttributes();
  538. for (int i=0; i<nm.getLength(); i++) {
  539. XmlAttribute a = document.CreateAttribute( nm.item(i).getNodeName() );
  540. a.Value = nm.item(i).getNodeValue();
  541. n.Attributes.Append( a );
  542. }
  543. //copy childs
  544. org.w3c.dom.NodeList nl = jNode.getChildNodes();
  545. for (int i=0; i<nl.getLength(); i++) {
  546. XmlConvert(nl.item(i), n);
  547. }
  548. }
  549. protected virtual float ParseFloatValue(string strValue) {
  550. try {
  551. if ((strValue != null) && (strValue != "")) {
  552. int dividerPos = strValue.IndexOf("/");
  553. if (dividerPos < 0) {
  554. return float.Parse(strValue);
  555. }
  556. else {
  557. return float.Parse(strValue.Substring( 0, dividerPos )) /
  558. float.Parse(strValue.Substring( dividerPos + 1 ));
  559. }
  560. }
  561. return float.NaN;
  562. }
  563. catch (Exception) {
  564. return float.NaN;
  565. }
  566. }
  567. #endregion
  568. #region IDisposable members
  569. public void Dispose() {
  570. if (NativeReader != null) {
  571. NativeReader.dispose();
  572. NativeReader = null;
  573. }
  574. if (NativeWriter != null) {
  575. NativeWriter.dispose();
  576. NativeWriter = null;
  577. }
  578. }
  579. #endregion
  580. }
  581. }