Agile Zone is brought to you in partnership with:
DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
  • submit to reddit
Mike Bushong10/16/14
2608 views
0 replies

On choice-supportive bias and the need for paranoid optimism

Companies are not blind because the individuals are not talented; they are blind because there are powerful psychological dynamics at play that keep us all from seeing that which we must see the most.

Kin Lane10/16/14
2624 views
2 replies

I Am Not Signing Any More NDAs

I just replied to a company who I really like, and trust, letting them know I won’t be signing any more NDAs. There are plenty of companies that I can exchange knowledge, research, data, and other vital information without signing NDAs.

Venkatesh Kris...10/16/14
4433 views
0 replies

If you are start-up, think beyond one user!

It's extraordinarily difficult to take a product that was built perfectly for a particular user and commercialize that into a broader market.

//Insert a Document at Any Location
/**
 * Inserts content of the external document after the specified node.
 * Section breaks and section formatting of the inserted document are ignored.
 *
 * @param insertAfterNode Node in the destination document after which the content
 * should be inserted. This node should be a block level node (paragraph or table).
 * @param srcDoc The document to insert.
 */
public static void insertDocument(Node insertAfterNode, Document srcDoc) throws Exception
{
    // Make sure that the node is either a paragraph or table.
    if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
      (insertAfterNode.getNodeType() != NodeType.TABLE))
        throw new IllegalArgumentException("The destination node should be either a paragraph or table.");

    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.getParentNode();

    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);

    // Loop through all sections in the source document.
    for (Section srcSection : srcDoc.getSections())
    {
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        for (Node srcNode : (Iterable<Node>) srcSection.getBody())
        {
            // Let's skip the node if it is a last empty paragraph in a section.
            if (srcNode.getNodeType() == (NodeType.PARAGRAPH))
            {
                Paragraph para = (Paragraph)srcNode;
                if (para.isEndOfSection() && !para.hasChildNodes())
                    continue;
            }

            // This creates a clone of the node, suitable for insertion into the destination document.
            Node newNode = importer.importNode(srcNode, true);

            // Insert new node after the reference node.
            dstStory.insertAfter(newNode, insertAfterNode);
            insertAfterNode = newNode;
        }
    }
}

//Insert a Document During Mail Merge

//This example relies on the InsertDocument method shown at the beginning of the article to insert a document into a merge field during mail merge execution. This code demonstrates how to use the InsertDocument method to insert a document into a merge field during mail merge.

public void insertDocumentAtMailMerge() throws Exception
{
    // Open the main document.
    Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc");

    // Add a handler to MergeField event
    mainDoc.getMailMerge().setFieldMergingCallback(new InsertDocumentAtMailMergeHandler());

    // The main document has a merge field in it called "Document_1".
    // The corresponding data for this field contains fully qualified path to the document
    // that should be inserted to this field.
    mainDoc.getMailMerge().execute(
        new String[] { "Document_1" },
        new String[] { getMyDir() + "InsertDocument2.doc" });

    mainDoc.save(getMyDir() + "InsertDocumentAtMailMerge Out.doc");
}

private class InsertDocumentAtMailMergeHandler implements IFieldMergingCallback
{
    /**
     * This handler makes special processing for the "Document_1" field.
     * The field value contains the path to load the document.
     * We load the document and insert it into the current merge field.
     */
    public void fieldMerging(FieldMergingArgs e) throws Exception
    {
        if ("Document_1".equals(e.getDocumentFieldName()))
        {
            // Use document builder to navigate to the merge field with the specified name.
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            builder.moveToMergeField(e.getDocumentFieldName());

            // The name of the document to load and insert is stored in the field value.
            Document subDoc = new Document((String)e.getFieldValue());

            // Insert the document.
            insertDocument(builder.getCurrentParagraph(), subDoc);

            // The paragraph that contained the merge field might be empty now and you probably want to delete it.
            if (!builder.getCurrentParagraph().hasChildNodes())
                builder.getCurrentParagraph().remove();

            // Indicate to the mail merge engine that we have inserted what we wanted.
            e.setText(null);
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {
        // Do nothing.
    }
}
 
//If a document to be inserted is stored as binary data in the database field (BLOB field), use the following example. A slight variation to the above example to load a document from a BLOB database field instead of a file.

private class InsertDocumentAtMailMergeBlobHandler implements IFieldMergingCallback
{
    /**
     * This handler makes special processing for the "Document_1" field.
     * The field value contains the path to load the document.
     * We load the document and insert it into the current merge field.
     */
    public void fieldMerging(FieldMergingArgs e) throws Exception
    {
        if ("Document_1".equals(e.getDocumentFieldName()))
        {
            // Use document builder to navigate to the merge field with the specified name.
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            builder.moveToMergeField(e.getDocumentFieldName());

            // Load the document from the blob field.
            ByteArrayInputStream inStream = new ByteArrayInputStream((byte[])e.getFieldValue());
            Document subDoc = new Document(inStream);
            inStream.close();

            // Insert the document.
            insertDocument(builder.getCurrentParagraph(), subDoc);

            // The paragraph that contained the merge field might be empty now and you probably want to delete it.
            if (!builder.getCurrentParagraph().hasChildNodes())
                builder.getCurrentParagraph().remove();

            // Indicate to the mail merge engine that we have inserted what we wanted.
            e.setText(null);
        }
    }
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {
        // Do nothing.
    }
}

//Insert a Document During Replace

//This example shows how to insert content of one document into another during a customized find and replace operation.

[Code Sample]

public void insertDocumentAtReplace() throws Exception
{
    Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc");
    mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), new InsertDocumentAtReplaceHandler(), false);
    mainDoc.save(getMyDir() + "InsertDocumentAtReplace Out.doc");
}

