Example usage for org.apache.commons.vfs FileObject getClass

List of usage examples for org.apache.commons.vfs FileObject getClass

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.pentaho.di.trans.steps.xsdvalidator.XsdValidator.java

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (XsdValidatorMeta) smi;/*from  ww w.j a v  a 2  s .  c  o m*/
    data = (XsdValidatorData) sdi;

    Object[] row = getRow();

    if (row == null) { // no more input to be expected...

        setOutputDone();
        return false;
    }

    if (first) {
        first = false;
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);

        // Check if XML stream is given
        if (meta.getXMLStream() != null) {
            // Try to get XML Field index
            data.xmlindex = getInputRowMeta().indexOfValue(meta.getXMLStream());
            // Let's check the Field
            if (data.xmlindex < 0) {
                // The field is unreachable !
                logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorFindingField") + "["
                        + meta.getXMLStream() + "]");
                throw new KettleStepException(BaseMessages.getString(PKG,
                        "XsdValidator.Exception.CouldnotFindField", meta.getXMLStream()));
            }

            // Let's check that Result Field is given
            if (meta.getResultfieldname() == null) {
                // Result field is missing !
                logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorResultFieldMissing"));
                throw new KettleStepException(
                        BaseMessages.getString(PKG, "XsdValidator.Exception.ErrorResultFieldMissing"));
            }

            // Is XSD file is provided?
            if (meta.getXSDSource().equals(meta.SPECIFY_FILENAME)) {
                if (meta.getXSDFilename() == null) {
                    logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorXSDFileMissing"));
                    throw new KettleStepException(
                            BaseMessages.getString(PKG, "XsdValidator.Exception.ErrorXSDFileMissing"));
                } else {
                    // Is XSD file exists ?
                    FileObject xsdfile = null;
                    try {
                        xsdfile = KettleVFS.getFileObject(environmentSubstitute(meta.getXSDFilename()),
                                getTransMeta());
                        if (!xsdfile.exists()) {
                            logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XSDFileNotExists"));
                            throw new KettleStepException(
                                    BaseMessages.getString(PKG, "XsdValidator.Exception.XSDFileNotExists"));
                        }

                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.GettingXSDFile"));
                        throw new KettleStepException(
                                BaseMessages.getString(PKG, "XsdValidator.Exception.GettingXSDFile"));
                    } finally {
                        try {
                            if (xsdfile != null) {
                                xsdfile.close();
                            }
                        } catch (IOException e) {
                            // Ignore errors
                        }
                    }
                }
            }

            // Is XSD field is provided?
            if (meta.getXSDSource().equals(meta.SPECIFY_FIELDNAME)) {
                if (meta.getXSDDefinedField() == null) {
                    logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XSDFieldMissing"));
                    throw new KettleStepException(
                            BaseMessages.getString(PKG, "XsdValidator.Exception.XSDFieldMissing"));
                } else {
                    // Let's check if the XSD field exist
                    // Try to get XML Field index
                    data.xsdindex = getInputRowMeta().indexOfValue(meta.getXSDDefinedField());

                    if (data.xsdindex < 0) {
                        // The field is unreachable !
                        logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorFindingXSDField",
                                meta.getXSDDefinedField()));
                        throw new KettleStepException(BaseMessages.getString(PKG,
                                "XsdValidator.Exception.ErrorFindingXSDField", meta.getXSDDefinedField()));
                    }
                }
            }

        } else {
            // XML stream field is missing !
            logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XmlStreamFieldMissing"));
            throw new KettleStepException(
                    BaseMessages.getString(PKG, "XsdValidator.Exception.XmlStreamFieldMissing"));
        }
    }

    try {

        // Get the XML field value
        String XMLFieldvalue = getInputRowMeta().getString(row, data.xmlindex);

        boolean isvalid = false;

        // XSD filename
        String xsdfilename = null;

        if (meta.getXSDSource().equals(meta.SPECIFY_FILENAME)) {
            xsdfilename = environmentSubstitute(meta.getXSDFilename());
        } else if (meta.getXSDSource().equals(meta.SPECIFY_FIELDNAME)) {
            // Get the XSD field value
            xsdfilename = getInputRowMeta().getString(row, data.xsdindex);
        }

        // Get XSD filename
        FileObject xsdfile = null;
        String validationmsg = null;
        try {

            SchemaFactory factoryXSDValidator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            xsdfile = KettleVFS.getFileObject(xsdfilename, getTransMeta());

            // Get XML stream
            Source sourceXML = new StreamSource(new StringReader(XMLFieldvalue));

            if (meta.getXMLSourceFile()) {

                // We deal with XML file
                // Get XML File
                File xmlfileValidator = new File(XMLFieldvalue);
                if (!xmlfileValidator.exists()) {
                    logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XMLfileMissing",
                            XMLFieldvalue));
                    throw new KettleStepException(BaseMessages.getString(PKG,
                            "XsdValidator.Exception.XMLfileMissing", XMLFieldvalue));
                }
                sourceXML = new StreamSource(xmlfileValidator);
            }

            // create the schema
            Schema SchematXSD = null;
            if (xsdfile instanceof LocalFile) {
                SchematXSD = factoryXSDValidator.newSchema(new File(KettleVFS.getFilename(xsdfile)));
            } else if (xsdfile instanceof HttpFileObject) {
                SchematXSD = factoryXSDValidator.newSchema(new URL(KettleVFS.getFilename(xsdfile)));
            } else {
                // we should not get here as anything entered in that does not look like
                // a url should be made a FileObject.
                throw new KettleStepException(BaseMessages.getString(PKG,
                        "XsdValidator.Exception.CannotCreateSchema", xsdfile.getClass().getName()));
            }

            if (meta.getXSDSource().equals(meta.NO_NEED)) {
                // ---Some documents specify the schema they expect to be validated against,
                // ---typically using xsi:noNamespaceSchemaLocation and/or xsi:schemaLocation attributes
                // ---Schema SchematXSD = factoryXSDValidator.newSchema();
                SchematXSD = factoryXSDValidator.newSchema();
            }

            // Create XSDValidator
            Validator XSDValidator = SchematXSD.newValidator();
            // Validate XML / XSD
            XSDValidator.validate(sourceXML);

            isvalid = true;

        } catch (SAXException ex) {
            validationmsg = ex.getMessage();
        } catch (IOException ex) {
            validationmsg = ex.getMessage();
        } finally {
            try {
                if (xsdfile != null) {
                    xsdfile.close();
                }
            } catch (IOException e) {
                // Ignore errors
            }
        }

        Object[] outputRowData = null;
        Object[] outputRowData2 = null;

        if (meta.getOutputStringField()) {
            // Output type=String
            if (isvalid) {
                outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(),
                        environmentSubstitute(meta.getIfXmlValid()));
            } else {
                outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(),
                        environmentSubstitute(meta.getIfXmlInvalid()));
            }
        } else {
            outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(), isvalid);
        }

        if (meta.useAddValidationMessage()) {
            outputRowData2 = RowDataUtil.addValueData(outputRowData, getInputRowMeta().size() + 1,
                    validationmsg);
        } else {
            outputRowData2 = outputRowData;
        }

        if (log.isRowLevel()) {
            logRowlevel(BaseMessages.getString(PKG, "XsdValidator.Log.ReadRow") + " "
                    + getInputRowMeta().getString(row));
        }

        // add new values to the row.
        putRow(data.outputRowMeta, outputRowData2); // copy row to output rowset(s);
    } catch (KettleException e) {
        boolean sendToErrorRow = false;
        String errorMessage = null;

        if (getStepMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        }

        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), row, 1, errorMessage, null, "XSD001");
        } else {
            logError(BaseMessages.getString(PKG, "XsdValidator.ErrorProcesing" + " : " + e.getMessage()));
            throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.ErrorProcesing"), e);
        }
    }

    return true;

}

