1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.model.impl;
  27 
  28 import javax.activation.MimeType;
  29 import javax.activation.MimeTypeParseException;
  30 import javax.xml.bind.annotation.XmlMimeType;
  31 import javax.xml.bind.annotation.XmlSchemaType;
  32 import javax.xml.bind.annotation.XmlSchemaTypes;
  33 import javax.xml.namespace.QName;
  34 
  35 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
  36 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationSource;
  37 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
  38 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
  39 
  40 /**
  41  * Common code between {@link PropertyInfoImpl} and {@link ElementInfoImpl}.
  42  *
  43  * @author Kohsuke Kawaguchi
  44  */
  45 final class Util {
  46     static <T,C,F,M> QName calcSchemaType(
  47             AnnotationReader<T,C,F,M> reader,
  48             AnnotationSource primarySource, C enclosingClass, T individualType, Locatable src ) {
  49 
  50         XmlSchemaType xst = primarySource.readAnnotation(XmlSchemaType.class);
  51         if(xst!=null) {
  52             return new QName(xst.namespace(),xst.name());
  53         }
  54 
  55         // check the defaulted annotation
  56         XmlSchemaTypes xsts = reader.getPackageAnnotation(XmlSchemaTypes.class,enclosingClass,src);
  57         XmlSchemaType[] values = null;
  58         if(xsts!=null)
  59             values = xsts.value();
  60         else {
  61             xst = reader.getPackageAnnotation(XmlSchemaType.class,enclosingClass,src);
  62             if(xst!=null) {
  63                 values = new XmlSchemaType[1];
  64                 values[0] = xst;
  65             }
  66         }
  67         if(values!=null) {
  68             for( XmlSchemaType item : values ) {
  69                 if(reader.getClassValue(item,"type").equals(individualType)) {
  70                     return new QName(item.namespace(),item.name());
  71                 }
  72             }
  73         }
  74 
  75         return null;
  76     }
  77 
  78     static MimeType calcExpectedMediaType(AnnotationSource primarySource,
  79                         ModelBuilder builder ) {
  80         XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
  81         if(xmt==null)
  82             return null;
  83 
  84         try {
  85             return new MimeType(xmt.value());
  86         } catch (MimeTypeParseException e) {
  87             builder.reportError(new IllegalAnnotationException(
  88                 Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
  89                 xmt
  90             ));
  91             return null;
  92         }
  93     }
  94 }