MetaDataHandlerV2.java :  » J2EE » enhydra-IDE-plugin » org » enhydra » kelp » common » bridge » Java Open Source

Java Open Source » J2EE » enhydra IDE plugin 
enhydra IDE plugin » org » enhydra » kelp » common » bridge » MetaDataHandlerV2.java
/*
 * Enhydra Java Application Server Project
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific terms governing rights and limitations
 * under the License.
 *
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 *
 * Contributor(s):
 *
 */

package org.enhydra.kelp.common.bridge;

// XMLC imports
import org.enhydra.xml.xmlc.XMLCException;
import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions;
import org.enhydra.xml.xmlc.metadata.DeleteElement;
import org.enhydra.xml.xmlc.metadata.CompileOptions;
import org.enhydra.xml.xmlc.metadata.DocumentClass;
import org.enhydra.xml.xmlc.metadata.DOMEdits;
import org.enhydra.xml.xmlc.metadata.MetaData;
import org.enhydra.xml.xmlc.metadata.MetaDataDocument;

// ToolBox imports
import org.enhydra.tool.common.PathHandle;

// Kelp imports
import org.enhydra.kelp.common.Constants;
import org.enhydra.kelp.common.node.OtterXMLCNode;

// Standard imports
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;

//
public class MetaDataHandlerV2 implements MetaDataHandler {

    // strings not to be resourced
    private final String[] CUSTOM_INPUT_DOC_TYPES = {
        "chtml", "htm", "html", "xhtml"
    }; // nores
    private final String FILE_PROTOCOL = "file:"; // nores

    //
    private MetaData       metaData = null;

    //
    public MetaDataHandlerV2() {
        MetaDataDocument doc = new MetaDataDocument();

        metaData = doc.getMetaData();
        metaData.setCompileOptions(new CompileOptions(doc));
        metaData.setDocumentClass(new DocumentClass(doc));
        metaData.setDOMEdits(new DOMEdits(doc));
    }

    // Compiler Options
    public boolean getCompileSource() {
        return metaData.getCompileOptions().getCompileSource();
    }

    public void setCompileSource(boolean b) {
        setCompileSource(metaData, b);
    }

    private void setCompileSource(MetaData meta, boolean b) {
        meta.getCompileOptions().setCompileSource(b);
    }

    public String getDocumentOutput() {
        return PathHandle.createPathString(metaData.getCompileOptions().getDocumentOutput());
    }

    public void setDocumentOutput(String s) {
        setDocumentOutput(metaData, s);
    }

    private void setDocumentOutput(MetaData meta, String s) {
        String path = PathHandle.createPathString(s);

        meta.getCompileOptions().setDocumentOutput(path);
    }

    public String getInputDocument() {
        String doc = null;

        doc = metaData.getCompileOptions().getInputDocument();
        if (doc.startsWith(FILE_PROTOCOL)) {
            doc = doc.substring(FILE_PROTOCOL.length());
        }
        return PathHandle.createPathString(doc);
    }

    public void setInputDocument(String s) {
        setInputDocument(metaData, s);
    }

    private void setInputDocument(MetaData meta, String s) {
        PathHandle   handle = null;
        StringBuffer buf = new StringBuffer();

        handle = PathHandle.createPathHandle(s);
        if (!handle.hasExtension(CUSTOM_INPUT_DOC_TYPES)) {
            buf.append(FILE_PROTOCOL);
        }
        buf.append(handle.getPath());
        meta.getCompileOptions().setInputDocument(buf.toString());
    }

    public boolean getKeepGeneratedSource() {
        return metaData.getCompileOptions().getKeepGeneratedSource();
    }

    public void setKeepGeneratedSource(boolean b) {
        setKeepGeneratedSource(metaData, b);
    }

    private void setKeepGeneratedSource(MetaData meta, boolean b) {
        meta.getCompileOptions().setKeepGeneratedSource(b);
    }

    public boolean getPrintAccessorInfo() {
        return metaData.getCompileOptions().getPrintAccessorInfo();
    }

