/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Igor A. Pyankov
*/
package org.apache.harmony.x.print.cups;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Vector;
import javax.print.DocFlavor;
import javax.print.MultiDocPrintService;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import org.apache.harmony.x.print.DefaultPrintService;
import org.apache.harmony.x.print.ipp.IppAttribute;
import org.apache.harmony.x.print.ipp.IppAttributeGroup;
import org.apache.harmony.x.print.ipp.IppClient;
import org.apache.harmony.x.print.ipp.IppOperation;
import org.apache.harmony.x.print.ipp.IppPrinter;
import org.apache.harmony.x.print.ipp.IppRequest;
import org.apache.harmony.x.print.ipp.IppResponse;
/*
* The class extends PrintServiceLookup and is intended for
* looking up CUPS/IPP printers
*
* 1. The class allways looks printers on http://localhost:631
* This URL is default URL for default installation of CUPS server
* 2. The class accepts two properties:
* print.cups.servers - a list of CUPS servers
* print.ipp.printers - a list of IPP printers
* (note, that CUPS printer is IPP printer too)
*/
public class CUPSPrintServiceProvider extends PrintServiceLookup {
private static String cupsdefault = "http://localhost:631";
private static ArrayList services = new ArrayList();
/*
* 0 - no
* 1 - more
* 2 - more and more
* ...
*/
private static int verbose = 0;
static {
String verbose_property = (String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("print.cups.verbose");
}
});
if (verbose_property != null) {
try {
Integer v = new Integer(verbose_property);
setVerbose(v.intValue());
} catch (NumberFormatException e) {
setVerbose(0);
}
}
}
public CUPSPrintServiceProvider() {
super();
}
/*
* The method returns array of URLs of CUPS servers
*/
private static String[] getCUPSServersByProperty() {
ArrayList cupslist = new ArrayList();
cupslist.add(cupsdefault);
String cupspath = (String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("print.cups.servers");
}
});
String pathsep = ",";
if (cupspath != null && !cupspath.equals("")) {
String[] cupss = cupspath.split(pathsep);
for (int i = 0, ii = cupss.length; i < ii; i++) {
if (!cupss[i].equals("")) {
try {
URI cupsuri = new URI(cupss[i]);
cupslist.add(cupsuri.toString());
} catch (URISyntaxException e) {
if (verbose > 0) {
System.err.println("CUPS url: " + cupss[i]);
e.printStackTrace();
} else {
// IGNORE bad URI exception
}
}
}
}
}
return (String[]) cupslist.toArray(new String[0]);
}
/*
* The method returns array of URLs of IPP printers
*/
private static String[] getIppPrintersByProperty() {
ArrayList ipplist = new ArrayList();
String ipppath = (String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("print.ipp.printers");
}
});
String pathsep = ","; //System.getProperty("path.separator");
if (ipppath != null && !ipppath.equals("")) {
String[] ipps = ipppath.split(pathsep);
for (int i = 0, ii = ipps.length; i < ii; i++) {
if (!ipps[i].equals("")) {
try {
URI cupsuri = new URI(ipps[i]);
ipplist.add(cupsuri.toString());
} catch (URISyntaxException e) {
if (verbose > 0) {
System.err.println("IPP url: " + ipps[i]);
e.printStackTrace();
} else {
// IGNORE bad URI exception
}
}
}
}
}
return (String[]) ipplist.toArray(new String[0]);
}
/*
* @see javax.print.PrintServiceLookup#getDefaultPrintService()
*/
public PrintService getDefaultPrintService() {
synchronized (this) {
String defaultService = findDefaultPrintService();
if (defaultService != null) {
PrintService service = getServiceStored(defaultService,
services);
if (service != null) {
return service;
}
CUPSClient client;
try {
client = new CUPSClient(defaultService);
service = new DefaultPrintService(defaultService, client);
services.add(service);
return service;
} catch (PrintException e) {
// just ignore
e.printStackTrace();
}
}
if (services.size() == 0) {
getPrintServices();
}
if (services.size() > 0) {
return (PrintService) services.get(0);
}
}
return null;
}
/*
* @see javax.print.PrintServiceLookup#getPrintServices()
*/
public PrintService[] getPrintServices() {
synchronized (this) {
String[] serviceNames = findPrintServices();
if (serviceNames == null || serviceNames.length == 0) {
services.clear();
return new PrintService[0];
}
ArrayList newServices = new ArrayList();
for (int i = 0; i < serviceNames.length; i++) {
PrintService service = getServiceStored(serviceNames[i],
services);
if (service != null) {
newServices.add(service);
} else if (getServiceStored(serviceNames[i], newServices) == null) {
try {
CUPSClient client = new CUPSClient(serviceNames[i]);
service = new DefaultPrintService(serviceNames[i],
client);
newServices.add(service);
} catch (PrintException e) {
// just ignore
e.printStackTrace();
}
}
}
services.clear();
services = newServices;
return (services.size() == 0) ? new PrintService[0]
: (PrintService[]) services.toArray(new PrintService[0]);
}
}
/*
* find printers on particular CUPS server
*/
private PrintService[] getCUPSPrintServices(String cups) {
synchronized (this) {
// just update static field 'services'
findPrintServices();
// next find services on server 'cups'
String[] serviceNames = (String[]) findCUPSPrintServices(cups)
.toArray(new String[0]);
if (serviceNames == null || serviceNames.length == 0) {
return new PrintService[0];
}
// return only those are stored in field 'services'
ArrayList newServices = new ArrayList();
for (int i = 0; i < serviceNames.length; i++) {
PrintService service = getServiceStored(serviceNames[i],
services);
if (service != null) {
newServices.add(service);
}
}
return (newServices.size() == 0) ? new PrintService[0]
: (PrintService[]) services.toArray(new PrintService[0]);
}
}
/*
* find printers on localhost only
*/
public PrintService[] getPrintServicesOnLocalHost() {
return getCUPSPrintServices(cupsdefault);
}
/*
* find service which name is same as serviceName
*/
private PrintService getServiceStored(String serviceName,
ArrayList servicesList) {
for (int i = 0; i < servicesList.size(); i++) {
PrintService service = (PrintService) servicesList.get(i);
if (service.getName().equals(serviceName)) {
return service;
}
}
return null;
}
/*
* @see javax.print.PrintServiceLookup#getPrintServices(javax.print.DocFlavor
* , javax.print.attribute.AttributeSet)
*/
public PrintService[] getPrintServices(DocFlavor flavor,
AttributeSet attributes) {
PrintService[] cupsservices = getPrintServices();
if (flavor == null && attributes == null) {
return cupsservices;
}
ArrayList requestedServices = new ArrayList();
for (int i = 0; i < cupsservices.length; i++) {
try {
AttributeSet unsupportedSet = cupsservices[i]
.getUnsupportedAttributes(flavor, attributes);
if (unsupportedSet == null) {
requestedServices.add(cupsservices[i]);
}
} catch (IllegalArgumentException iae) {
// DocFlavor not supported by service, skiping.
}
}
return (requestedServices.size() == 0) ? new PrintService[0]
: (PrintService[]) requestedServices
.toArray(new PrintService[0]);
}
/*
* @see javax.print.PrintServiceLookup#getMultiDocPrintServices(javax.print.DocFlavor[]
* , javax.print.attribute.AttributeSet)
*/
public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors,
AttributeSet attributes) {
// No multidoc print services available, yet.
return new MultiDocPrintService[0];
}
/*
* find all printers
*/
private static String[] findPrintServices() {
ArrayList ippservices = new ArrayList();
/*
* First, find on localhost and servers from print.cups.servers property
* and add them to full list
*/
String[] cupses = CUPSPrintServiceProvider.getCUPSServersByProperty();
for (int j = 0; j < cupses.length; j++) {
ippservices.addAll(findCUPSPrintServices(cupses[j]));
}
/*
* Then, check URLs from print.ipp.printers property and
* if is valid ipp printer add them to full list
*/
String[] ippp = CUPSPrintServiceProvider.getIppPrintersByProperty();
for (int j = 0; j < ippp.length; j++) {
try {
URI ippuri = new URI(ippp[j]);
IppPrinter printer = new IppPrinter(ippuri);
IppResponse response;
response = printer.requestPrinterAttributes(
"printer-uri-supported", null);
Vector gg = response
.getGroupVector(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (gg != null) {
for (int i = 0, ii = gg.size(); i < ii; i++) {
IppAttributeGroup g = (IppAttributeGroup) gg.get(i);
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
ippservices.add(new String((byte[]) v.get(0)));
}
}
}
}
} catch (Exception e) {
if (verbose > 0) {
System.err.println("IPP url: " + ippp[j]);
e.printStackTrace();
} else {
// IGNORE - connection refused due to no server, etc.
}
}
}
// return array of printers
return (String[]) ippservices.toArray(new String[0]);
}
/*
* find ipp printers on CUPS server 'cups'
*/
public static ArrayList findCUPSPrintServices(String cups) {
ArrayList ippservices = new ArrayList();
URI cupsuri = null;
IppClient c = null;
IppRequest request;
IppResponse response;
IppAttributeGroup agroup;
Vector va = new Vector();
request = new IppRequest(1, 1, IppOperation.TAG_CUPS_GET_PRINTERS,
"utf-8", "en-us");
agroup = request.getGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
va.add("printer-uri-supported".getBytes());
agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
"requested-attributes", va));
try {
cupsuri = new URI(cups);
c = new IppClient(cupsuri);
response = c.request(request.getBytes());
Vector gg = response
.getGroupVector(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (gg != null) {
for (int i = 0, ii = gg.size(); i < ii; i++) {
IppAttributeGroup g = (IppAttributeGroup) gg.get(i);
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
ippservices.add(new String((byte[]) v.get(0)));
}
}
}
}
} catch (Exception e) {
if (verbose > 0) {
System.err.println("CUPS url: " + cups);
System.err.println("CUPS uri: " + cupsuri);
System.err.println("Ipp client: " + c);
System.err.println(request.toString());
e.printStackTrace();
} else {
// IGNORE - connection refused due to no server, etc.
}
}
return ippservices;
}
/*
* find default printer
* At first, try to find default printer on CUPS servers and return first found
* If failed, return first found IPP printer
* If failed return null
*/
private static String findDefaultPrintService() {
String serviceName = null;
String[] cupses = CUPSPrintServiceProvider.getCUPSServersByProperty();
for (int i = 0; i < cupses.length; i++) {
try {
URI cupsuri = new URI(cupses[i]);
IppClient c = new IppClient(cupsuri);
IppRequest request;
IppResponse response;
IppAttributeGroup agroup;
Vector va = new Vector();
request = new IppRequest(1, 1,
IppOperation.TAG_CUPS_GET_DEFAULT, "utf-8", "en-us");
agroup = request
.getGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
va.add("printer-uri-supported".getBytes());
agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
"requested-attributes", va));
response = c.request(request.getBytes());
IppAttributeGroup g = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (g != null) {
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
serviceName = new String((byte[]) v.get(0));
break;
}
}
}
} catch (URISyntaxException e) {
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
//e.printStackTrace();
}
}
if (serviceName != null && !serviceName.equals("")) {
return serviceName;
}
String[] ippp = CUPSPrintServiceProvider.getIppPrintersByProperty();
for (int i = 0; i < ippp.length; i++) {
try {
URI ippuri = new URI(ippp[i]);
IppClient c = new IppClient(ippuri);
IppRequest request;
IppResponse response;
IppAttributeGroup agroup;
Vector va = new Vector();
request = new IppRequest(1, 1,
IppOperation.GET_PRINTER_ATTRIBUTES, "utf-8", "en-us");
agroup = request
.getGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
va.add("printer-uri-supported".getBytes());
agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
"requested-attributes", va));
response = c.request(request.getBytes());
IppAttributeGroup g = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (g != null) {
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
serviceName = new String((byte[]) v.get(0));
break;
}
}
}
} catch (URISyntaxException e) {
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
//e.printStackTrace();
}
}
return serviceName;
}
public static int isVerbose() {
return verbose;
}
public static void setVerbose(int newverbose) {
CUPSPrintServiceProvider.verbose = newverbose;
CUPSClient.setVerbose(newverbose);
}
}
分享到:
相关推荐
基于4GGPRS DTU开发板的硬件图纸与软件代码全套资源,军工级电路,支持多种通信协议与数据加密,适合物联网应用。,基于4GGPRS DTU开发板的硬件图纸与软件代码全套,军工级电路,支持多种通信协议与数据加密,适用于多种物联网应用。,资料:4g GPRS DTU 开发板软件代码硬件图纸料包括:原理图,版图,单片机代码,sim800c官方资料 不含PCB板 本公司批产产品,已无故障运行数年 全套硬件图纸和软件代码。 程序比正点原子的可靠,军工级485电路。 NBIOT和4G等采用AT指令的均可参照此代码 GPRS具有比NBIOT更低的价格更好的网络,是目前低速物联网的主要通讯技术之一。 485转GPRS GPRS支持协议: TCP UDP HTTP-GET HTTP-POST FTP Md5数据加密 心跳包 电源部分,带共模电感,防反接二极管,Tvs管,5-30Vdc转5V和4V 485部分,硬件延时电路,可靠稳定 引出网络状态(兼电源)指示灯,收发指示灯,设置状态指示灯 微动按键设置工作状态 已预留LORA模块位置,若不用可将他的Io口改做他用,能引出一路串口,2路Io口 单片机
scala-intellij-bin-2024.1.1.zip
基于Android的平台书架设计实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
# 基于nRF5系列芯片和SoftDevice SDK的蓝牙低能耗应用 ## 项目简介 这是一个基于nRF5系列芯片和SoftDevice SDK的蓝牙低能耗(BLE)应用程序的示例项目。项目包含基于nRF51822和nRF52832芯片的示例代码,以及设备固件升级(DFU)相关的代码。 ## 项目的主要特性和功能 基于nRF5系列芯片项目代码适用于Nordic Semiconductor的nRF51822和nRF52832芯片,这些芯片是专为蓝牙低能耗应用设计的。 使用SoftDevice SDK项目使用了Nordic的SoftDevice SDK,这是一个高度优化的BLE堆栈,适用于nRF5系列芯片。 支持UART通信项目中的BLE应用程序通过UART接口进行通信,允许数据通过BLE连接进行发送和接收。 设备固件升级(DFU)支持项目包含用于安全设备固件升级的引导加载程序,支持固件更新的验证和存储。
矿业生产管理数字化平台解决方案.doc
Matlab领域上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
kylin v10 SP1 系统下 可以查看本机电池容量放电和充电电流
【资源介绍】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,也可以作为小白实战演练和初期项目立项演示的重要参考借鉴资料。 3、本资源作为“学习资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研和多多调试实践。 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip 基于深度学习的movielens推荐模型新版算法源码+数据+说明文档.zip
海神之光上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
1、以上文章可用于参考,请勿直接抄袭,学习、当作参考文献可以,主张借鉴学习 2、资源本身不含 对应项目代码,如需完整项目源码,请私信博主获取
1、以上文章可用于参考,请勿直接抄袭,学习、当作参考文献可以,主张借鉴学习 2、资源本身不含 对应项目代码,如需完整项目源码,请私信博主获取
14.智能台灯(语音模式)_20240318_205506.zip
数字信号处理中的采样与重构理论及其应用
python快速入门,零基础也能轻松掌握的入门指南,看着一个就够了。
LabView与三菱全系列通讯方法详解:上位机读取方法及实践,LabView与三菱全系列通讯方法及上位机数据读取攻略,labview和三菱全系列通讯方法 labview和三菱全系列通讯办法,和上位机读取方法。 ,LabVIEW; 三菱全系列通讯方法; 三菱全系列通讯办法; 上位机读取方法,LabVIEW与三菱全系列通讯方案及上位机读取方法详解
题目:基于51单片机的多参数水质监测与报警系统设计 主控:AT89C51 显示:LCD1602 DS18B20温度传感器 浊度传感器(PCF8591+滑动变阻器模拟) PH传感器(ADC0832+滑动变阻器) 声光报警 led*4 功能: 1.实时检测水质温度、浊度、PH 2.实时显示相关数据 3.可以通过按键修改阈值 4.各数值不在标准范围内启动声光报警 5.ph低于下限红色小灯点亮;ph高于上限绿色小灯电亮;温度低于阈值蓝色小灯电亮;浑浊度高于阈值橙色小灯电亮
在B站看黑马程序员视频,整理的个人笔记
java项目之水果系统源码
Office_Tool_with_runtime_v10.14.28.0_x64.zip.rar
海神之光上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作