{{theTime}}

Search This Blog

Total Pageviews

Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

XML to HTML Converter

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *Java class to transform XML to HTML.
 *
 */
public class XSLTransformer
{    
    public static void main(String[] str) throws Exception {        
        XSLTransformer xslt = new XSLTransformer() ;
        String output = xslt.transform() ;
        System.out.println( output) ;        
    }

    public String transform(String sourceinput, String xslinput)
            throws Exception {
        Source source = new StreamSource(new StringReader(sourceinput));
        Source xsl = new StreamSource(new StringReader(xslinput));
        Writer outWriter = new StringWriter();
        Result result = new StreamResult(outWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(xsl);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        String outString = outWriter.toString();
        return outString;
    }

    public String transform() throws Exception {
        String sourceStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Company>  <Employee>  <name Java1=\"J1\">DevCommunity.net  </name>  </Employee>  <Employee>  <name C1=\"C1\">C# developer  </name>  </Employee>  <Employee>  <name Perl1=\"p1\">Java developer </name>  </Employee></Company> ";
        String xslStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"html\" indent=\"yes\"/>  <xsl:template match=\"/\">  <html>  <head>  <title>Java Developer</title>  </head>  <body>  <xsl:apply-templates/>  </body>  </html>  </xsl:template>  <xsl:template match=\"devcommunity\">  <xsl:value-of select=\"@key\"/>=  <xsl:value-of select=\"@value\"/>  <br></br>  </xsl:template></xsl:stylesheet>";
        return transform(sourceStr, xslStr);
    }
}

SeekableByteChannel example using FileChannel in Java

SeekableByteChannel: A Channel to read, write, map and, manipulate a file.  It has a current position within its file which can be both queried and modified.

        -- For huge files this is often much more efficient than invoking the usual read or write methods.

        -- Ensures the data is not lost in the event of a system crash.

        -- Very fast transfer directly to or from the filesystem cache.

        -- File channels are safe for use by multiple concurrent threads. Sample Program to use FileChannel to copy a File.
public class CopyFileChannel { public static void main(String str[]) throws IOException { /** Use try with resource feature to autoclose the FileChannel.*/
try (FileChannel inputChannel = new FileInputStream("Fromfile_PATH").getChannel()){                  try (FileChannel outputChannel = new FileOutputStream("Tofile_PATH").getChannel()){                 inputChannel.transferTo(0, inputChannel.size(), outputChannel);
                    }                }                  } }

Java Program to List Todays File in a Directory with file name starting MMDD

import java.io.* ;   
public class ListOfTodaysFiles {
    public static void main(String str[]){
        String todaysfilepattern = "MMDD\\..*";
        final Pattern pattern = Pattern.compile(todaysfilepattern);
        FilenameFilter filenameFilter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name)
            {
                Matcher m = pattern.matcher(name);
                return m.matches();
            }
        };
        File currentDir = new File( "Current_DIR" );
        String[] todaysfiles = currentDir.list(filenameFilter);       
    }
}


Factory Method Design Pattern Java Implementation

    interface JavaCertification {

        public JavaCertification getCertificate() ;

    }
    class OCJPCertification implements JavaCertification {     
        public JavaCertification getCertificate(){         
            return new OCJPCertification() ;
        }
    }

    class OCWCDCertification implements JavaCertification {     
        public JavaCertification getCertificate(){         
            return new OCWCDCertification() ;
        }
    }

    class CertificationBuilder {

        public CertificationBuilder() {}

        public JavaCertification getCertifcate(String certificate){         
            if(certificate.equalsIgnoreCase("OCJP")){
                return new OCJPCertification() ;
            }else if(certificate.equalsIgnoreCase("OCWCD")){
                return new OCWCDCertification() ;
            }
            return null ;
        }
    }


Thread safe Singleton Design Pattern Java Implementation

public class SingletonClass{
private static SingletonClass instance = null;
private SingletonClass() { }
public static synchronized SingletonClass getInstance() {
if (instance == null) {
instance = new SingletonClass();
}
return instance;
}
}

AI Predictive Bot - Scenario Model

  💡 How to Read the Costco Scenario Model This simulator bridges the gap between how a company operates (its business performance) and how...