private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");

        // Insert a document after the paragraph, containing the match text.
        Paragraph para = (Paragraph)e.getMatchNode().getParentNode();
        insertDocument(para, subDoc);

        // Remove the paragraph with the match text.
        para.remove();

        return ReplaceAction.SKIP;
    }
}
Alec Noller10/15/14
6607 views
1 replies

Dev of the Week: Ashwini Kuntamukkala

Every week here and in our newsletter, we feature a new developer/blogger from the DZone community to catch up and find out what he or she is working on now and what's coming next. This week we're talking to Ashwini Kuntamukkala, Software Architect at SciSpike, Inc.

John Walter10/15/14
8819 views
0 replies

The Best of DZone: Oct. 8 - Oct. 15

If you missed anything on DZone this week, now's your chance to catch up! This week's best include a tutorial for getting started building mobile apps with the Ionic framework, an editorial detailing how developers are integrating, an attempt at linear regression, and more.

Adam Diaz10/15/14
5466 views
0 replies

Hadoop and the mystery of the version number

When I’m working with people on Hadoop I ask what you would think is a simple question. What version of Hadoop are you using? In reality though it’s not as straight forward as you might think.

Peter Lawrey10/15/14
4709 views
0 replies

Kafka Benchmark on Chronicle Queue

I was recently asked to compare the performance of Kafka with Chronicle Queue. No two products are exactly alike, and performing a fair comparison is not easy. We can try to run similar tests and see what results we get.

Mikio Braun10/14/14
2793 views
0 replies

Parts But No Car

One question which pops up again and again when I talk about streamdrill is whether that cannot be done by X, where X is one of Hadoop, Spark, Go, or some other piece of Big Data infrastructure. The truth is that there’s a huge gap between “in principle” and “in reality”, and I’d like to spell this difference out in this post.

Mike Cottmeyer10/14/14
5990 views
0 replies

Teams 102

In this post, I want to go just a little deeper on the notion of teams and challenge some of the common thinking around agile and agile transformation.

Johanna Rothman10/14/14
5074 views
2 replies

Management Feedback: Are You Abrasive or Assertive?

We all need feedback. Managers need feedback, too. Because managers leverage the work of others, they need feedback even more than technical people.

Andrew Gilmartin10/13/14
4594 views
0 replies

An Observer pattern implementation and illiterate programming

The Observer pattern and an experiment in documenting Java code using "illiterate programming."

Pubudu Dissanayake10/13/14
2243 views
0 replies

How to draw a Control flow graph & Cyclometric complexity for a given procedure

Cyclomatic complexity is a software metric used to measure the complexity of a program. This metric measures independent paths through the program's source code.

Tim Murphy10/12/14
7019 views
3 replies

Teaching Old Dogs New Tricks

Old developers can easily get set in their ways especially if they work in one environment for a dozen years or more. So how do you overcome the “that is the way it has always been done” mentality that sets in?

Mike Cottmeyer10/11/14
6582 views
0 replies

Teams 101

The notion of forming complete cross functional teams is one of the most well understood concepts in the agile community but maybe one of the least implemented in practice.

Arthur Charpentier10/11/14
2700 views
0 replies

Cross Validation for Kernel Density Estimation

In a post published in July, I mentioned the so-called Goldilocks principle, in the context of kernel density estimation, and bandwidth selection. The bandwidth should not be too small (the variance would be too large) and it should not be too large (the bias would be too large).

Paul Underwood10/10/14
2441 views
0 replies

Output Contents Of A Doctrine Object

I've being doing a lot of development with Doctrine recently and one of the things that I found out is that the Objects it works with can be huge which makes debugging the objects quite difficult for you and the browser to process.

Alec Noller10/10/14
16727 views
6 replies

Poll Results Are In: The World's Greatest Programmers

You may have seen our recent poll about the world's greatest programmers - maybe you gave an opinion! Well, the results are in, and we've created this infographic to help you visualize the rankings as determined by DZone's audience.

James Shore10/10/14
5607 views
0 replies

Agile and Predictability

It's entirely possible to make predictions with Agile. They're just as good as predictions made with other methods, and with XP practices, they can be much better. Agile leaders talk about embracing change because that has more potential value than making predictions.

Chris Odell10/10/14
5747 views
0 replies

Anyone Can Be A Codesmith.

I think that anyone who writes code in any shape or form should feel free about calling themselves a codesmith. The ideals of craftmanship should be followed, with a healthy dose of pragmatism, but learning the skills to become a good coder takes time and experience.