From source file:org.sonatype.gshell.commands.vfs.EditCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;

    FileObject file = resolveFile(context, path);

    new FileObjectAssert(file).exists().isReadable().isWritable();

    // TODO: Add to FileObjectAssert
    ensureFileHasContent(file);/* w w  w .j  a  v  a 2  s . c  o m*/

    FileObject tmp = file;

    // If the file is not on the local file system, then create tmp file for editing
    if (!getFileSystemAccess().isLocalFile(file)) {
        // Create a new temporary file, copy the contents for editing
        tmp = resolveFile(context, "tmp:/gshell-edit-" + System.currentTimeMillis() + ".txt");
        log.debug("Using temporary file: {} ({})", tmp, tmp.getClass());
        tmp.createFile();
        tmp.copyFrom(file, Selectors.SELECT_SELF);
    }

    // Have to dereference the VFS file into a local file so the editor can access it
    File localFile = getFileSystemAccess().getLocalFile(tmp);
    Object result = edit(context, localFile);

    // If we had to use a tmp file for editing, then copy back and clean up
    if (tmp != file) {
        log.debug("Updating original file with edited content");
        file.copyFrom(tmp, Selectors.SELECT_SELF);
        tmp.delete();
        FileObjects.close(tmp);
    }

    FileObjects.close(file);

    return result;
}

From source file:org.sonatype.gshell.vfs.FileSystemAccessImpl.java

public File getLocalFile(final FileObject file) throws FileSystemException {
    if (!isLocalFile(file)) {
        throw new FileSystemException("Unable to get local file from: " + file.getClass());
    }//from   w  w w . jav  a  2  s . co m

    try {
        file.refresh();
        Field field = LocalFile.class.getDeclaredField("file");

        try {
            return (File) field.get(file);
        } catch (IllegalAccessException ignore) {
            // try again
            field.setAccessible(true);
            return (File) field.get(file);
        }
    } catch (Exception e) {
        throw new FileSystemException(e);
    }
}