2012-08-06 3 views
1

질문은, 어떤 이유로 든. xsd는 기본 속성과 setter 및 getter 이외의 모든 논리 변수를 정의 할 수 없으므로 xsd 정의로 '코드 삽입'을 시도했습니다. 실제로 xsd 정의는 다른 사람들이 몇 번 논의한 것입니다. 나는 '간단한 자바 메소드'로 '간단한 주입'에 문제가 없으며 클래스 정의의 맨 위에 'import'문이 필요 없다.XSD def로 생성 된 JAXB java 파일에 논리 코드를 삽입하십시오.

아직 사용하고 싶다면. setter 나 getter가 아닌 다른 패키지를 가져 오거나 가져올 수있는 방법이 없다고 생각합니다. xjc.bat test.xsd -Xinject 코드 -extension

  • 아래 관찰 :

    1. XSD 정의 test.xsd이

        <?xml version="1.0" encoding="UTF-8"?> 
           <xs:schema targetNamespace="http://company.com/schema/response" 
          xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://company.com/schema/response" 
          xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1" 
          xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector" 
          jaxb:extensionBindingPrefixes="ci"> 
          <xs:element name="client"> 
           <xs:complexType> 
            <xs:annotation> 
             <xs:appinfo> 
              <ci:code> 
               <![CDATA[ 
             private String str; 
             public String returnStr() { 
              Locations locationCls =this.getLocations(); 
              List<String> locationids = new ArrayList<String>(); 
      
              // get a list of locationid into locationids (list) 
              List<Location> locationList = locationCls.getLocation(); 
              for (Location loc : locationList) { 
               locationids.add(String.valueOf(loc.getId())); 
              } 
              // return string like loc1,loc2,loc3 
              return StringUtils.join(locationids, ','); 
             } 
               ]]> 
              </ci:code> 
             </xs:appinfo> 
            </xs:annotation> 
            <xs:sequence> 
             <xs:element name="name" type="xs:NCName" /> 
             <xs:element name="pass" type="xs:NCName" /> 
             <xs:element ref="test:locations" /> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
          <xs:element name="locations"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element maxOccurs="unbounded" ref="test:location" /> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
          <xs:element name="location"> 
           <xs:complexType> 
            <xs:attribute name="id" use="required" type="xs:string" /> 
            <xs:attribute name="address" use="required" type="xs:string" /> 
            <xs:attribute name="biz" type="xs:string" /> 
           </xs:complexType> 
          </xs:element> 
          </xs:schema> 
      
    2. 실행 JAXB의 리 명령에 대한 자세한

      내용은 아래 참조 Client.java의 코드 조각 성공적으로

    결과적으로 jdk는 Apache 공유 (또는 다른 시나리오에서 도움이되는 Google 모음과 같은 기타 3 부 util 도구)의 StringUtils가 생성 된 파일로 가져 오기되지 않으므로 컴파일 오류가 발생합니다. jaxb 플러그인을 사용하여 생성 된 자바 파일에 메소드를 삽입하거나 호출하는 일부 Google 프로젝트가 있음을 이해합니다. 우리는 xsd 자체만으로 플러그인을 만들지 않을지 알기 위해 하루 정도를 보내고 싶습니다. 어떤 아이디어라도 감사 할 것입니다.

  • 답변

    1

    당신은 당신이 주입하려는 코드, 예컨대 :

    return org.apache.commons.lang.StringUtils.join(locationids, ','); 
    
    +0

    대니, 그레이트 내에서 완전히 분류 된 클래스 이름을 지정할 수 있습니다. 이것은 어떤 경우에이 문제를 해결할 수있는 방법입니다. 이 경우 제대로 작동합니다. –

    관련 문제