    public void setPrintAccessorInfo(boolean b) {
        setPrintAccessorInfo(metaData, b);
    }

    private void setPrintAccessorInfo(MetaData meta, boolean b) {
        meta.getCompileOptions().setPrintAccessorInfo(b);
    }

    public boolean getPrintDocumentInfo() {
        return metaData.getCompileOptions().getPrintDocumentInfo();
    }

    public void setPrintDocumentInfo(boolean b) {
        setPrintDocumentInfo(metaData, b);
    }

    private void setPrintDocumentInfo(MetaData meta, boolean b) {
        meta.getCompileOptions().setPrintDocumentInfo(b);
    }

    public boolean getPrintDOM() {
        return metaData.getCompileOptions().getPrintDOM();
    }

    public void setPrintDOM(boolean b) {
        setPrintDOM(metaData, b);
    }

    private void setPrintDOM(MetaData meta, boolean b) {
        meta.getCompileOptions().setPrintDOM(b);
    }

    public boolean getPrintParseInfo() {
        return metaData.getCompileOptions().getPrintParseInfo();
    }

    public void setPrintParseInfo(boolean b) {
        setPrintParseInfo(metaData, b);
    }

    private void setPrintParseInfo(MetaData meta, boolean b) {
        meta.getCompileOptions().setPrintParseInfo(b);
    }

    public boolean getPrintVersion() {
        return metaData.getCompileOptions().getPrintVersion();
    }

    public void setPrintVersion(boolean b) {
        setPrintVersion(metaData, b);
    }

    private void setPrintVersion(MetaData meta, boolean b) {
        meta.getCompileOptions().setPrintVersion(b);
    }

    public boolean getVerbose() {
        return metaData.getCompileOptions().getVerbose();
    }

    public void setVerbose(boolean b) {
        setVerbose(metaData, b);
    }

    private void setVerbose(MetaData meta, boolean b) {
        meta.getCompileOptions().setVerbose(b);
    }

    // Document Class
    public String getClassName() {
        return metaData.getDocumentClass().getName();
    }

    public void setClassName(String n) {
        setClassName(metaData, n);
    }

    private void setClassName(MetaData meta, String n) {
        meta.getDocumentClass().setName(n);
    }

    public File getJavaClassSource() {
        return metaData.getDocumentClass().getJavaClassSource();
    }

    public void setJavaClassSource(File f, OtterXMLCNode node) {
        setJavaClassSource(metaData, f, node);
    }

    private void setJavaClassSource(MetaData meta, File f,
                                    OtterXMLCNode node) {
        String           outputRoot = node.getGenerateToRoot();
        String           path = f.getAbsolutePath();
        MetaDataDocument doc = meta.getDocument();

        outputRoot = outputRoot.replace('\\', '/').trim();
        path = path.replace('\\', '/').trim();
        String name = pathToClass(outputRoot, path);

        meta.getDocumentClass().setName(name);
        meta.getCompileOptions().setClassOutputRoot(outputRoot);
        meta.getCompileOptions().setSourceOutputRoot(outputRoot);
        try {
            doc.completeModifications();
        } catch (XMLCException e) {
            e.printStackTrace();
        }
    }

    public File getJavaInterfaceSource() {
        return metaData.getDocumentClass().getJavaInterfaceSource();
    }

    public void setJavaInterfaceSource(File f, OtterXMLCNode node) {
        setJavaInterfaceSource(metaData);
    }

    private void setJavaInterfaceSource(MetaData meta) {

        // the rest should be automatic.
        try {
            meta.getDocument().completeModifications();
        } catch (XMLCException e) {
            e.printStackTrace();
        }
    }

    public String getPackageName() {
        String name = null;

        try {
            name = metaData.getDocumentClass().getPackageName();
        } catch (NullPointerException e) {

            // Workaround?
            name = null;
        }
        return name;
    }

    public boolean getRecompilation() {
        return metaData.getDocumentClass().getRecompilation();
    }

