Post List

태그

2019년 1월 24일 목요일

자바 정규식(비밀번호, 이메일, 아이피주소)

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RegexTest1{
 
   private Pattern pattern;
   private Matcher matcher;

   public RegexTest1(String pattern){
    this.pattern = Pattern.compile(pattern);
   }
   
   public boolean validate(String text){
    
    matcher = pattern.matcher(text);
    return matcher.matches();
          
   }
   
   public static void main(String [] args){
    
    String passwordPattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
    
    String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
     + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    String ipaddressPattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
               + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
    
    
    RegexTest1 password = new RegexTest1(passwordPattern);
    RegexTest1 email = new RegexTest1(emailPattern);
    RegexTest1 ipaddress = new RegexTest1(ipaddressPattern);
    
    String text1 = "aaSS12!@";
    String text2 = "ASsdv221@nate.com";
    String text3 = "127.1.1.1";
    
    System.out.println(password.validate(text1) + "\t" + text1);
    System.out.println(email.validate(text2) + "\t" + text2);
    System.out.println(ipaddress.validate(text3) + "\t" + text3);
    
    text1 = "aaS!@";
    text2 = "ASsdv221@natecom";
    text3 = "0127.1.1.1";
    
    System.out.println(password.validate(text1) + "\t" + text1);
    System.out.println(email.validate(text2) + "\t" + text2);
    System.out.println(ipaddress.validate(text3) + "\t" + text3);
   }
   
   
}



자바 geotools 15.1 좌표간 거리계산


import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.GeodeticCalculator;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.vividsolutions.jts.geom.Coordinate;

public class GeoDistanceTest { 

 private CoordinateReferenceSystem sourceCRS;
 private String sourceX;
 private String sourceY;
 
 public GeoDistanceTest(String sourceCRS, String sourceX, String sourceY) throws Exception {
  this.sourceCRS = CRS.decode(sourceCRS);
  this.sourceX = sourceX;
  this.sourceY = sourceY;
  
 }
 
 public void distanceCalculator(String x, String y) throws Exception {
  
  GeodeticCalculator gc = new GeodeticCalculator(sourceCRS);
  
  gc.setStartingPosition(JTS.toDirectPosition(new Coordinate(Double.parseDouble(sourceX), Double.parseDouble(sourceY)),sourceCRS));
  gc.setDestinationPosition(JTS.toDirectPosition(new Coordinate(Double.parseDouble(x), Double.parseDouble(y)),sourceCRS));
  
  double distance = gc.getOrthodromicDistance();
  
  System.out.println("distance (m) : "+ distance);
 } 
 
 public static void main(String[] args) throws Exception {
  
  GeoDistanceTest g = new GeoDistanceTest("EPSG:4326", "37.5639686", "126.9794393");

  g.distanceCalculator("37.5639686", "126.9794393");
     
 }

}


자바 geotools 15.1 좌표 변환

import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class GeoTransTest { 

 private CoordinateReferenceSystem sourceCRS;
 private CoordinateReferenceSystem targetCRS;
 
 public GeoTransTest(String sourceCRS, String targetCRS) throws Exception {
  this.sourceCRS = CRS.decode(sourceCRS);
  this.targetCRS = CRS.decode(targetCRS);
  
 }
 
 public void transform(String x, String y) throws Exception {
  
  GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  Coordinate coord = new Coordinate(Double.parseDouble(x), Double.parseDouble(y));
  Geometry sourceGeometry = geometryFactory.createPoint(coord);
  
  MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
  
  Geometry targetGeomery = JTS.transform(sourceGeometry, transform);
  Point targetPoint = targetGeomery.getCentroid();

  System.out.println("변환전 EPSG:4326(WGS84) X : "+x + "\t EPSG:4326(WGS84) Y : "+y);
  System.out.println("변환후 EPSG:3857 X : "+targetPoint.getX() + "\t EPSG:3857 Y : "+targetPoint.getY());
  
 } 
 
 public static void main(String[] args) throws Exception {
  
  GeoTransTest g = new GeoTransTest("EPSG:4326", "EPSG:3857");

  g.transform("37.5639686", "126.9794393");
     
 }

}


자바 진법 변환


import java.util.LinkedList;
import java.util.Scanner;

public class Test1 {
 
 
 
 
 public static void main(String[] args){
  
  Scanner sc = new Scanner(System.in);
  System.out.println("변환할 숫자를 입력하세요.");
  
  String value = sc.next();
  
  for(int i=2;i<=16;i++){
   LinkedList list = convert(Integer.parseInt(value),i);
   System.out.print(value + "의 " + i +" 진법 : ");
   while(!list.isEmpty()){
    System.out.print(list.pop());
   }
  }
 }
 
 public static LinkedList convert(int value, int i){

  LinkedList list = new LinkedList();
    
  while(value != 0){
   // 나머지가 0~9 사이 일때
   if((value % i)<10){
    
    list.push(String.valueOf((value % i)));
    
   }else{
    // 나머지가 10 이상일때 해당하는 알파뱃을 저장
    char temp1 = (char)((value % i)+55);
        
    list.push(String.valueOf(temp1));
    
   }
   //몫을 구함
   value /= i;
  }
    
  return list;
 }

}

카카오 지도 api 사용

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
public class KakaoGeoApi1 {

    public static void main(String[] args) throws Exception {
        
    
     System.out.println(addrToCoord(URLEncoder.encode("주소입력","UTF-8")));
     System.out.println(coordToAddr("37.5665958","126.9783813"));
    }
    
    public static String addrToCoord(String addr){
     
        String url = "https://dapi.kakao.com/v2/local/search/address.json?query="+addr;
        String json = "";
        try{
            json = getJSONData(url);
        }catch(Exception e){
            
            e.printStackTrace();
        }
        return json;
    }

    public static String coordToAddr(String x, String y){
     
     String url = "https://dapi.kakao.com/v2/local/geo/coord2address.json?x="+x+"&y="+y+"&input_coord=WGS84";
        String json = "";
        try{
            json = getJSONData(url);
        }catch(Exception e){
            
            e.printStackTrace();
        }
        return json;
    }
    
    
    private static String getJSONData(String apiUrl) throws Exception {
        String jsonString = new String();
        String buf;
        String apikey = "apikey"; //apikey
        
        URL url = new URL(apiUrl);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        String auth = "KakaoAK "+apikey;
        conn.setRequestMethod("GET");
        conn.setRequestProperty("X-Requested-With", "curl");
        conn.setRequestProperty("Authorization", auth);
        
        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));
        while ((buf = br.readLine()) != null) {
            jsonString += buf;
        }
        return jsonString;
    }
}

네이버 지도 api 사용

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class NaverGeoApi {

    public static void main(String[] args) {
        String clientId = "clientId "; //clientId 
        String clientSecret = "clientSecret ";  //clientSecret 
        
        try {
            String addr = URLEncoder.encode("주소입력", "UTF-8");  //주소입력
            String apiURL = "https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query=" + addr; //json
            //String apiURL = "https://openapi.naver.com/v1/map/geocode.xml?query=" + addr; // xml
            URL url = new URL(apiURL);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId);
            con.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret);
            int responseCode = con.getResponseCode();
            BufferedReader br;
            if(responseCode==200) { 
                br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            } else {  
                br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            }
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = br.readLine()) != null) {
                response.append(inputLine);
            }
            br.close();
            System.out.println(response.toString());
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}