// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   TutorialXmlProcessor.java

package com.ptc.windchill.enterprise.tutorials.server;

import java.io.*;
import java.util.HashMap;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import wt.httpgw.EncodingConverter;
import wt.util.WTException;
import wt.util.xml.jaxp.JAXPFactories;
import wt.util.xml.xpath.*;

public class TutorialXmlProcessor
{

    public TutorialXmlProcessor()
    {
    }

    public boolean buildDocument(File xmlFile, String schemaSource)
    {
        DocumentBuilderFactory dfactory;
        JAXPFactories jaxpFactories = new JAXPFactories();
        dfactory = jaxpFactories.getDocumentBuilderFactory();
        dfactory.setValidating(false);
        dfactory.setIgnoringComments(true);
        dfactory.setNamespaceAware(true);
        DocumentBuilder builder = dfactory.newDocumentBuilder();
        builder.setErrorHandler(new DefaultHandler());
        xmlDocument = builder.parse(xmlFile);
        return true;
        ParserConfigurationException pce;
        pce;
        break MISSING_BLOCK_LABEL_106;
        SAXException se;
        se;
        break MISSING_BLOCK_LABEL_106;
        IOException ie;
        ie;
        System.err.println("TutorialXmlProcessor: " + ie);
        break MISSING_BLOCK_LABEL_106;
        IllegalArgumentException x;
        x;
        return false;
    }

    public Vector processDocument(Document doc, String processDirectory)
    {
        Vector tempVector = new Vector();
        HashMap topicMap = new HashMap();
        NodeList mainTopicList = getMainTopics(doc);
        if(processDirectory != null && processDirectory.trim().length() > 0)
        {
            Node interopHeaderNode = getInteropMainTopics(doc).item(0);
            HashMap interop = new HashMap();
            if(interopHeaderNode != null)
            {
                interop = processInteropHeader(interopHeaderNode);
                interop.put("Level", processDirectory + "mainMenu");
                interop.put("Parent", "mainMenu");
                tempVector.add(interop);
            }
        }
        if(mainTopicList != null)
        {
            int i;
            for(i = 0; i < mainTopicList.getLength(); i++)
            {
                int mainLevel = i + tutorialLevels;
                HashMap main = processMainTopics(mainTopicList.item(i));
                if(main == null)
                    continue;
                HashMap mainMap = new HashMap();
                mainMap.put("Label", main.get("Label"));
                mainMap.put("Resource", main.get("Resource"));
                mainMap.put("Level", "sub" + mainLevel);
                tempVector.add(mainMap);
                Vector sub = (Vector)main.get("SubTopics");
                if(sub == null || sub.size() <= 0)
                    continue;
                for(int x = 0; x < sub.size(); x++)
                {
                    HashMap subTopic = processSubTopics((Node)sub.elementAt(x));
                    if(subTopic == null)
                        continue;
                    HashMap subMap = new HashMap();
                    subMap.put("Label", subTopic.get("Label"));
                    subMap.put("Resource", subTopic.get("Resource"));
                    subMap.put("Parent", "sub" + mainLevel);
                    subMap.put("Level", "sub" + mainLevel + "_" + x);
                    tempVector.add(subMap);
                    Vector finalTopics = (Vector)subTopic.get("FinalTopics");
                    if(finalTopics == null || finalTopics.size() <= 0)
                        continue;
                    for(int y = 0; y < finalTopics.size(); y++)
                    {
                        HashMap finalTopic = processFinalTopics((Node)finalTopics.elementAt(y));
                        if(finalTopic != null)
                        {
                            HashMap finalMap = new HashMap();
                            finalMap.put("Label", finalTopic.get("Label"));
                            finalMap.put("Resource", finalTopic.get("Resource"));
                            finalMap.put("Parent", "sub" + mainLevel + "_" + x);
                            finalMap.put("Level", "sub" + mainLevel + "_" + x + "_" + y);
                            tempVector.add(finalMap);
                        }
                    }

                }

            }

            tutorialLevels += i;
        }
        return tempVector;
    }

    private NodeList getMainTopics(Document doc)
    {
        NodeList list = getNodeListFromXpath(xmlDocument, "/Tutorials/MainTopic");
        return list;
    }

    private NodeList getInteropMainTopics(Document doc)
    {
        NodeList list = getNodeListFromXpath(xmlDocument, "/Tutorials/InteropMainTopic");
        return list;
    }

