<?xml version="1.0" encoding="UTF-8"?>
<rmml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="L:\collaborative-nir\program\Cscl3DWS\rmml\RMML.xsd">
	<!-- Рабочая область 2Dконструктора -->
	<composition name="CMCParser">
		<xml name="xmlEngine"/>
		<script src="config.mjs"/>
		<script src="atom.mjs"/>
		<script src="cycle.mjs"/>
		<script src="speccycles.mjs"/>
		<script src="board.mjs"/>
		<script src="relatives.mjs"/>
		<script src="groups.mjs"/>
		<script src="visual.mjs"/>
		<script src="marker.mjs"/>
		<script src="optimization.mjs"/>
		<script src="linkEvents.mjs"/>
		<script><![CDATA[
			this.loadedMolecule = null;

			// Загрузка данных молекулы из тега isomer
			function load()
			{
				if( this.xmlEngine.XMLDocument == null ||
					typeof(this.xmlEngine.XMLDocument) == "undefined")
				{
					trace("mc2D parser error: can`t load molecule file");
					return null;
				}
				if( this.xmlEngine.XMLDocument.firstChild == null ||
					typeof(this.xmlEngine.XMLDocument.firstChild) == "undefined" ||
					this.xmlEngine.XMLDocument.firstChild.nodeName != "isomer")
				{
					trace("this.xmlEngine="+this.xmlEngine.src);
					if( this.xmlEngine.XMLDocument.firstChild != null)
						trace("mc2D parser error: incorrect document start tag["+this.xmlEngine.XMLDocument.firstChild.nodeName+"]");
					return null;
				}
				loadedMolecule = new CMolecule();
				var children=this.xmlEngine.XMLDocument.firstChild.childNodes;
				var len=children.length;
				for(var ii=0; ii<len; ii++)
				{
					var item=children.item(ii);
					if( item.nodeName == null)	continue;
					else	if( item.nodeName == "molecule")
					{
						trace("item.nodeName="+item.nodeName);
						if ( loadMolecule(item) == false)
						{
							trace("mc2D parser error: incorrect molecule structure");
							return null;
						}
					}
					else	if( item.nodeName == "info")
					{
						loadInfo(item);
					}
				}
				loadedMolecule.calculateDistances();
				return loadedMolecule;
			}

			// Загрузка данных молекулы из тега molecule
			function loadMolecule( node)
			{
				var name = node.getAttribute('name');
				if( name!="")
					this.loadedMolecule.name = name;
				var children=node.childNodes;
				if( children == null || typeof(children)=="undefined")
				{
					trace("mc2D parser error: no children in molecule tag");
					return false;
				}
				var len=children.length;
				if( len != 1)
				{
					trace("mc2D parser error: may be only one root in molecule");
					return false;
				}
				for(var ii=0; ii<len; ii++)
				{
					var child=children.item(ii);
					if( child.nodeName == "atom")
					{
						this.loadedMolecule.root = loadAtom( child);
						this.loadedMolecule.root.molecule = this.loadedMolecule;
					}
				}
				return true;
			}

			// Загрузка данных atoma из тега atom
			function loadAtom( node, parent)
			{
				var flipped = node.getAttribute('flipped');
				var index = node.getAttribute('index');
				if( index=="")
				{
					index = -1;
				}
				else
				{
					index = Number(index);
				}
				var type = node.getAttribute('type');
				if( type=="")
				{
					type = "C";
				}
				var link = node.getAttribute('link');
				if( link=="single" || link=="")
				{
					link = 1;
				}
				else	if( link=="double")
				{
					link = 2;
				}
				else	if( link=="troub")
				{
					link = 3;
				}
				else
				{
					trace("mc2D parser error: incorrect link type["+link+"]");
					return null;
				}

				var atom = null;
				if( CAtom.prototype.isCycleType(type))
				{
					atom = CCycle.prototype.create( type);
				}
				else	if( CAtom.prototype.isSpecialCycleType(type))
				{
					atom = CSpecCycle.prototype.create( type);
				}
				else
				{
					if( index == -1)
					{
						// это обычный атом
//trace("2");
						atom = CAtom.prototype.create( type);
					}
					else
					{
						// это атом цикла
//trace("3");
						atom = parent.cycleChildren[index];
					}
				}

				atom.linkType = link;
//trace("atom.linkType="+atom.linkType);
				atom.molecule = this.loadedMolecule;

				var children=node.childNodes;
				var len=0;
				if( children != null)
				{
					len = children.length;
				}

				var ii=0;
				for(; ii<len; ii++)
				{
					var child=children.item(ii);
					if( child.nodeName == "atom")
					{
						var childAtom = loadAtom( child, atom);
						if( childAtom != null)
						{
							if( !(childAtom instanceof CCycleAtom) && !(childAtom instanceof CSpecCycleAtom))
							{
								childAtom.parent = atom;
								atom.children.push(childAtom);
							}
							else
							{
//trace("childAtom.children.length="+childAtom.children.length);
								for(var i=0; i<childAtom.children.length; i++)
								{
									var child = childAtom.children[i];
									if( !(child instanceof CCycleAtom) && !(child instanceof CSpecCycleAtom))
									{
//trace("*********atom.children.push(child)************");
										atom.children.push(child);
									}
								}
							}
						}
					}
				}
				if( flipped!="" && flipped=="true")
					atom.isFlipped = true;
				//trace("Loaded atom:" + atom.type);
				return atom;
			}

			// Загрузка данных молекулы из тега info
			function loadInfo( node)
			{
			}
		]]></script>
	</composition>
</rmml>