Monday, February 26, 2018

XML helper classes (for files from SWIFT)

package au.brian;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.*;

public class FileReaderTest {

String file = "BICPLUS_V1_DELTA_20180223.xml";
String zipFile = "BICPLUS_20180223_XML.zip";
String dir = "C:\\Users\\brian\\Downloads\\";

Logger log = LoggerFactory.getLogger(FileReaderTest.class);

@Test
public void testGetFileText() {
// fail("Not yet implemented");
FileReader f = new FileReader();
String s = f.getFileText(new File(dir + zipFile), file);
if (s == null || s.length() == 0) {
fail("file contents = " + s);
}
else {
System.out.println("s.length()=" + s.length());
System.out.println("start=" + StringUtils.substring(s, 0, 200));
System.out.println("end=" + StringUtils.substring(s, s.length()-200,s.length()));
}

}

@Test
public void testPrintContents() {
// fail("Not yet implemented");
FileReader f = new FileReader();
f.printContents(new File(dir + zipFile), file);
// if (s == null || s.length() == 0) {
// fail("file contents = " + s);
// }
// else {
// System.out.println("s.length()=" + s.length());
// }

}

@Test
public void testPrintElements() {
// fail("Not yet implemented");
FileReader f = new FileReader();
f.printElements(new File(dir + zipFile), file, "bicplus_v1", 0, 2);
// if (s == null || s.length() == 0) {
// fail("file contents = " + s);
// }
// else {
// System.out.println("s.length()=" + s.length());
// }

}

}

-----------

package au.brian;

import java.util.Enumeration;
import java.util.Hashtable;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class BicPrintContentsHandler extends DefaultHandler {
  private Hashtable<String, Integer> tags;
  
    public void startDocument() throws SAXException {
        tags = new Hashtable<>();
    }

    public void endDocument() throws SAXException {
        Enumeration<String> e = tags.keys();
        while (e.hasMoreElements()) {
            String tag = e.nextElement();
            int count = tags.get(tag);
            System.out.println("Local Name \"" + tag + "\" occurs " 
                               + count + " times");
        }    
    }
    
    public void startElement(String namespaceURI,
                             String localName,
                             String qName, 
                             Attributes atts)
        throws SAXException {

        String key = localName;
        Integer count = tags.get(key);

        if (count == null) {
            tags.put(key, new Integer(1));
        } 
        else {
            count++;
            tags.put(key, new Integer(count));
        }
    }
     
}

-----

package au.brian;

import org.apache.commons.lang3.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class BicPrintElementsHandler extends DefaultHandler {
  private String tagToPrint;
  private int occuranceToPrint;
  private int numberToPrint;
  private int occuranceCount = 0;
  private int numberCount = 0;
  private boolean tagStarted = false;
  
  public BicPrintElementsHandler(String tagToPrint, int occuranceToPrint, int numberToPrint) {
  this.tagToPrint = tagToPrint;
  this.occuranceToPrint = occuranceToPrint;
  this.numberToPrint = numberToPrint;
  }
      
  @Override
  public void startElement(String namespaceURI,
                             String localName,
                             String qName, 
                             Attributes atts)
        throws SAXException {
 
  if (tagStarted) {
        System.out.print("\n<" + localName + ">");
  }
  else if (localName.equals(tagToPrint)) {
        if (occuranceCount == occuranceToPrint) {
        tagStarted = true;
              System.out.print("\n<" + localName + ">");
        }
        occuranceCount++;
        } 
    }
  
  @Override
  public void endElement(String namespaceURI,
              String localName,
              String qName) {
  
        if (tagStarted) {
        if (localName.equals(tagToPrint)) { 
        if (numberCount == numberToPrint) {
          tagStarted = false;
        }
        else {
        numberCount++;
        }
        }
          System.out.print("</" + localName + ">");
        }
  }
 
  
  @Override
  public void characters(char[] ch, int start, int length)  {
  if (tagStarted) {
  System.out.print(StringUtils.substring(String.valueOf(ch), start, start+length) );
  }
  }
     
}

-------

package au.brian;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.io.IOUtils;
import org.xml.sax.XMLReader;

public class FileReader {
public String getFileText(File zipFilename, String filename) {
try {
ZipFile zipFile = new ZipFile(zipFilename);
    InputStream input = null;
    input = getStream(zipFile, filename);
    try {
    if (input == null) {
    return "";
    }
    String s = IOUtils.toString(input, "UTF-8");
    return s;
    }
    finally {
    if (input != null) input.close();
    zipFile.close();
    }
}
catch (final IOException ioe) {
    System.err.println("Unhandled exception:");
    ioe.printStackTrace();
}
return "";
}
public void printContents(File zipFilename, String filename) {
try {
ZipFile zipFile = new ZipFile(zipFilename);
    InputStream input = null;
    input = getStream(zipFile, filename);
    try {
    if (input == null) {
    return;
    }
    SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();     
        saxParser.parse(input,new BicPrintContentsHandler());

    }
catch (final Exception e) {
    System.err.println("Unhandled exception:");
    e.printStackTrace();
}
    finally {
    if (input != null) input.close();
    zipFile.close();
    }
}
catch (final IOException ioe) {
    System.err.println("Unhandled exception:");
    ioe.printStackTrace();
}
}
public void printElements(File zipFilename, String filename, String tag, int occurence, int number) {
try {
ZipFile zipFile = new ZipFile(zipFilename);
    InputStream input = null;
    input = getStream(zipFile, filename);
    try {
    if (input == null) {
    return;
    }
    SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();     
        saxParser.parse(input,new BicPrintElementsHandler(tag, occurence, number));

    }
catch (final Exception e) {
    System.err.println("Unhandled exception:");
    e.printStackTrace();
}
    finally {
    if (input != null) input.close();
    zipFile.close();
    }
}
catch (final IOException ioe) {
    System.err.println("Unhandled exception:");
    ioe.printStackTrace();
}
}
private InputStream getStream(ZipFile zipFile, String filename) {
try {
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    InputStream input = null;
    while (entries.hasMoreElements()) {
  final ZipEntry zipEntry = entries.nextElement();
  if (!zipEntry.isDirectory()) {
    final String fileName = zipEntry.getName();
    if (fileName.equalsIgnoreCase(filename)) {
       input = zipFile.getInputStream(zipEntry);
       return input;
    }
  }
    }
}
catch (final IOException ioe) {
    System.err.println("Unhandled exception:");
    ioe.printStackTrace();
}
return null;
}
}

Tuesday, September 15, 2015

Chapter 3 - How to model services

A good service: loose coupling and high cohesion

Bounded context: a model has specific information. If this is wanted externally, a request must be made using an explicit interface. Share information uses a shared model object, not the internal representation.

Break the system up along functional (and/or organisational) boundaries, not technical boundaries.

Chapter 1




What Are Microservices?

Small: the smaller the service, the more you maximize the benefits and downsides of microservice architecture.
Focused on Doing One Thing Well: Robert C. Martin’s Single Responsibility Principle, which states “Gather together those things that change for the same reason, and separate those things that change for different reasons.”
Autonomous: All communication between the services themselves are via network calls, to enforce separation between the services and avoid the perils of tight coupling.

Benefits
- tech heterogeneity
- resilience
- scaling
- ease of deployment
- organisational alignment
- composability
- optimized for replaceability



Hexagonal architecture: Create your application to work without either a UI or a database