    private HashMap processMainTopics(Node mainNode)
    {
        HashMap tempMap = new HashMap();
        NodeList childNodes = mainNode.getChildNodes();
        Vector subTopics = new Vector();
        for(int i = 0; i < childNodes.getLength(); i++)
        {
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Label"))
            {
                tempMap.put("Label", getLabelValue(childNodes.item(i)).trim());
                continue;
            }
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Resource"))
            {
                tempMap.put("Resource", getResourceValue(childNodes.item(i)).trim());
                continue;
            }
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("SubTopic"))
                subTopics.add(childNodes.item(i));
        }

        tempMap.put("SubTopics", subTopics);
        return tempMap;
    }

    private HashMap processSubTopics(Node subNode)
    {
        HashMap tempMap = new HashMap();
        NodeList childNodes = subNode.getChildNodes();
        Vector finalTopics = new Vector();
        for(int i = 0; i < childNodes.getLength(); i++)
        {
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Label") && getLabelValue(childNodes.item(i)).trim() != null)
            {
                tempMap.put("Label", getLabelValue(childNodes.item(i)).trim());
                continue;
            }
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Resource") && getResourceValue(childNodes.item(i)) != null)
            {
                tempMap.put("Resource", getResourceValue(childNodes.item(i)).trim());
                continue;
            }
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("FinalTopic"))
                finalTopics.add(childNodes.item(i));
        }

        tempMap.put("FinalTopics", finalTopics);
        return tempMap;
    }

    private HashMap processFinalTopics(Node finalNode)
    {
        HashMap tempMap = new HashMap();
        NodeList childNodes = finalNode.getChildNodes();
        String tempLabel = null;
        String tempResource = null;
        for(int i = 0; i < childNodes.getLength(); i++)
        {
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Label"))
            {
                tempLabel = getLabelValue(childNodes.item(i)).trim();
                continue;
            }
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Resource") && getResourceValue(childNodes.item(i)) != null)
                tempResource = getResourceValue(childNodes.item(i)).trim();
        }

        if(tempLabel != null && tempResource != null)
        {
            tempMap.put("Label", tempLabel);
            tempMap.put("Resource", tempResource);
        } else
        {
            System.err.println(" The value for an entry in the xml file was invalid and skipped");
        }
        return tempMap;
    }

    private HashMap processInteropHeader(Node headerNode)
    {
        HashMap tempMap = new HashMap();
        NodeList childNodes = headerNode.getChildNodes();
        Vector subTopics = new Vector();
        for(int i = 0; i < childNodes.getLength(); i++)
            if((childNodes.item(i) instanceof Element) && childNodes.item(i).getNodeName().equals("Label"))
                tempMap.put("Label", getLabelValue(childNodes.item(i)).trim());

        return tempMap;
    }

    private String getResourceValue(Node node)
    {
        Node n = null;
        NodeList kids = null;
        if((node instanceof Element) && node.getNodeName().equals("Resource"))
        {
            kids = node.getChildNodes();
            n = kids.item(2);
        }
        if(n != null && n.getNodeValue() != null)
            return n.getNodeValue();
        n = kids.item(0);
        if(n != null && n.getNodeValue() != null)
            return n.getNodeValue();
        else
            return "";
    }

    private String getLabelValue(Node node)
    {
        Node n = null;
        NodeList kids = null;
        if((node instanceof Element) && node.getNodeName().equals("Label"))
        {
            kids = node.getChildNodes();
            n = kids.item(2);
        }
        if(n != null && n.getNodeValue() != null)
            return EncodingConverter.unicodeToAscii(n.getNodeValue());
        n = kids.item(0);
        if(n != null && n.getNodeValue() != null)
            return EncodingConverter.unicodeToAscii(n.getNodeValue());
        else
            return "";
    }

    private NodeList getNodeListFromXpath(Document doc, String xpath)
    {
        NodeListCallback mapping_callback = new NodeListCallback();
        try
        {
            XPathFactory factory_ = XPathFactory.createNewFactoryInstance();
            XPath x_path = factory_.newXPath(xpath);
            x_path.evaluate(doc, mapping_callback);
        }
        catch(WTException wte)
        {
            wte.printStackTrace();
        }
        return mapping_callback.getNodeList();
    }

    public Document getMainDocument()
    {
        return xmlDocument;
    }

    private Document xmlDocument;
    private static final boolean DEBUG = false;
    private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
    private final String mainXpath = "/Tutorials/MainTopic";
    private final String interopXpath = "/Tutorials/InteropMainTopic";
    private static int tutorialLevels;
}
