Ejemplo n.º 1
0
        public ExternalReferenceContent <T> BuildContent <T>(string filename,
                                                             out object content,
                                                             string importerName = null,
                                                             Dictionary <string, object> importerData = null,
                                                             string processorName = null,
                                                             Dictionary <string, object> processorData = null,
                                                             string writerName = null,
                                                             Dictionary <string, object> writerData = null,
                                                             bool ignoreBuildItem = false)
        {
            if (IsBuiltin(filename))
            {
                content = null;
                string outputname = _buildOptions.GetOutputFilename(typeof(T), filename);
                return(new ExternalReferenceContent <T>(filename));
            }

            if (!Path.IsPathRooted(filename))
            {
                filename = _processorContext.GetFilenamePath(filename);
            }

            if (!ignoreBuildItem && _buildSource.HasBuildItem(filename))
            {
                // if there is a build item for this item leave it up to the build source to generate the actual build, and just return a ExternalReferenceContent item.
                content = null;
                string outputname = _buildOptions.GetOutputFilename(typeof(T), filename);
                outputname = _contentSaver.GetPath(outputname, typeof(T));
                return(new ExternalReferenceContent <T>(outputname));
            }

            // first find out the output type of the processor
            IContentImporter importer;

            if ((importer = CreateImporter(importerName ?? FindImporterByExtension(Path.GetExtension(filename)), importerData ?? new Dictionary <string, object>())) == null)
            {
                throw new ContentLoadException(importerName != null ?
                                               string.Format("Importer {0} not found.", importerName) :
                                               string.Format("Importer not found that handles extension {0}", Path.GetExtension(filename)));
            }
            IContentProcessor processor;

            if ((processor = CreateProcessor(processorName ?? FindDefaultProcessor(importer.GetType()), processorData ?? new Dictionary <string, object>())) == null)
            {
                throw new ContentLoadException(processorName != null ?
                                               string.Format("Processor {0} not found.", processorName) :
                                               string.Format("Processor not found that handles type {0}", importer.GetType().Name));
            }

            _processorContext.PushDirectory(Path.GetDirectoryName(filename));

            using (FileStream stream = File.OpenRead(filename))
            {
                content = importer.Import(stream, this);
            }

            content = processor.Process(content, _processorContext);

            _processorContext.PopDirectory();

            object outName;

            if (!(content is ContentItem) || !((ContentItem)content).OpaqueData.TryGetValue("OutputFileName", out outName))
            {
                outName = filename;
            }
            string outputFilename = _buildOptions.GetOutputFilename(content.GetType(), (string)outName);

            if (!_buildOptions.ForceRebuild && _contentSaver.GetLastModified(outputFilename, content.GetType()) > File.GetLastWriteTime(filename))
            {
                return(new ExternalReferenceContent <T>(_contentSaver.GetPath(outputFilename, processor.OutputType)));
            }

            ContentTypeWriter typeWriter = GetTypeWriter(writerName, content.GetType(), writerData);

            if (typeWriter == null && (typeWriter = GetTypeWriter(content.GetType(), writerData ?? new Dictionary <string, object>())) == null)
            {
                throw new ContentLoadException(string.Format("ContentTypeWriter not found for content type {0}", content.GetType()));
            }

            if (_typeWriterManager == null)
            {
                _typeWriterManager = new ContentTypeWriterManager();
            }
            using (MemoryStream stream = new MemoryStream())
            {
                ContentWriter writer = CreateWriter(_typeWriterManager, stream, _buildOptions.CompressOutput, _buildOptions.GetIdentifierString(content.GetType()));
                writer.WriteObject(content, typeWriter);
                writer.Flush();

                string path = _contentSaver.Save(stream, outputFilename, content.GetType());
                return(new ExternalReferenceContent <T>(path));
            }
        }
Ejemplo n.º 2
0
 public abstract void Write(ContentWriter writer, object value);