#!ipy

"""
++++++++
 docbook.py
++++++++

  author: Jesse Wiles
  copyright: Quadralay Corporation, 2007.
  
  Handler for application/docbook+xml content type.
"""

# system imports
from System import DateTime
from System.Diagnostics import Process, ProcessWindowStyle
from System.IO import File, Path

# XSL ref/imports
import clr
clr.AddReference('System.Xml')

from System.Xml import XmlReader, XmlReaderSettings, XmlWriter, XmlWriterSettings
from System.Xml.Xsl import XslCompiledTransform

# local imports
from abstracts import PyToolAdapter
import utils

class Docbook(PyToolAdapter):
    def duplicate(self):
        if self._env.uri.Scheme == 'http':
            data = utils.get_web_uri(self._env.uri)

            fp = open(self._env.dupDocPath, 'wb')
            fp.write(data)
            fp.close()
            
        elif self._env.uri.Scheme == 'file':
            if self._env.uri.IsFile:
                File.Copy(self._env.uri.LocalPath, self._env.dupDocPath)
                
        else:
            raise "ERROR: '%s' is not a supported URI scheme." % self._env.uri.Scheme

    def xml(self):
        xslpath = Path.Combine(self._env.xslDir, 'docbook2wif.xsl')

        readerSettings = XmlReaderSettings()
        readerSettings.ProhibitDtd = False

        reader = XmlReader.Create(self._env.docPath, readerSettings)

        writerSettings = XmlWriterSettings()
        writerSettings.Indent = True
        writerSettings.IndentChars = ' '

        writer = XmlWriter.Create(self._env.xmlPath, writerSettings)

        transform = XslCompiledTransform()
        transform.Load(xslpath)
        try:
            transform.Transform(reader, writer)

        finally:
            reader.Close()
            writer.Close()

        # touch original doc path
        File.SetLastWriteTime(self._env.originalDocPath, DateTime.Now)

    def scan_doc(self):
        self._env.paras['{Heading Styles}'] = ''
        self._env.paras['db-section'] = '{Heading Styles}'
        self._env.paras['db-section-1'] = 'db-section'
        self._env.paras['db-section-2'] = 'db-section'
        self._env.paras['db-section-3'] = 'db-section'
        self._env.paras['db-section-4'] = 'db-section'
        self._env.paras['db-section-5'] = 'db-section'

        self._env.paras['{Paragraph Styles}'] = ''
        self._env.paras['db-para'] = '{Paragraph Styles}'

        self._env.paras['{Indented Styles}'] = ''
        self._env.paras['db-para-indent'] = '{Indented Styles}'
        self._env.paras['db-para-indent-1'] = 'db-para-indent'
        self._env.paras['db-para-indent-2'] = 'db-para-indent'
        self._env.paras['db-para-indent-3'] = 'db-para-indent'

        self._env.paras['{Numbered Styles}'] = ''
        self._env.paras['db-para-numbered'] = '{Numbered Styles}'
        self._env.paras['db-para-numbered-1'] = 'db-para-numbered'
        self._env.paras['db-para-numbered-2'] = 'db-para-numbered'

        self._env.paras['{Bulleted Styles}'] = ''
        self._env.paras['db-para-bulleted'] = '{Bulleted Styles}'
        self._env.paras['db-para-bulleted-1'] = 'db-para-bulleted'
        self._env.paras['db-para-bulleted-2'] = 'db-para-bulleted'
        self._env.paras['db-para-bulleted-3'] = 'db-para-bulleted'

        self._env.paras['{Literal Styles}'] = ''
        self._env.paras['db-screen'] = '{Literal Styles}'

        self._env.paras['{Table Styles}'] = ''
        self._env.paras['db-table'] = '{Table Styles}'
        self._env.paras['db-para-cell-head'] = 'db-table'
        self._env.paras['db-para-cell-body'] = 'db-table'
        self._env.paras['db-para-cell-foot'] = 'db-table'

        self._env.charas['db-emphasis-strong'] = ''
        self._env.charas['db-code'] = ''

        self._env.tables['docbook1'] = ''