Package org.apache.torque.generator.source

Examples of org.apache.torque.generator.source.SourceElement


    {
        if (log.isDebugEnabled())
        {
            log.debug("start creating root Element");
        }
        SourceElement result;
        InputStream inputStream = null;
        try
        {
            inputStream = new FileInputStream(path);
            log.debug("Reading file "
View Full Code Here


    @Override
    public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
            throws SAXException
    {
        SourceElement current = new SourceElement(qName);
        for (int i = 0; i < attributes.getLength(); ++i)
        {
            current.setAttribute(
                    attributes.getQName(i), attributes.getValue(i));
        }
        if (element != null)
        {
            element.getChildren().add(current);
View Full Code Here

    {
        Output output = controllerState.getOutput();
        log.debug("adding all sources of output " + output.getName()
                + " to current source tree at element " + addTo);
        // the element where the additional source should be anchored.
        SourceElement addToSourceElement;
        List<SourceElement> sourceElementList
                = SourcePath.getElementsFromRoot(root, addTo);
        if (sourceElementList.isEmpty())
        {
            throw new SourceTransformerException(
                    "Source element " + addTo + " does not exist");
        }
        addToSourceElement = sourceElementList.get(0);


        UnitConfiguration unitConfiguration
                = controllerState.getUnitConfiguration();
        ConfigurationHandlers configurationHandlers
                = unitConfiguration.getConfigurationHandlers();
        Controller helperController = new Controller();

        SourceElement newSourceElement = new SourceElement(newElement);
        boolean newSourceElementAdded = false;
        try
        {
            // do not change state of original source provider,
            // instead make a copy.
            SourceProvider sourceProvider
                    = controllerState.getSourceProvider().copy();
            sourceProvider.init(configurationHandlers, controllerState);

            while (sourceProvider.hasNext())
            {
                Source source = sourceProvider.next();
                SourceElement rootElement = source.getRootElement();
                SourceProcessConfiguration sourceProcessConfiguration
                    = output.getSourceProcessConfiguration();
                List<SourceTransformerDefinition> transformerDefinitions
                    = sourceProcessConfiguration.getTransformerDefinitions();
                transformerDefinitions
                        = new ArrayList<SourceTransformerDefinition>
                             (transformerDefinitions);
                Iterator<SourceTransformerDefinition> transformerDefinitionIt
                        = transformerDefinitions.iterator();
                while (transformerDefinitionIt.hasNext())
                {
                    SourceTransformerDefinition transformerDefinition
                        = transformerDefinitionIt.next();
                    if (this.equals(
                            transformerDefinition.getSourceTransformer()))
                    {
                        transformerDefinitionIt.remove();
                    }
                }

                rootElement = helperController.transformSource(
                        rootElement,
                        transformerDefinitions,
                        controllerState);

                newSourceElement.getChildren().add(rootElement);
                if (!newSourceElementAdded)
                {
                    addToSourceElement.getChildren().add(newSourceElement);
                    newSourceElementAdded = true;
                }

                if (!combineSources)
                {
                    newSourceElement = new SourceElement(newElement);
                    newSourceElementAdded = false;
                }
            }
        }
        catch (ConfigurationException e)
View Full Code Here

        }

        // process children
        {
            String childIndent = currentIndent + indent;
            SourceElement previousChild = null;
            for (SourceElement child : currentElement.getChildren())
            {
                if (!hasTextAttribute)
                {
                    result.append("\n");
View Full Code Here

        if (log.isDebugEnabled())
        {
            log.debug("start creating root Element");
        }

        SourceElement result = new SourceElement(ROOT_ELEMENT_NAME);
        for (FileSource fileSource : fileSources)
        {
            SourceElement fileElement = new SourceElement(FILE_ELEMENT_NAME);
            fileElement.setAttribute(
                    PATH_ATTRIBUTE_NAME,
                    fileSource.getPath().getPath());
            fileElement.getChildren().add(fileSource.getRootElement());
            result.getChildren().add(fileElement);
        }

        if (log.isDebugEnabled())
        {
View Full Code Here

    }

    @Override
    protected SourceElement createRootElement() throws SourceException
    {
        SourceElement rootElement = new SourceElement("database");
        {
            try
            {
                Class.forName(driver);
            }
            catch (ClassNotFoundException e)
            {
                throw new SourceException(
                        "Could not find database driver class " + driver, e);
            }
            log.debug("DB driver " + driver + " loaded");
        }

        Connection con = null;
        try
        {

            con = DriverManager.getConnection(url, username, password);
            log.debug("DB connection to database " + url + " established");

            DatabaseMetaData dbMetaData = con.getMetaData();

            List<String> tableList = getTableNames(dbMetaData, schema);

            for (int i = 0; i < tableList.size(); i++)
            {
                // Add Table.
                String tableName = (String) tableList.get(i);
                log.debug("Processing table: " + tableName);

                SourceElement table = new SourceElement("table");
                rootElement.getChildren().add(table);
                table.setAttribute("name", tableName);

                List<ColumnMetadata> columns
                        = getColumns(dbMetaData, tableName, schema);
                Set<String> primaryKeys
                        = getPrimaryKeys(dbMetaData, tableName, schema);

                for (ColumnMetadata col : columns)
                {
                    String name = col.getName();
                    Integer type = col.getSqlType();
                    int size = col.getSize().intValue();
                    int scale = col.getDecimalDigits().intValue();

                    Integer nullType = col.getNullType();
                    String defValue = col.getDefValue();

                    SourceElement column = new SourceElement("column");
                    column.setAttribute("name", name);

                    column.setAttribute(
                            "type",
                            SchemaType.getByJdbcType(type).toString());

                    if (size > 0 && (type.intValue() == Types.CHAR
                            || type.intValue() == Types.VARCHAR
                            || type.intValue() == Types.LONGVARCHAR
                            || type.intValue() == Types.DECIMAL
                            || type.intValue() == Types.NUMERIC))
                    {
                        column.setAttribute("size", String.valueOf(size));
                    }

                    if (scale > 0 && (type.intValue() == Types.DECIMAL
                            || type.intValue() == Types.NUMERIC))
                    {
                        column.setAttribute("scale", String.valueOf(scale));
                    }

                    if (primaryKeys.contains(name))
                    {
                        column.setAttribute("primaryKey", "true");
                    }
                    else if (nullType.intValue() == 0)
                    {
                        column.setAttribute("required", "true");
                    }

                    if (StringUtils.isNotEmpty(defValue))
                    {
                        // trim out parens & quotes out of def value.
                        // makes sense for MSSQL. not sure about others.
                        if (defValue.startsWith("(") && defValue.endsWith(")"))
                        {
                            defValue = defValue.substring(1, defValue.length() - 1);
                        }

                        if (defValue.startsWith("'") && defValue.endsWith("'"))
                        {
                            defValue = defValue.substring(1, defValue.length() - 1);
                        }

                        column.setAttribute("default", defValue);
                    }
                    table.getChildren().add(column);
                }

                // Foreign keys for this table.
                Collection<ForeignKeyMetadata> forgnKeys
                        = getForeignKeys(dbMetaData, tableName, schema);
                for (ForeignKeyMetadata foreignKeyMetadata : forgnKeys)
                {
                    SourceElement fk = new SourceElement("foreign-key");
                    fk.setAttribute(
                            "foreignTable",
                            foreignKeyMetadata.getReferencedTable());
                    for (int m = 0; m < foreignKeyMetadata.getLocalColumns().size(); m++)
                    {
                        SourceElement ref = new SourceElement("reference");
                        ref.setAttribute("local", foreignKeyMetadata.getLocalColumns().get(m));
                        ref.setAttribute("foreign", foreignKeyMetadata.getForeignColumns().get(m));
                        fk.getChildren().add(ref);
                    }
                    table.getChildren().add(fk);
                }
            }
View Full Code Here

            SourceElement root,
            ControllerState controllerState)
        throws SourceTransformerException
    {
        // the element where the additional source should be anchored.
        SourceElement sourceElement;
        List<SourceElement> sourceElementList
                = SourcePath.getElementsFromRoot(root, element);
        if (sourceElementList.isEmpty())
        {
            throw new SourceTransformerException(
                    "Source element " + element + " does not exist");
        }
        sourceElement = sourceElementList.get(0);

        ConfigurationHandlers configurationHandlers
                = controllerState.getUnitConfiguration()
                        .getConfigurationHandlers();
        Set<StreamSourceFormat> streamSourceFormats
            = configurationHandlers.getStreamSourceFormats();
        StreamSourceFormat streamSourceFormat = null;
        for (StreamSourceFormat candidate : streamSourceFormats)
        {
            if (sourceFormat.equals(candidate.getKey()))
            {
                streamSourceFormat = candidate;
                break;
            }
        }
        if (streamSourceFormat == null)
        {
            throw new SourceTransformerException(
                    "Unknown source format" + sourceFormat);
        }

        FileSourceProvider fileSourceProvider;
        try
        {
            fileSourceProvider = new FileSourceProvider(
                    streamSourceFormat,
                    new Fileset(
                            controllerState.getSourceFile().getParentFile(),
                            fileIncludes,
                            fileExcludes),
                    false);
            fileSourceProvider.init(configurationHandlers, controllerState);
        }
        catch (ConfigurationException e)
        {
            throw new SourceTransformerException(
                    "Could initialize file source provider", e);
        }
        while (fileSourceProvider.hasNext())
        {
            Source additionalSource = fileSourceProvider.next();
            SourceElement additionalSourceRoot;
            try
            {
                additionalSourceRoot = additionalSource.getRootElement();
            }
            catch (SourceException e)
View Full Code Here

                = new FileSource(
                        new PropertiesSourceFormat(),
                        propertiesFile,
                        controllerState);

        SourceElement rootElement = fileSource.getRootElement();
        assertEquals("properties", rootElement.getName());
        assertEquals(0, rootElement.getAttributeNames().size());

        assertEquals(2, rootElement.getChildren().size());
        {
            SourceElement child0 = rootElement.getChildren().get(0);
            assertEquals("entry", child0.getName());
            assertEquals(2, child0.getAttributeNames().size());
            assertEquals("propertyName1", child0.getAttribute("key"));
            assertEquals("propertyValue1", child0.getAttribute((String) null));
        }
        {
            SourceElement child1 = rootElement.getChildren().get(1);
            assertEquals("entry", child1.getName());
            assertEquals(2, child1.getAttributeNames().size());
            assertEquals("propertyName2", child1.getAttribute("key"));
            assertEquals("propertyValue2", child1.getAttribute((String) null));
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.torque.generator.source.SourceElement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.