Beispiel #1
0
        /// <summary>
        /// Imports the first graph not being of type "gxl-1.0" from the given text reader input stream.
        /// Any errors will be reported by exception.
        /// </summary>
        /// <param name="inStream">The text reader input stream import source.</param>
        /// <param name="backend">The backend to use to create the graph.</param>
        /// <param name="graphModel">The graph model to be used,
        ///     it must be conformant to the model used in the file to be imported.</param>
        /// <param name="actions">Receives the actions object in case a .grg model is given.</param>
        public static IGraph Import(TextReader inStream, IBackend backend, IGraphModel graphModel, out IActions actions)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(inStream);

            XmlElement gxlelem = doc["gxl"];

            if (gxlelem == null)
            {
                throw new Exception("The document has no gxl element.");
            }

            XmlElement graphelem = null;
            String     graphtype = null;

            foreach (XmlElement curgraphnode in gxlelem.GetElementsByTagName("graph"))
            {
                graphtype = GetTypeName(curgraphnode);
                if (!graphtype.EndsWith("gxl-1.0.gxl#gxl-1.0"))
                {
                    if (graphelem != null)
                    {
                        throw new Exception("More than one instance graph included (not yet supported)!");
                    }
                    graphelem = curgraphnode;
                }
            }
            if (graphelem == null)
            {
                throw new Exception("No non-meta graph found!");
            }

            String graphname = graphelem.GetAttribute("id");
            IGraph graph     = backend.CreateGraph(graphModel, graphname);

            ImportGraph(graph, doc, graphelem);

            actions = null;
            return(graph);
        }
Beispiel #2
0
        /// <summary>
        /// Imports the first graph not being of type "gxl-1.0" from the given text reader input stream.
        /// Any errors will be reported by exception.
        /// </summary>
        /// <param name="inStream">The text reader input stream import source.</param>
        /// <param name="modelOverride">If not null, overrides the filename of the graph model to be used.</param>
        /// <param name="backend">The backend to use to create the graph.</param>
        /// <param name="actions">Receives the actions object in case a .grg model is given.</param>
        public static IGraph Import(TextReader inStream, String modelOverride, IBackend backend, out IActions actions)
        {
            XmlDocument doc = new XmlDocument();

            doc.XmlResolver = null;
            doc.Load(inStream);

            XmlElement gxlelem = doc["gxl"];

            if (gxlelem == null)
            {
                throw new Exception("The document has no gxl element.");
            }

            XmlElement graphelem = null;
            String     graphtype = null;

            foreach (XmlElement curgraphnode in gxlelem.GetElementsByTagName("graph"))
            {
                graphtype = GetTypeName(curgraphnode);
                if (!graphtype.EndsWith("gxl-1.0.gxl#gxl-1.0"))
                {
                    if (graphelem != null)
                    {
                        throw new Exception("More than one instance graph included (not yet supported)!");
                    }
                    graphelem = curgraphnode;
                }
            }
            if (graphelem == null)
            {
                throw new Exception("No non-meta graph found!");
            }

            String modelfilename;

            if (modelOverride == null)
            {
                XmlDocument modeldoc;
                int         hashindex = graphtype.IndexOf('#');
                if (hashindex <= 0)
                {
                    modeldoc = doc;
                }
                else
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ProhibitDtd = false;
                    XmlReader reader = XmlReader.Create(graphtype.Substring(0, hashindex), settings);
                    modeldoc = new XmlDocument();
                    modeldoc.Load(reader);
                }

                String     localname = graphtype.Substring(hashindex + 1);
                XmlElement modelnode = (XmlElement)modeldoc.SelectSingleNode(
                    "descendant::graph/node[@id='" + localname + "']");
                if (modelnode == null)
                {
                    throw new Exception("Graph schema \"" + graphtype + "\" not found.");
                }
                if (!GetTypeName(modelnode).EndsWith("gxl-1.0.gxl#GraphClass"))
                {
                    throw new Exception("Graph type \"" + graphtype + "\" must refer to a GraphClass node.");
                }

                String modelname = GetGXLAttr(modelnode, "name", "string");
                if (modelname.EndsWith("__gxl"))
                {
                    modelname = modelname.Substring(0, modelname.Length - 5);
                }
                XmlElement modelgraph = (XmlElement)modelnode.ParentNode;
                modelfilename = ImportModel(modelgraph, modelname);
            }
            else
            {
                modelfilename = modelOverride;
            }

            IGraph graph;
            String graphname = graphelem.GetAttribute("id");

            if (modelfilename.EndsWith(".grg"))
            {
                backend.CreateFromSpec(modelfilename, graphname, null,
                                       ProcessSpecFlags.UseNoExistingFiles, new List <String>(),
                                       out graph, out actions);
            }
            else
            {
                graph   = backend.CreateGraph(modelfilename, graphname);
                actions = null;
            }

            ImportGraph(graph, doc, graphelem);

            return(graph);
        }