    public void setRecompilation(boolean b) {
        setRecompilation(metaData, b);
    }

    private void setRecompilation(MetaData meta, boolean b) {
        meta.getDocumentClass().setRecompilation(b);
    }

    // DOM Edits
    public Object[] getDeleteElements() {
        return metaData.getDOMEdits().getDeleteElements();
    }

    private void mergeDeleteElements(MetaData meta,
                                     DeleteElement[] elements) {
        List eList = Arrays.asList(meta.getDOMEdits().getDeleteElements());

        for (int i = 0; i < elements.length; i++) {
            if (!eList.contains(elements[i])) {
                meta.getDOMEdits().addDeleteElement(elements[i]);
            }
        }
    }

    public Object[] getURLMappings() {
        return metaData.getDOMEdits().getURLMappings();
    }

    // Other
    public Object getMetaData() {
        return metaData;
    }

    public void parse(String[] files, String[] args, PrintWriter writer,
                      OtterXMLCNode node) throws XMLCException, IOException {
        XMLCOptions opParser = null;
        Reporter    reporter = null;
        String[]    parseArgs = new String[files.length + args.length + 1];
        MetaData    newMeta = null;

        reporter = new Reporter(writer);
        for (int i = 0; i < args.length; i++) {
            parseArgs[i] = args[i];
        }
        for (int i = args.length; i < (args.length + files.length); i++) {
            parseArgs[i] = files[(i - args.length)];
        }
        metaData.getDocument().completeModifications();
        if (parseArgs.length > 1) {
            parseArgs[parseArgs.length - 1] = node.getFilePath().replace('\\',
                    '/');
            try {
                opParser = new XMLCOptions();
                newMeta = opParser.parse(parseArgs, reporter);
                mergeMetaData(newMeta, node);
            } catch (Exception e) {
                e.printStackTrace();
                if (e instanceof XMLCException) {
                    throw (XMLCException) e;
                } else if (e instanceof IOException) {
                    throw (IOException) e;
                } else {
                    throw new XMLCException(e);
                }
            }
        }
    }

    private void mergeMetaData(MetaData newMeta, OtterXMLCNode node) {

        // merge group 1
        setKeepGeneratedSource(newMeta, getKeepGeneratedSource());
        setPrintAccessorInfo(newMeta, getPrintAccessorInfo());
        setPrintDOM(newMeta, getPrintDOM());
        setPrintParseInfo(newMeta, getPrintParseInfo());
        setPrintVersion(newMeta, getPrintVersion());
        setRecompilation(newMeta, getRecompilation());
        setVerbose(newMeta, getVerbose());

        // merge group 2
        setClassName(newMeta, getClassName());
        setCompileSource(newMeta, getCompileSource());
        mergeDeleteElements(newMeta, (DeleteElement[]) getDeleteElements());
        setDocumentOutput(newMeta, getDocumentOutput());
        setInputDocument(newMeta, getInputDocument().replace('\\', '/'));
        setJavaClassSource(newMeta, getJavaClassSource(), node);
        setJavaInterfaceSource(newMeta);
        this.metaData = newMeta;
    }

    public void save(File op) throws IOException {
      try {
        metaData.getDocument().serialize(op);
      } catch (XMLCException e) {
            e.printStackTrace();
        }
    }

    // Private Stuff
    private String pathToClass(String outputRoot, String in) {
        String out = in;
        int    index = 0;

        if (out.startsWith(outputRoot)) {
            out = out.substring(outputRoot.length());
        }
        index = out.lastIndexOf('.' + Constants.TYPE_JAVA);
        if (index > -1) {
            out = out.substring(0, index);
        }
        out = out.replace('\\', '.');
        out = out.replace('/', '.');
        while ((out.length() > 0) && (out.charAt(0) == '.')) {
            out = out.substring(1);
        }
        while ((out.length() > 0) && (out.charAt(out.length()-1) == '.')) {
            out = out.substring(0, out.length() - 1);
        }
        return out;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.