jaxb 生成xsd文件

本来想直接张贴代码的,但是公司用的是内网,考不出来,直接手工敲代码,可能会有错误,自己改就行了
首先我们新建一个类用来配置我们的路劲,如下:

public class XSDBuilder{
    private final String path;

    private final String despath;

    private final boolean judge;

    private final List<Class> cls = new ArrayList<Class>();

    private XSDBuilder(XXBuilder builder){
        this.path = builder.path;
        this.despath = builder.despath;
        this.judge = builder.change;
        this.cls.addAll(builder.cls);
    }


   public String getPath(){
        return path;
    }

    public String getDespath(){
        return despath;
    }

    public boolean isJudge(){
        return judge;
    }

    public List<Class> getCls(){
        return cls;
    }

    public static class XXBuilder{
        private String path;

        private String despath;

        private boolean change = false;

        private List<Class> cls = new ArrayList<Class>();

        public XXBuilder path(String path){
            this.path = path;
            return this;
        } 

        public XXBuilder despath(String despath){
            this.despath= despath;
            return this;
        }

        public XXBuilder changeXSD(boolean change){
            this.change = change;
            return this;
        }

        public XXBuilder addCls(Class cls){
            this.cls.add(cls);
            return this;
        }

        public XSDBuilder build(){
            return new XSDBuilder(this);
        }
    }
    }

XSDGeneral.java

public class XSDGeneral{
    private static XSDSchemaOutputResolver resolver;

    public static void createXSD(XSDBuilder builder){
        List<Class> cls = builder.getCls();
        for(Class cl : cls){
            resolver = new XSDSchemaOutputResolver(builder.getPath(),cl.getSimpleName()+".xsd");
            generalXSDFile(cl);
        }
        if(builder.isJudge()){
            XSDFileUtils.readFiles(builder.getPath(),builder.getDespath());
        }
    }

    public static void generalXSDFile(Class classes){
        try{
            JAXBContext context = JAXBContext.newInstance(classes);
            context.generateSchema(resolver);
        }catch(JAXBException e){

        }catch(IOException e){

        }
    }

}

XSDSchemaOutputResolver.class

public class XSDSchemaOutputResolver extends SchemaOutputResolver{
    private File file;
    XSDFileUtils.generalFile(file);
}

@Override
public Result createOutput(String namespaceUri,String 
suggestedFileName
)throws IOException{
    return new StreamResult(file);
}

XSDFileUtils .class

public class XSDFileUtils{
    private static StringBuilder buffer = new StringBuilder();
    private static String prefix = "<xs:element";
    private static String minOccurs = "minOccurs=\"1\" ";
    private static String minOccurs2 = "minOccurs=\"0\" ";
private static String nillable= "nillable=\"false\" ";
private static String nillable2= "nillable=\"true\" ";
private String desPath = null;

public static void readFiles(String path,String despath){
    File file = new File(path);
    if(file.isDirectory()){
        String[] fl = file.list();
        for(String tempFile : fl){
            File f = new File(path+"\\"+tempFile);
            if(!f.isDirectory()){
                changeContent(f,despath);
            }
        }
    }

    public static void changeContent(File f,String despath){
        try{
            InputStreamReader in = new InputStreamReader(new FileInputStream(f),"utf-8");
            StringBuilder build = new StringBuilder();
            BufferedReader reader = new BufferedReader(in);
            String content = null;
            while((content = reader.readLine()) != null){
        String s = changeContent(content);
        build.append(s+"\n");
}
    if(despath == null){
        despath = f.getParentFile().getPath();
    }
    String outpath = System.getProperty("user.dir");
    String filepath = f.getPath().substring(f.getPath().lastIndexOf("\\")+1);
    String temp = despath + File.separator +outpath.substring(outpath.lastIndexOf("\\")+1);
    writeFile(build.toString(),temp,filepath);
    reader.close();
        }catch(FileNotFoundException e){
        }catch(IOException e){
        }
    }
}

pubic static String changeContent(String content){
    String temp = content;
    if(content.trim().startWith(prefix)){
        if(content.indexOf("minOccurs=")<0 && content.indexOf("nillable=")<0){
            String end = content.substring(content.indexOf(prefix)+prefix.length()+1)+minOccurs+nillable+end;
        }
        if(content.contains("minOccurs=\"0\"") && !content.contains("nillable=")){
        String prefix2 = "minOccurs=\"0\"";
        String tempContent = content.replace(prefix2,"");
        String end = tempContent.substring(tempContent.indexOf(prefix)+prefix.length()+1);
        temp = tempContent.substring(0,tempContent.indexOf(prefix)+prefix.length()+1)+minOccurs2+nillable2+end;
    }
    }
    return temp;
}

public static void writeFile(String content,String path,String fileName){
    try{
        File files = new File(path,fileName);
        generalFile(files);
        BufferedWriter write = new BufferedWriter(new FileWriter(files));
        write.write(content);
        write.close();
    }catch(IOException){}
}

public static void generalFile(File file){
    try{
        if(!file.getParenFile().isDirectory()){
            file.getParentFile().mkdirs();
        }
        if(!file.exists()){
            file.createNewFile();
        }
    }catch(IOException e){

    }

}

}

测试类

XSDBuilder builder = new XSDBuilder.XXBuilder()
                    .path("D:\\hello")
                    .addCls(User.class)
                    .addCls(Teacher.class)
                    .build();
XSDGeneral.createXSD(builder);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值